pythonwarningsdeprecation-warning

ignore Python SyntaxError coming from `PYTHONWARNINGS="error::Warning`?


I'm using PYTHONWARNINGS="error::Warning" to raise exceptions on warnings, but I want to ignore certain such exceptions, which is normally fine. When the exception that results is SyntaxError, I would sometimes like to ignore that but don't know how.

Detailed example:

# has_deprecated_syntax.py
"""this becomes invalid: \* """

Then:

`$PYTHONWARNINGS="error::Warning" python3.6 -c "import     has_deprecated_syntax"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/davidchudzicki/temp/so/has_deprecated_syntax.py", line 1
    """this becomes invalid: \* """
    ^
SyntaxError: invalid escape sequence \*

These attempts to ignore it both got me the same failure:

PYTHONWARNINGS="error::Warning,default::Warning:has_deprecated_syntax" python3.6 -c "import has_deprecated_syntax" 

and

PYTHONWARNINGS="error::Warning,default::SyntaxError" python3.6 -c "import has_deprecated_syntax"

For reference, here's what makes me think this is the correct environment setting in general for ignoring warnings from particular modules:

With:

# has_warning.py
import warnings
warnings.warn("hi")

... I get:

$PYTHONWARNINGS="error::Warning" python -c "import has_warning"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "has_warning.py", line 3, in <module>
    warnings.warn("hi")
UserWarning: hi

...which I can ignore with:

$PYTHONWARNINGS="error::Warning,default::Warning:has_warning" python -c 
"import has_warning"
has_warning.py:3: UserWarning: hi
  warnings.warn("hi")

But it doesn't work for SyntaxError. Is there any way to ignore those from particular modules?

(Note: I've constructed a small example for this question, but the real reason I care is that I want to fail on warnings in continuous integration for package I help with, but I get the SyntaxError from one of our dependencies when I do this.)


Solution

  • This worked for me:

    python -W error -W 'ignore:invalid escape sequence' -c "import has_deprecated_syntax"