Skip to content

Why is pytest aggressively escaping the message string provided to pytest.mark.filterwarnings? #3785

@Asday

Description

@Asday

I'd like to silence the warnings for a specific test which currently raises warnings about naive DateTimes in Django. The reason is because the test is creating noise which I don't have time to fix the underlying cause of.

My initial idea was to silence the warning, leave a comment explaining the silencing, and a comment in the offending code as a TODO.

I've run into issues, as I'd like to specifically ignore the warning about naive DateTimes. If other warnings turn up in the future, I want to see them.

From what I'm understand, pytest.mark.filterwarnings() takes a single string argument, which it splits on colons, escapes aggressively, and passes the results to warnings.filterwarnings().

My question is why is the escaping happening? My warning is as follows:

DateTimeField Model.field received a naive datetime (2018-08-04 00:00:00) while time zone support is active.

My .filterwarnings() mark is as follows:

@pytest.mark.filterwarnings(
    'ignore:'
    'DateTimeField.*received a naive datetime.*while time zone'
    ' support is active:'
    'RuntimeWarning'
)

The arguments that make it through to warnings.filterwarnings() are:

(Pdb) action
'ignore'
(Pdb) message
'DateTimeField\\.\\*received\\ a\\ naive\\ datetime\\.\\*while\\ time\\ zone\\ support\\ is\\ active'
(Pdb) category
<type 'exceptions.RuntimeWarning'>
(Pdb)

The message is re.compile()d in warnings.filterwarnings(), and that's what I'd like to leverage, but it appears pytest.mark.filterwarnings() has other ideas. Is there a reason for this? How do I get around this?

Pip list:

Package                  Version Location                                                                         
------------------------ ------- ---------------------------------------------------------------------------------
alabaster                0.7.11  
apipkg                   1.5     
asn1crypto               0.24.0  
atomicwrites             1.1.5   
attrs                    18.1.0  
Babel                    2.6.0   
bcrypt                   3.1.4   
beautifulsoup4           4.6.1   
cffi                     1.11.5  
cov-core                 1.15.0  
coverage                 3.7.1   
coveralls                0.5     
cryptography             2.3     
Django                   1.7.6   
django-braces            1.8.0   
django-colorful          1.1.0   
django-configurations    0.8     
django-dbbackup          2.3.2   
django-extensions        1.5.2   
django-fancypages        0.3.0
django-model-utils       3.1.2   
django-phonenumber-field 1.3.0   
django-qurl              0.1.1   
django-redis-cache       0.13.1  
django-rest-swagger      0.3.2   
django-secure            1.0.1   
django-sendfile          0.3.6   
django-shortuuidfield    0.1.3   
django-sslserver         0.15    
django-treebeard         4.3     
django-webtest           1.7.8   
django-widget-tweaks     1.3     
djangorestframework      2.4.5   
docopt                   0.6.2   
docutils                 0.12    
dropbox                  3.42    
enum34                   1.1.6   
execnet                  1.5.0   
Fabric                   1.14.0  
factory-boy              2.8.1   
Faker                    0.8.17  
fluffy                   0.0.0   /home/asday/code/src/github.com/[...]
funcsigs                 1.0.2   
futures                  3.2.0   
haversine                0.4.5   
idna                     2.7     
ipaddress                1.0.22  
ipython                  3.1.0   
Jinja2                   2.10    
MarkupSafe               1.0     
mock                     1.0.1   
more-itertools           4.3.0   
paramiko                 2.4.1   
pathlib2                 2.3.2   
pep8                     1.7.1   
phonenumberslite         8.9.10  
Pillow                   2.8.1   
pip                      18.0    
piprot                   0.9.10  
pkg-resources            0.0.0   
pluggy                   0.7.1   
psycopg2                 2.7.5   
pudb                     2015.2  
purl                     1.0.3   
py                       1.5.4   
pyasn1                   0.4.4   
pycparser                2.18    
Pygments                 2.2.0   
PyNaCl                   1.2.1   
pytest                   3.7.1   
pytest-cache             1.0     
pytest-cov               1.8.1   
pytest-django            3.1.2   
pytest-pep8              1.0.6   
pytest-splinter          1.7.7   
python-dateutil          2.4.2   
python-ntlm              1.1.0   
pytz                     2018.5  
PyYAML                   3.13    
qrcode                   5.1     
raven                    5.2.0   
redis                    2.10.6  
requests                 2.6.0   
requests-futures         0.9.7   
scandir                  1.8     
selenium                 2.53.5  
setuptools               40.0.0  
shortuuid                0.5.0   
six                      1.11.0  
snowballstemmer          1.2.1   
sorl-thumbnail           12.2    
Sphinx                   1.3.1   
sphinx-rtd-theme         0.1.7   
splinter                 0.7.3   
structlog                15.1.0  
text-unidecode           1.2     
unicodecsv               0.14.1  
Unidecode                1.0.22  
urllib3                  1.23    
urwid                    2.0.1   
uWSGI                    2.0.15  
waitress                 1.1.0   
WebOb                    1.8.2   
WebTest                  2.0.18  
Werkzeug                 0.10.4  
wheel                    0.31.1  

pytest version 3.7.1. Operating system Linux Mint 18.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions