SyntaxError contains more useful information than SyntaxWarning, and SyntaxError is special cased in tracebacks for better reporting. When SyntaxWarning is raised as an exception, the error report doesn't contain information about the source. It is hard to find the source of a SyntaxWarning. Currently:
$ ./python syntaxwarning.py
syntaxwarning.py:1: SyntaxWarning: assertion is always true, perhaps remove parentheses?
x = 1; assert (x, "msg")
$ ./python -We syntaxwarning.py
SyntaxWarning: assertion is always true, perhaps remove parentheses?
$ ./python -m syntaxwarning
/home/serhiy/py/cpython3.7/syntaxwarning.py:1: SyntaxWarning: assertion is always true, perhaps remove parentheses?
x = 1; assert (x, "msg")
$ ./python -We -m syntaxwarning
Traceback (most recent call last):
File "/home/serhiy/py/cpython3.7/Lib/runpy.py", line 183, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/home/serhiy/py/cpython3.7/Lib/runpy.py", line 153, in _get_module_details
code = loader.get_code(mod_name)
File "<frozen importlib._bootstrap_external>", line 860, in get_code
File "<frozen importlib._bootstrap_external>", line 791, in source_to_code
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
SyntaxWarning: assertion is always true, perhaps remove parentheses?
The proposed PR replaces SyntaxWarning with a SyntaxError if the former was raised as an exception:
$ ./python -We syntaxwarning.py
File "syntaxwarning.py", line 1
x = 1; assert (x, "msg")
^
SyntaxError: assertion is always true, perhaps remove parentheses?
$ ./python -We -m syntaxwarning
Traceback (most recent call last):
File "/home/serhiy/py/cpython/Lib/runpy.py", line 183, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/home/serhiy/py/cpython/Lib/runpy.py", line 153, in _get_module_details
code = loader.get_code(mod_name)
File "<frozen importlib._bootstrap_external>", line 909, in get_code
File "<frozen importlib._bootstrap_external>", line 839, in source_to_code
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/serhiy/py/cpython/syntaxwarning.py", line 1
x = 1; assert (x, "msg")
^
SyntaxError: assertion is always true, perhaps remove parentheses?
Similar replacement is already performed for warnings raised for unrecognized escape sequences at parsing time (see issue32912).
|