You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
compile fails on 3.14 on a valid expression when -OO is set #137308
So the bisected commit actually had a side-effect where ast.parse() seems to inherit the value of optimize from the interpreter [it should per the docs but it didn't seem to be the case previously] (it's only inherited by default, not when we explicitly indicate optimize=...)
EDIT:
Let's quote the ast.parse docs again:
Parse the source into an AST node. Equivalent to compile(source, filename, mode,
flags=FLAGS_VALUE, optimize=optimize), where FLAGS_VALUE is ast.PyCF_ONLY_AST if
optimize <= 0 and ast.PyCF_OPTIMIZED_AST otherwise.
I think the issue is how we handle the optimize default value and when we actually handle it (and how we then determine the flags to pass). In Python 3.13 it seems that the flags and optimize values passed to compile after calling ast.parse were not the correct ones (I haven't checked) and incidently, they are now correct. Note that AST optimization (defined via ast.PyCF_*( has nothing to do with optimize=... which is about parser optimization as one could still have docstrings even if we had ast.PyCF_OPTIMIZED_AST. Docstrings disappear once we have optimize=2 or -OO.
More generally, eliminating the docstring makes the class def (or the function def) empty. A similar error occurs with a function. Maybe docstring elimination should always add a "pass" statement instead? (namely we replace docstrings by pass)
More generally, eliminating the docstring makes the class def (or the function def) empty. A similar error occurs with a function. Maybe docstring elimination should always add a "pass" statement instead? (namely we replace docstrings by pass)
Just catching up on this. This would work, but I think it would add a line event for the line of the docstring, and I'm not sure whether that's desirable. Maybe add it only when the body is empty?
Activity
StanFromIreland commentedon Aug 1, 2025
CC @sobolevn (due to
-OO)The docstring is optimised away here, I’m not sure why it was allowed in 3.13.
picnixz commentedon Aug 1, 2025
I think we somehow changed the parser.
With Python 3.12:
With main:
EDIT:
Apparently, it's more than that. The docstring is not properly eliminated prior to Python 3.14:
brianschubert commentedon Aug 1, 2025
Bisects to ffc2f1d (#130087), cc @iritkatriel
picnixz commentedon Aug 1, 2025
I think it's because when we parse the AST we somehow also optimize it, namely we inherit from "-OO":
So the bisected commit actually had a side-effect where ast.parse() seems to inherit the value of
optimizefrom the interpreter [it should per the docs but it didn't seem to be the case previously] (it's only inherited by default, not when we explicitly indicate optimize=...)EDIT:
Let's quote the
ast.parsedocs again:I think the issue is how we handle the
optimizedefault value and when we actually handle it (and how we then determine the flags to pass). In Python 3.13 it seems that the flags and optimize values passed tocompileafter callingast.parsewere not the correct ones (I haven't checked) and incidently, they are now correct. Note that AST optimization (defined viaast.PyCF_*( has nothing to do withoptimize=...which is about parser optimization as one could still have docstrings even if we hadast.PyCF_OPTIMIZED_AST. Docstrings disappear once we haveoptimize=2or-OO.picnixz commentedon Aug 1, 2025
More generally, eliminating the docstring makes the class def (or the function def) empty. A similar error occurs with a function. Maybe docstring elimination should always add a "pass" statement instead? (namely we replace docstrings by pass)
pythongh-137308: Replace a single docstring with `pass`
gh-137308: Replace a single docstring with `pass` in `-OO` mode (#137318
pythongh-137308: Replace a single docstring with `pass` in `-OO` mode (…
iritkatriel commentedon Aug 4, 2025
Just catching up on this. This would work, but I think it would add a line event for the line of the docstring, and I'm not sure whether that's desirable. Maybe add it only when the body is empty?
sobolevn commentedon Aug 4, 2025
@iritkatriel this is exactly what I did :)
When
.bodycontains only a docstring, we replace it withpass, otherwise, we just remove it.[3.14] gh-137308: Replace a single docstring with `pass` in `-OO` mode (
StanFromIreland commentedon Aug 10, 2025
The documentation was not updated accordingly after b74f3be IMO. I think it should note the exception.
picnixz commentedon Aug 10, 2025
I don't think we should update anything here. Docstrings are properly eliminated and we just have
passinstead.pythongh-137308: Replace a single docstring with `pass` in `-OO` mode (…
[3.14] pythongh-137308: Replace a single docstring with `pass` in `-O…