Skip to content

[ty] Reject deletingFinal attributes#24508

Merged
charliermarsh merged 1 commit intomainfrom
charlie/deleter-ii
Apr 10, 2026
Merged

[ty] Reject deletingFinal attributes#24508
charliermarsh merged 1 commit intomainfrom
charlie/deleter-ii

Conversation

@charliermarsh
Copy link
Copy Markdown
Member

Summary

We now error when attempting to delete a Final attribute, as in:

from typing import Final

class FinalAttribute:
    def __init__(self) -> None:
        self.x: Final[int] = 1

final_attribute = FinalAttribute()
# error: [invalid-assignment] "Cannot delete final attribute `x` on type `FinalAttribute`"
del final_attribute.x

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Apr 9, 2026
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented Apr 9, 2026

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 87.80%. The percentage of expected errors that received a diagnostic held steady at 82.91%. The number of fully passing files held steady at 76/132.

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented Apr 9, 2026

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented Apr 9, 2026

ecosystem-analyzer results

Lint rule Added Removed Changed
invalid-await 40 0 0
invalid-return-type 2 0 0
invalid-assignment 1 0 0
Total 43 0 0

Large timing changes:

Project Old Time New Time Change
pandas 1.09s 1.64s +50%

Changes in flaky projects detected. Raw diff output excludes flaky projects; see the HTML report for details.

Raw diff:

schema_salad (https://github.com/common-workflow-language/schema_salad)
+ schema_salad/ref_resolver.py:119:13 error[invalid-assignment] Cannot delete final attribute `normalize` on type `Self@__del__`: `Final` attributes cannot be deleted

Full report with detailed diff (timing results)

@charliermarsh charliermarsh marked this pull request as ready for review April 9, 2026 00:43
Comment on lines +2827 to +2834
if self.validate_final_attribute_deletion(
target,
object_ty,
attribute,
emit_diagnostics,
) {
return false;
}
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.

Removing this doesn't fail any tests. It's not totally clear to me what behavior is right here. If we have a __delattr__, we don't know what it does internally, so should we assume it's deleting the Final attr of the same name, or not?

Other type checkers that validate deletion of Final attrs do it even if there is a __delattr__, so maybe we should too? In that case we should add a test.

@AlexWaygood AlexWaygood removed their request for review April 9, 2026 07:18
Base automatically changed from charlie/deleter-i to main April 10, 2026 21:33
@charliermarsh charliermarsh merged commit f715215 into main Apr 10, 2026
55 checks passed
@charliermarsh charliermarsh deleted the charlie/deleter-ii branch April 10, 2026 21:39
carljm added a commit that referenced this pull request Apr 10, 2026
* main:
  Bump typing conformance suite commit to latest upstream (#24553)
  [ty] Reject deleting`Final` attributes (#24508)
  [ty] Respect property deleters in attribute deletion checks (#24500)
  [ty] stop unioning Unknown into types of un-annotated attributes (#24531)
  [ty] Fix bad diagnostic range for incorrect implicit `__init_subclass__` calls (#24541)
  [ty] Add a `SupportedPythonVersion` enum (#24412)
  [ty] Ignore unsupported editor-selected Python versions (#24498)
  [ty] Add snapshots for `__init_subclass__` diagnostics (#24539)
  [ty] Minor fix in tests (#24538)
  [ty] Allow `Final` variable assignments in `__post_init__` (#24529)
  [ty] Expand test suite for assignment errors (#24537)
  [ty] Use `map`, not `__map`, as the name of the mapping parameter in `TypedDict` `__init__` methods (#24535)
  [ty] Rework logic for synthesizing `TypedDict` methods (#24534)
  [flake8-bandit] Fix S103 false positives and negatives in mask analysis (#24424)
  [ty] mdtest.py: update dependencies (#24533)
  Rename patterns and arguments source order iterator method (#24532)
  [ty] Omit invalid keyword arguments from `TypedDict` signature (#24522)
  [ty] support super() in metaclass methods (#24483)
  [ty] Synthesize `__init__` for `TypedDict` (#24476)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants