Skip to content

Conversation

@ntBre
Copy link
Contributor

@ntBre ntBre commented Apr 1, 2025

Summary

This PR reimplements
load-before-global-declaration (PLE0118) as a semantic syntax error.

I added a global method to the SemanticSyntaxContext trait to make this very easy, at least in ruff. Does red-knot have something similar?

If this approach will also work in red-knot, I think some of the other PLE rules are also compile-time errors in CPython, PLE0117 in particular. 0115 and 0116 also mention SyntaxErrors in their docs, but I haven't confirmed them in the REPL yet.

Test Plan

Existing linter tests for PLE0118. I think this actually can't be tested very easily in an inline test because the TestContext doesn't have a real way to track globals.

Summary
--

This PR reimplements
[load-before-global-declaration (PLE0118)](https://docs.astral.sh/ruff/rules/load-before-global-declaration/)
as a semantic syntax error.

I added a `global` method to the `SemanticSyntaxContext` trait to make this very
easy, at least in ruff. Does red-knot have something similar?

If this approach will also work in red-knot, I think some of the other PLE rules
are also compile-time errors in CPython, PLE0117 in particular. 0115 and 0116
also mention `SyntaxError`s in their docs, but I haven't confirmed them in the
REPL yet.

Test Plan
--

Existing linter tests for PLE0118. I think this actually can't be tested very
easily in an inline test because the `TestContext` doesn't have a real way to
track globals.
@ntBre ntBre added the internal An internal refactor or improvement label Apr 1, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Apr 1, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

fn python_version(&self) -> PythonVersion;

/// Return the [`TextRange`] at which a name is declared as `global`.
fn global(&self, name: &str) -> Option<TextRange>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Red Knot doesn't support globals yet, so it's hard to say if this will be supported in Red Knot and how ;)

It does have the downside that this check will not work in the test context. And it also means that it requires one extra traversal of the body to get the globals first but I don't see how we can avoid that.

But we (you) can worry about that later ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha sounds good :)

@ntBre ntBre enabled auto-merge (squash) April 2, 2025 13:01
@ntBre ntBre merged commit d382065 into main Apr 2, 2025
21 checks passed
@ntBre ntBre deleted the brent/syn-ple0118 branch April 2, 2025 13:03
maxmynter pushed a commit to maxmynter/ruff that referenced this pull request Apr 3, 2025
Summary
--

This PR reimplements
[load-before-global-declaration
(PLE0118)](https://docs.astral.sh/ruff/rules/load-before-global-declaration/)
as a semantic syntax error.

I added a `global` method to the `SemanticSyntaxContext` trait to make
this very easy, at least in ruff. Does red-knot have something similar?

If this approach will also work in red-knot, I think some of the other
PLE rules are also compile-time errors in CPython, PLE0117 in
particular. 0115 and 0116 also mention `SyntaxError`s in their docs, but
I haven't confirmed them in the REPL yet.

Test Plan
--

Existing linter tests for PLE0118. I think this actually can't be tested
very easily in an inline test because the `TestContext` doesn't have a
real way to track globals.

---------

Co-authored-by: Micha Reiser <micha@reiser.io>
ntBre added a commit that referenced this pull request Apr 15, 2025
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-ple0118
ntBre added a commit that referenced this pull request Apr 25, 2025
…17411)

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
# try.py
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
```

I'm not sure this is the best or most elegant solution, but it was a
quick fix that passed all of our tests.

Test Plan
--

New PLE0118 test case.

[this comment]:
#7633 (comment)
[load-before-global-declaration (PLE0118)]:
https://docs.astral.sh/ruff/rules/load-before-global-declaration/#load-before-global-declaration-ple0118
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants