This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Convert SyntaxWarning exception raised at code generation time to a SyntaxError
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: serhiy.storchaka
Priority: normal Keywords: patch

Created on 2018-10-20 07:24 by serhiy.storchaka, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 9999 merged serhiy.storchaka, 2018-10-20 07:29
Messages (2)
msg328130 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-20 07:24
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).
msg328192 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-21 07:09
New changeset d31e7730cd5d74efbd7320751dacd51d09cc415d by Serhiy Storchaka in branch 'master':
bpo-35029: Replace the SyntaxWarning exception with a SyntaxError. (GH-9999)
https://github.com/python/cpython/commit/d31e7730cd5d74efbd7320751dacd51d09cc415d
History
Date User Action Args
2022-04-11 14:59:07adminsetgithub: 79210
2018-10-21 07:28:51serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-10-21 07:09:43serhiy.storchakasetmessages: + msg328192
2018-10-20 07:29:14serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request9341
2018-10-20 07:24:03serhiy.storchakacreate