Fix missing slot false positives for finalize.atexit & TarInfo.path - #16333
Merged
Conversation
Both are declared as bare instance attributes on classes whose stub also
declares `__slots__` without them, so type checkers reject every assignment
to them even though the runtime allows it. CPython implements both as
read-write properties.
`weakref.finalize` (Lib/weakref.py) has `__slots__ = ()` and implements
`atexit` as a property with a setter; the neighbouring `alive` is
already modelled as a property in typeshed.
`tarfile.TarInfo` (Lib/tarfile.py) implements `path` as a read-write
property aliasing `name`, exactly like `linkpath` aliases `linkname`;
`linkpath` is already modelled as a property, `path` was not, and
`path` is absent from the `__slots__` tuple.
Reproducer, rejected before and accepted after this patch:
import tarfile
import weakref
class C: ...
def f(c: C, ti: tarfile.TarInfo) -> None:
weakref.finalize(c, print).atexit = False
ti.path = "x"
mypy 2.3.1 before:
error: Trying to assign name "atexit" that is not in "__slots__" of type "weakref.finalize" [misc]
error: Trying to assign name "path" that is not in "__slots__" of type "tarfile.TarInfo" [misc]
ty 0.0.77 reports the same two as `missing-slot`; pyrefly reports
`missing-attribute`. stubtest output is unchanged for both modules.
The `__slots__` entries came from the automated sweep in python#14611.
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: dd-trace-py (https://github.com/DataDog/dd-trace-py)
- ddtrace/internal/wrapping/context.py:485: error: Trying to assign name "atexit" that is not in "__slots__" of type "weakref.finalize" [misc]
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Both are declared as bare instance attributes on classes whose stub also declares
__slots__without them, so type checkers reject every assignment to them even though the runtime allows it. CPython implements both as read-write properties.weakref.finalize(Lib/weakref.py) has__slots__ = ()and implementsatexitas a property with a setter; the neighbouringaliveis already modelled as a property in typeshed.tarfile.TarInfo(Lib/tarfile.py) implementspathas a read-write property aliasingname, exactly likelinkpathaliaseslinkname;linkpathis already modelled as a property,pathwas not, andpathis absent from the__slots__tuple.Reproducer, rejected before and accepted after this patch:
mypy 2.3.1 before:
ty 0.0.77 reports the same two as
missing-slot; pyrefly reportsmissing-attribute. stubtest output is unchanged for both modules.The
__slots__entries came from the automated sweep in #14611 (part of #8832). The attribute declarations predate it, so the contradiction was introduced by adding__slots__on top of them rather than by the annotations themselves.