warnings.filterwarnings("ignore", category=psycopg2.Warning)为什么报AssertionError: category must be a Warning subclass
时间: 2024-03-20 11:43:34 浏览: 61
VS错误提示:To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
The error "AssertionError: category must be a Warning subclass" is raised because the `category` argument passed to `warnings.filterwarnings()` is expected to be a subclass of the built-in `Warning` class.
In this specific case, `psycopg2.Warning` is not a subclass of `Warning`, which is the base class for all warning categories in Python. Instead, it's a warning category defined by the `psycopg2` library.
To fix the error, you should replace `psycopg2.Warning` with a subclass of `Warning` that you define yourself or that is provided by another library. For example:
```
import warnings
from my_warnings import MyCustomWarning
warnings.filterwarnings("ignore", category=MyCustomWarning)
```
Here, `MyCustomWarning` is a subclass of `Warning` that you define in a separate module named `my_warnings`. You can also use other built-in warning classes such as `DeprecationWarning` or `UserWarning`.
阅读全文