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

[syntax-errors] Store to or delete __debug__ #16984

Merged
merged 6 commits into from
Mar 29, 2025
Merged

[syntax-errors] Store to or delete __debug__ #16984

merged 6 commits into from
Mar 29, 2025

Conversation

ntBre
Copy link
Contributor

@ntBre ntBre commented Mar 26, 2025

Summary

Detect setting or deleting __debug__. Assigning to __debug__ was a SyntaxError on the earliest version I tested (3.8). Deleting __debug__ was made a SyntaxError in BPO 45000, which said it was resolved in Python 3.10. However, del __debug__ was also a runtime error (NameError) when I tested in Python 3.9.6, so I thought it was worth including 3.9 in this check.

I don't think it was ever a good idea to try del __debug__, so I think there's also an argument for not making this version-dependent at all. That would only simplify the implementation very slightly, though.

Test Plan

New inline tests. This also required adding a PythonVersion field to the TestContext that could be taken from the inline ParseOptions and making the version field on the options accessible.

Summary
--

Detect setting or deleting `__debug__`. Assigning to `__debug__` was a
`SyntaxError` on the earliest version I tested (3.8). Deleting `__debug__` was
made a `SyntaxError` in [BPO 45000], which said it was resolved in Python 3.10.
However, `del __debug__` was also a runtime error (`NameError`) when I tested in
Python 3.9.6, so I thought it was worth including 3.9 in this check.

I don't think it was ever a *good* idea to try `del __debug__`, so I think
there's also an argument for not making this version-dependent at all. That
would only simplify the implementation very slightly, though.

[BPO 45000]: python/cpython#89163

Test Plan
--

New inline tests. This also required adding a `PythonVersion` field to the
`TestContext` that could be taken from the inline `ParseOptions` and making the
version field on the options accessible.
@ntBre ntBre added rule Implementing or modifying a lint rule preview Related to preview mode features labels Mar 26, 2025
Copy link
Contributor

github-actions bot commented Mar 26, 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.

@dhruvmanila
Copy link
Member

I don't think it was ever a good idea to try del __debug__, so I think there's also an argument for not making this version-dependent at all. That would only simplify the implementation very slightly, though.

I'd be in favor of just making it version independent but happy to hear others thoughts on this.

// # parse_options: {"target-version": "3.8"}
// del __debug__

// test_ok read_from_debug
Copy link
Member

Choose a reason for hiding this comment

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

I think there are more cases that need handling where we don't use an ExprName in the AST

from x import __debug__
import __debug__

match x:
	__debug__: ...

f(__debug__ = b)

Finding the Identifier nodes is somewhat annoying because our visitor doesn't traverse Identifier nodes. You can take a look at where I believe I identified all Identifier nodes

pub(crate) enum GoToTarget<'a> {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah good catch. I think I should have covered all of these now.

For match patterns, I moved the check into the MultipleCaseAssignmentVisitor, so it might be worth renaming that to something more generic like MatchPatternVisitor or just PatternVisitor now too.

Comment on lines 156 to 162
if type_param.name() == "__debug__" {
Self::add_error(
ctx,
SemanticSyntaxErrorKind::WriteToDebug(WriteToDebugKind::Store),
type_param.name().range,
);
}
Copy link
Member

Choose a reason for hiding this comment

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

should we add a check_identifier function that handles the if name == "__debug__" handling to avoid some repetition?

@ntBre ntBre merged commit a0819f0 into main Mar 29, 2025
22 checks passed
@ntBre ntBre deleted the brent/del-debug-3.10 branch March 29, 2025 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants