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

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]: https://github.com/python/cpython/issues/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.
This commit is contained in:
Brent Westbrook 2025-03-29 12:07:20 -04:00 committed by GitHub
parent 8396d7cd63
commit a0819f0c51
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 1372 additions and 7 deletions

View file

@ -0,0 +1,2 @@
class __debug__: ... # class name
class C[__debug__]: ... # type parameter name

View file

@ -0,0 +1,3 @@
def __debug__(): ... # function name
def f[__debug__](): ... # type parameter name
def f(__debug__): ... # parameter name

View file

@ -0,0 +1,4 @@
import __debug__
import debug as __debug__
from x import __debug__
from x import debug as __debug__

View file

@ -0,0 +1,2 @@
match x:
case __debug__: ...

View file

@ -0,0 +1,2 @@
try: ...
except Exception as __debug__: ...

View file

@ -0,0 +1,2 @@
type __debug__ = list[int] # visited as an Expr but still flagged
type Debug[__debug__] = str

View file

@ -0,0 +1 @@
with open("foo.txt") as __debug__: ...

View file

@ -0,0 +1,2 @@
# parse_options: {"target-version": "3.9"}
del __debug__

View file

@ -0,0 +1,4 @@
del __debug__
del x, y, __debug__, z
__debug__ = 1
x, y, __debug__, z = 1, 2, 3, 4

View file

@ -0,0 +1,3 @@
import __debug__ as debug
from __debug__ import Some
from x import __debug__ as debug

View file

@ -0,0 +1,2 @@
# parse_options: {"target-version": "3.8"}
del __debug__

View file

@ -0,0 +1,2 @@
if __debug__: ...
x = __debug__