Commit 5140fa3
committed
[
Summary
--
While going through the syntax errors in [this comment], I was surprised to see
the error `name 'x' is assigned to before global declaration`, which corresponds
to [load-before-global-declaration (PLE0118)] and has also been reimplemented as
a syntax error (#17135). However, it looks like neither of the implementations
consider `global` declarations in the top-level module scope, which is a syntax
error in CPython:
```python
x = None
global x
```
```shell
> python -m compileall -f try.py
Compiling 'try.py'...
*** File "try.py", line 2
global x
^^^^^^^^
SyntaxError: name 'x' is assigned to before global declaration
```
Test Plan
--
New PLE0118 test case.
We should also check the codspeed results carefully here. `Globals::from_body`
visits all of the statements in `body`, so this might be a really bad idea, but
it looked like the simplest fix.
[this comment]: #7633 (comment)
[load-before-global-declaration (PLE0118)]: https://docs.astral.sh/ruff/rules/load-before-global-declaration/#load-before-global-declaration-ple0118pylint] Detect global declarations in module scope (PLE0118)1 parent 312a487 commit 5140fa3
4 files changed
Lines changed: 39 additions & 18 deletions
File tree
- crates
- ruff_linter
- resources/test/fixtures/pylint
- src
- checkers/ast
- rules/pylint/snapshots
- ruff_python_semantic/src
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
156 | 156 | | |
157 | 157 | | |
158 | 158 | | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1971 | 1971 | | |
1972 | 1972 | | |
1973 | 1973 | | |
| 1974 | + | |
| 1975 | + | |
| 1976 | + | |
| 1977 | + | |
1974 | 1978 | | |
1975 | 1979 | | |
1976 | 1980 | | |
| |||
Lines changed: 8 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1503 | 1503 | | |
1504 | 1504 | | |
1505 | 1505 | | |
1506 | | - | |
1507 | | - | |
1508 | | - | |
1509 | | - | |
1510 | | - | |
1511 | | - | |
1512 | | - | |
1513 | | - | |
1514 | | - | |
1515 | | - | |
1516 | | - | |
1517 | | - | |
1518 | | - | |
1519 | | - | |
1520 | | - | |
1521 | | - | |
1522 | | - | |
1523 | | - | |
| 1506 | + | |
| 1507 | + | |
| 1508 | + | |
| 1509 | + | |
| 1510 | + | |
| 1511 | + | |
| 1512 | + | |
| 1513 | + | |
| 1514 | + | |
| 1515 | + | |
| 1516 | + | |
| 1517 | + | |
| 1518 | + | |
| 1519 | + | |
| 1520 | + | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
| 1526 | + | |
| 1527 | + | |
1524 | 1528 | | |
1525 | 1529 | | |
1526 | 1530 | | |
| |||
0 commit comments