parso icon indicating copy to clipboard operation
parso copied to clipboard

Python 3.10: Parenthesized context managers

Open gfokkema opened this issue 3 years ago • 3 comments

Besides match case, python 3.9 alpha6 / 3.10 also added parenthesized imports and context-managers. Parenthesized imports works no problem in jedi, but parenthesized context managers lead to a syntax error:

>>> import jedi
>>> source = '''
... with (
...     open('/dev/stdout', 'w') as a,
...     open('/dev/stdout', 'w') as b,
... ):
...     a.write('a')
...     b.write('b')
... '''
>>> script = jedi.Script(source, path='example.py')
>>> [print(x, x.get_message()) for x in script.get_syntax_errors()]
<SyntaxError from=(3, 29) to=(3, 31)> SyntaxError: invalid syntax
<SyntaxError from=(4, 29) to=(4, 31)> SyntaxError: invalid syntax
<SyntaxError from=(5, 0) to=(5, 1)> SyntaxError: invalid syntax
<SyntaxError from=(6, 0) to=(6, 4)> IndentationError: unexpected indent

See:

  • https://docs.python.org/3/whatsnew/3.10.html#parenthesized-context-managers

Probably relates to:

  • davidhalter/jedi#1830

gfokkema avatar Jan 17 '22 14:01 gfokkema

Moved to parso. As you said this is a more general issue with the PEG grammar (parso is only able to parse LL at the moment).

davidhalter avatar Jan 17 '22 18:01 davidhalter

@davidhalter if we agree on some sort of a verification process (or use the existing syntax error generation), we could simply add a rule like this:

namedexpr_test: asexpr_test [':=' asexpr_test]
asexpr_test: test ['as' test]

Which in theory (and also in practice, currently used by black) allows parenthesized context managers. Then if this is used anywhere outside of a with-statement's context expression, we would raise an error. This would be another inconsistency in the grammar that would be eliminated in a later stage, just like the LHS of many expressions.

Not sure if you think this would worth the effort without the full pattern matching stuff though.

isidentical avatar Jan 20 '22 17:01 isidentical

Not sure if you think this would worth the effort without the full pattern matching stuff though.

Yeah, that's mostly what I'm worried about as well. It's a good idea, but full pattern matching is probably the more pressing issue.

davidhalter avatar Jan 21 '22 12:01 davidhalter