Skip to content

Redundant and possibly misleading SyntaxWarnings #97029

@aroberge

Description

@aroberge

Feature or enhancement

This report actually suggest removing a feature.

When trying to add information about warnings in friendly-traceback, I found out that cPython emits SyntaxWarnings for code that triggers exceptions. In some cases, the warnings include hints of dubious value. Ignoring the hint, the message tacked on to the SyntaxWarning is identical to that provided by the exception that is raised.

Here are two examples.

Python 3.10.2 ... on win32
>>> import warnings
>>> warnings.simplefilter("always")
>>> 1[2]
<stdin>:1: SyntaxWarning: 'int' object is not subscriptable; perhaps you missed a comma?
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not subscriptable
>>> [1, 2, 3]()
<stdin>:1: SyntaxWarning: 'list' object is not callable; perhaps you missed a comma?
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

I am wondering about the value of emitting such warnings, and if this does result in reduced efficiency.

I realize that, if one uses compile(statement, filename, "single"), a SyntaxWarning will be shown without triggering an exception, but even then I fail to see the usefulness of emitting such warnings, and wonder about the additional code complexity required to provide this information, which is likely not visible by the vast majority of users who do not enable warnings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions