Instance variables typed as `Callable` on the class body don't get assignment warnings when missing in `__slots__`
Open
Nobody has claimed this yet.
bug
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report, To Reproduce, & Actual Behaviour
See mypy Playground:
from collections.abc import Callable
class A:
__slots__ = ()
a: int
b: Callable[[int], str]
def __init__(self, /) -> None:
self.a = 1 # E: Trying to assign name "a" that is not in "__slots__" of type "__main__.A" [misc]
self.b = lambda _: str(_) # No warning
Expected Behavior
from collections.abc import Callable
class A:
__slots__ = ()
a: int
b: Callable[[int], str]
def __init__(self, /) -> None:
self.a = 1 # E: Trying to assign name "a" that is not in "__slots__" of type "__main__.A" [misc]
self.b = lambda _: str(_) # E: Trying to assign name "b" that is not in "__slots__" of type "__main__.A" [misc]
Your Environment
- Mypy version used: (Occurs as far back as 1.0.0)
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.9, 3.12
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the example in the linked mypy Playground and compare the existing diagnostic for self.a with the missing diagnostic for self.b. Trace the __slots__ assignment-checking entry point and add regression coverage so the Callable-typed assignment receives the expected [misc] warning.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100