Skip to content
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

Illegal "yield (from)" python syntax accepted #89

Open
charliermarsh opened this issue Jun 13, 2023 · 0 comments
Open

Illegal "yield (from)" python syntax accepted #89

charliermarsh opened this issue Jun 13, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@charliermarsh
Copy link
Contributor

Originally reported as astral-sh/ruff#4992.

I'm not quite sure where the incorrectness here is, but I think this bug indicates an incorrect parsing of the yield from expression. The following is a python snippet with illegal syntax:

def e():
  x = yield from y = 5

We can verify that it is indeed illegal syntax with:

$ python yield-elision.py 
  File "/tmp/tmp.1z4PScoI6T/yield-elision.py", line 2
    x = yield from y = 5
        ^^^^^^^^^^^^
SyntaxError: assignment to yield expression not possible

Ruff, however, believes this is valid syntax:

$ ruff yield-elision.py --show-source --show-fixes
yield-elision.py:2:3: F841 [*] Local variable `x` is assigned to but never used
  |
2 | def e():
3 |   x = yield from y = 5
  |   ^ F841
  |
  = help: Remove assignment to unused variable `x`

yield-elision.py:2:18: F821 Undefined name `y`
  |
2 | def e():
3 |   x = yield from y = 5
  |                  ^ F821
  |

Found 2 errors.

And will attempt to fix it, leading to a self-reported parsing error:

$ ruff yield-elision.py  --fix
error: Autofix introduced a syntax error in `yield-elision.py` with rule codes F841: invalid syntax. Got unexpected token '=' at byte offset 24
---
def e():
  yield from y = 5

---
yield-elision.py:2:3: F841 Local variable `x` is assigned to but never used
yield-elision.py:2:18: F821 Undefined name `y`
Found 2 errors.

I believe this indicates a parsing bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant