-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parsing the with statement with tuple argument and type comment failed #4633
Comments
this might be related to #4632 |
Happy to report that the tool infact hit the minimal reproduction. It can be changed cosmetically slightly: with (x, y): # type:
pass But I'm very impressed. |
And I'm happy to report that this is most likely the last bug the tool has found. |
I see the problem now, it is a deviation from expectation from CPython itself (at least from what I think) In case of a type COMMENT, we get DIFFERENT ASTs, with and without the brackets: >>> import ast
>>> x = '''
... with (x, y): # type: int
... pass
... '''
>>> y = '''
... with x, y: # type: int
... pass
... '''
>>> print(ast.dump(ast.parse(x, type_comments=True), indent=2))
Module(
body=[
With(
items=[
withitem(
context_expr=Tuple(
elts=[
Name(id='x', ctx=Load()),
Name(id='y', ctx=Load())],
ctx=Load()))],
body=[
Pass()],
type_comment='int')],
type_ignores=[])
>>> print(ast.dump(ast.parse(y, type_comments=True), indent=2))
Module(
body=[
With(
items=[
withitem(
context_expr=Name(id='x', ctx=Load())),
withitem(
context_expr=Name(id='y', ctx=Load()))],
body=[
Pass()],
type_comment='int')],
type_ignores=[]) In case of type IGNORE, we get IDENTICAL ASTs with or without bracket: >>> x = '''
... with (x, y): # type: ignore
... pass
... '''
>>> y = '''
... with x, y: # type: ignore
... pass
... '''
>>> print(ast.dump(ast.parse(x, type_comments=True), indent=2))
Module(
body=[
With(
items=[
withitem(
context_expr=Name(id='x', ctx=Load())),
withitem(
context_expr=Name(id='y', ctx=Load()))],
body=[
Pass()])],
type_ignores=[
TypeIgnore(lineno=2, tag='')])
>>> print(ast.dump(ast.parse(y, type_comments=True), indent=2))
Module(
body=[
With(
items=[
withitem(
context_expr=Name(id='x', ctx=Load())),
withitem(
context_expr=Name(id='y', ctx=Load()))],
body=[
Pass()])],
type_ignores=[
TypeIgnore(lineno=2, tag='')]) |
Honestly this feels like a CPython bug, type comment on a line doesn't change semantic meaning and therefore should not change the AST. |
I agree. Do you want to create a cpython issue? |
Yup lemme do that. |
Created python/cpython#131570 |
Update on this: This was fixed in Python 3.13, and the bug doesn't reproduce on Python 3.13 either. I think it's safe to close this, but I'll let @JelleZijlstra decide. |
I think it would be ok to search the issues with pysource-codegen only with python 3.13, or to build some sort of white-list if needed. |
Describe the bug
The following code can not be parsed/formatted by black:
(playground)
black reported the following error:
the reported diff in /tmp/blk_pgrn0rje.log is:
but it can be parsed by cpython:
The code can be formatted with
black -l 100 -t py312 bug.py --fast
:Environment
Additional context
The bug was found by pysource-codegen (see #3908)
The above problem description was created from a script, let me know if you think it can be improved.
The text was updated successfully, but these errors were encountered: