python / python/mypy

Instance variables typed as `Callable` on the class body don't get assignment warnings when missing in `__slots__`

Open
#19,187 3 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.