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

Ruff does not signal E999 SyntaxError on literal assignment. #6895

Closed
0x6d6e647a opened this issue Aug 26, 2023 · 1 comment · Fixed by #8524
Closed

Ruff does not signal E999 SyntaxError on literal assignment. #6895

0x6d6e647a opened this issue Aug 26, 2023 · 1 comment · Fixed by #8524
Labels
bug Something isn't working

Comments

@0x6d6e647a
Copy link

I'm using Ruff v0.0.286 in the following.

For the following Python code:

#!/usr/bin/env python
5 = 3

No output is generated from ruff when analyzed:

user@host $ ruff literal_assign.py

When run from Python:

user@host $ python literal_assign.py
  File "/home/user/literal_assign.py", line 2
    5 = 3
    ^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

When analyzed with flake8:

user@host $ flake8 literal_assign.py
literal_assign.py:2:2: E999 SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
@charliermarsh charliermarsh added the bug Something isn't working label Aug 26, 2023
@charliermarsh
Copy link
Member

Yeah the parser is more permissive than the Python interpreter -- this is better than being less permissive (i.e., rejecting valid code), but it'd be nice to error here.

BurntSushi added a commit that referenced this issue Nov 6, 2023
This commit adds some additional error checking to the parser such
that assignments that are invalid syntax are rejected. This covers the
obvious cases like `5 = 3` and some not so obvious cases like `x + y =
42`.

This does add an additional recursive call to the parser for the cases
handling assignments. I had initially been concerned about doing this,
but `set_context` is already doing recursion during assignments, so I
didn't feel as though this was changing any fundamental performance
characteristics of the parser. (Also, in practice, I would expect any
such recursion here to be quite shallow since the recursion is done
on the target of an assignment. Such things are rarely nested much in
practice.)

Fixes #6895
BurntSushi added a commit that referenced this issue Nov 6, 2023
This commit adds some additional error checking to the parser such
that assignments that are invalid syntax are rejected. This covers the
obvious cases like `5 = 3` and some not so obvious cases like `x + y =
42`.

This does add an additional recursive call to the parser for the cases
handling assignments. I had initially been concerned about doing this,
but `set_context` is already doing recursion during assignments, so I
didn't feel as though this was changing any fundamental performance
characteristics of the parser. (Also, in practice, I would expect any
such recursion here to be quite shallow since the recursion is done
on the target of an assignment. Such things are rarely nested much in
practice.)

Fixes #6895
BurntSushi added a commit that referenced this issue Nov 6, 2023
This commit adds some additional error checking to the parser such
that assignments that are invalid syntax are rejected. This covers the
obvious cases like `5 = 3` and some not so obvious cases like `x + y =
42`.

This does add an additional recursive call to the parser for the cases
handling assignments. I had initially been concerned about doing this,
but `set_context` is already doing recursion during assignments, so I
didn't feel as though this was changing any fundamental performance
characteristics of the parser. (Also, in practice, I would expect any
such recursion here to be quite shallow since the recursion is done
on the target of an assignment. Such things are rarely nested much in
practice.)

Fixes #6895
BurntSushi added a commit that referenced this issue Nov 7, 2023
## Summary

This commit adds some additional error checking to the parser such that
assignments that are invalid syntax are rejected. This covers the
obvious cases like `5 = 3` and some not so obvious cases like `x + y =
42`.

This does add an additional recursive call to the parser for the cases
handling assignments. I had initially been concerned about doing this,
but `set_context` is already doing recursion during assignments, so I
didn't feel as though this was changing any fundamental performance
characteristics of the parser. (Also, in practice, I would expect any
such recursion here to be quite shallow since the recursion is done on
the target of an assignment. Such things are rarely nested much in
practice.)

Fixes #6895

## Test Plan

I've added unit tests covering every case that is detected as invalid on
an `Expr`.
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

Successfully merging a pull request may close this issue.

2 participants