Skip to content

[syntax-errors] Extend __debug__ write check to attribute targets - #27908

Merged
ntBre merged 5 commits into
astral-sh:mainfrom
Sacrimento:syntax-error-debug-attribute
Nov 3, 2026
Merged

ntBre merged 5 commits into
astral-sh:mainfrom
Sacrimento:syntax-error-debug-attribute

Conversation

@Sacrimento

@Sacrimento Sacrimento commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Ruff currently flags assigning to or deleting __debug__ when it's a bare name (__debug__ = False, del __debug__), but not when it's an attribute target (x.__debug__ = False, del x.__debug__).

CPython forbids both assigning to x.__debug__ (SyntaxError on every supported Python version) and deleting x.__debug__ only became a SyntaxError in Python 3.14.

This PR extends the existing __debug__ check to Expr::Attribute targets, gated on the target Python version for the deletion case.

Part of #17412.

Test Plan

Added inline test_err/test_ok fixtures alongside the new Expr::Attribute check
Cross-checked the version gating logic and errors against real CPython 3.12/3.13/3.14.

@astral-sh-bot

astral-sh-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged βœ…

@astral-sh-bot

astral-sh-bot Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected βœ…

Full report with detailed diff (timing results)

@astral-sh-bot

astral-sh-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

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.

@Sacrimento
Sacrimento marked this pull request as ready for review August 19, 2026 21:05
@MichaReiser
MichaReiser requested review from ntBre and removed request for dhruvmanila August 20, 2026 06:34
@MichaReiser MichaReiser added the parser Related to the parser label Aug 20, 2026
@MichaReiser

Copy link
Copy Markdown
Member

Thank you for looking into this

CPython forbids assigning to or deleting `__debug__` when accessed as
an attribute (`x.__debug__`). Attribute assignment is a `SyntaxError`
on all supported versions, but attribute deletion only became one in
Python 3.14.

This change extends `WriteToDebugKind::Delete` with a `removed_in`
field so the diagnostic message reports the version-specific removal,
and factors the shared `Store`/`Del` dispatch used by both the `Name`
and `Attribute` checks into a single `write_to_debug` helper.

Closes part of astral-sh#17412.
@Sacrimento
Sacrimento force-pushed the syntax-error-debug-attribute branch from 2fb9008 to c76b78a Compare August 31, 2026 21:41
@Sacrimento
Sacrimento requested a review from MichaReiser August 31, 2026 21:50
@codspeed

codspeed Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

βœ… 288 untouched benchmarks
⏩ 14 skipped benchmarks1


Comparing Sacrimento:syntax-error-debug-attribute (c7c7df4) with main (5a85f9b)

Open in CodSpeed

Footnotes

  1. 14 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@MichaReiser
MichaReiser removed their request for review September 23, 2026 06:59

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you! I made a few simplifications in line with my review comments, but this looked good to me overall.

@ntBre
ntBre dismissed MichaReiser’s stale review November 3, 2026 17:43

requested changes were made

@ntBre
ntBre merged commit 888976c into astral-sh:main Nov 3, 2026
77 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parser Related to the parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants