如何也忽略 pytest 中关于 Django41 中删除的警告?

How to also ignore warnings in pytest about removal in Django41?

https://docs.pytest.org/en/6.2.x/warnings.html#deprecationwarning-and-pendingdeprecationwarning 教我在 pytest.ini

中使用它

filterwarnings = ignore:.*U.*mode is deprecated:DeprecationWarning

但我仍然收到这样的警告:

../usr/local/lib/python3.8/dist-packages/django/apps/registry.py:91
  /usr/local/lib/python3.8/dist-packages/django/apps/registry.py:91: RemovedInDjango41Warning: 'pattern_library' defines default_app_config = 'pattern_library.apps.PatternLibraryAppConfig'. Django now detects this configuration automatically. You can remove default_app_config.
    app_config = AppConfig.create(entry)

我也想取消这个,因为这也是一个第三方库,我无法控制它,即使我已经提交了 PR 来更新它。

我应该在 pytest.ini 中添加什么?

您可以在给定警告的完整导入路径上注册过滤器 class;这是 django.utils.deprecation.RemovedInDjango41Warning:

filterwarnings =
    ignore:.*U.*mode is deprecated:DeprecationWarning
    ignore:.*Django now detects this configuration.*:django.utils.deprecation.RemovedInDjango41Warning

根据您的 Django 版本,RemovedInDjango41WarningDeprecationWarning (Django 4.0, still in development) or of PendingDeprecationWarning (Django 3.2) 的子class,并且警告过滤器与 subclass 匹配为嗯,所以:

ignore:.*Django now detects this configuration.*:PendingDeprecationWarning
ignore:.*Django now detects this configuration.*:DeprecationWarning

也适用于 Django 3.2 和 4.0。