Mayil-AI-Sandbox / loguru-Jan2023

MIT License
0 stars 0 forks source link

Loguru + warning handler suppressing DeprecationWarnings in pytest (hashtag641) #77

Closed vikramsubramanian closed 2 months ago

vikramsubramanian commented 2 months ago

Hi again !

I'm moving a unittest-based test suite to pytest. We're using loguru together with a warning handler, something like this (only to show minimal reproducible code):

import warnings
from loguru import logger

def warning_handler(message, category, filename, lineno, file=None, line=None):
    new_message = warnings.formatwarning(
        message, category, filename, lineno, line
    )
    logger.warning(new_message)

warnings.showwarning = warning_handler

When I run pytest locally (pytest -rxXsw), and without the warning handler I see this:

===== warnings summary ======
../../../opt/venv/lib/python3.10/site-packages/playhouse/cockroachdb.py:69
  /opt/venv/lib/python3.10/site-packages/playhouse/cockroachdb.py:69: DeprecationWarning: invalid escape sequence '\d'
    match_obj = re.match('^CockroachDB.+?v(\d+)\.(\d+)\.(\d+)', raw)

../../../opt/venv/lib/python3.10/site-packages/playhouse/migrate.py:668
  /opt/venv/lib/python3.10/site-packages/playhouse/migrate.py:668: DeprecationWarning: invalid escape sequence '\('
    column_re = re.compile('(.+?)\((.+)\)')

../../../opt/venv/lib/python3.10/site-packages/playhouse/migrate.py:825
  /opt/venv/lib/python3.10/site-packages/playhouse/migrate.py:825: DeprecationWarning: invalid escape sequence '\]'
    if re.match('%s(?:[\'"`\]]?\s|$)' % column_to_update, column):

../../../opt/venv/lib/python3.10/site-packages/peewee.py:236: 1 warning
tests/test_calculate.py: 1 warning
tests/cronjobs/test_stuff.py: 1 warning
tests/cronjobs/test_more_stuff.py: 2 warnings
  /opt/venv/lib/python3.10/site-packages/peewee.py:236: DeprecationWarning: "db_column" has been deprecated in favor of "column_name" for Field objects.
    warnings.warn(s, DeprecationWarning)

-- Docs: 

But as soon as I enable the warning handler (warnings.showwarning = warning_handler), the output looks like this when running pytest:

===== warnings summary ======
tests/test_calculate.py: 1 warning
tests/cronjobs/test_stuff.py: 1 warning
tests/cronjobs/test_more_stuff.py: 2 warnings
  /opt/venv/lib/python3.10/site-packages/peewee.py:236: DeprecationWarning: "db_column" has been deprecated in favor of "column_name" for Field objects.
    warnings.warn(s, DeprecationWarning)

-- Docs: 

Do you see what could possibly be going on here, and why the captured warning, re-logged via loguru, was not enough for pytest to show it in the warnings summary?

hashtaghashtaghashtag Side notes

The reason for doing this warning handling is I'm using Datadog and want to log everything to JSON format. Third-party libraries which are raising DeprecationWarnings must therefore be captured, so I can reformat them and set the desired severity level. I also use the InterceptHandler from the README.md, but for some reason the interceptor did not capture DeprecationWarnings from e.g. [redis]( but it worked as soon as I added this warning handler.

For local execution, I'm not using Datadog and I do not format the loguru logs (I'm using the default formatting).

vikramsubramanian commented 2 months ago

Hey

Sorry, despite the code snippet you shared, I'm unable to reproduce your issue. Do you have the possibility to share a more complete example to reproduce the error, with possibly the list of dependencies and their versions?

However I note the following:

vikramsubramanian commented 2 months ago

sorry for making this topic stale so quickly. Things have been busy at work but I'll revisit this next week.

vikramsubramanian commented 2 months ago

I will close this for now as I don't have time to investigate this. Sorry for the noise. I will try to revisit this as I have an internal issue about it in my org.