Description
Feature or enhancement
Proposal:
Syntax warnings during AST parsing use _PyCompile_Warn
to setup their warning context automatically. Though the filename is correct, the module is inferred and often not helpful. This is especially an issue when trying to ignore specific errors in a CI environment with pytest where the module is suddenly importlib._bootstrap
. In other cases with python alone it's often just sys
.
AFAICT this applies mainly to the "is" with '...' literal
and 'return' in 'finally' block
warnings.
--
To reproduce
# test.py
class A:
def __init__(self):
self.var = 2
def func(self) -> bool:
return self.var is 2
./python.exe -W error::SyntaxWarning:sys test.py
File "/.../test.py", line 7
return self.var is 2
^^^^^^^^^^^^^
SyntaxError: "is" with 'int' literal. Did you mean "=="?
Note: The SyntaxWarning is changed to an error for module sys
where test
would have been the expected module.
--
The SyntaxWarning is triggered here:
Lines 1831 to 1837 in b14986c
Lines 1204 to 1214 in b14986c
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response