Skip to content

Commit ded561e

Browse files
committed
Fix syntax error false positives on parenthesized context managers
This PR resolves the issue noticed in #20777 (comment). Namely, cases like this were being flagged as syntax errors despite being perfectly valid on Python 3.8: ```pycon Python 3.8.20 (default, Oct 2 2024, 16:34:12) [Clang 18.1.8 ] on linux Type "help", "copyright", "credits" or "license" for more information. >>> with (open("foo.txt", "w")): ... ... Ellipsis >>> with (open("foo.txt", "w")) as f: print(f) ... <_io.TextIOWrapper name='foo.txt' mode='w' encoding='UTF-8'> ``` The second of these was already allowed but not the first: ```shell > ruff check --target-version py38 --ignore ALL - <<EOF with (open("foo.txt", "w")): ... with (open("foo.txt", "w")) as f: print(f) EOF invalid-syntax: Cannot use parentheses within a `with` statement on Python 3.8 (syntax was added in Python 3.9) --> -:1:6 | 1 | with (open("foo.txt", "w")): ... | ^ 2 | with (open("foo.txt", "w")) as f: print(f) | Found 1 error. ``` There was some discussion of related cases in #16523 (comment), but it seems I overlooked the single-element case when examining tuple special cases. This PR also removes the existing false positive for tuples that we intentionally accepted there. If I'm understanding correctly, it turns out that we didn't need to modify the parser, and cases like: ```python with (a, b, c) as foo: ... # parses but fails at runtime, tuple doesn't have __enter__ ``` and ```
1 parent 975891f commit ded561e

File tree

0 file changed

+0
-0
lines changed

    0 file changed

    +0
    -0
    lines changed

    0 commit comments

    Comments
     (0)