Skip to content

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

Open
@bzoracler

Description

@bzoracler

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions