Skip to content

Mypy complains when [] is used for __slots__ definition #10870

Open
@sobolevn

Description

@sobolevn

Bug Report

Slot definition in a form of __slots__ = [] raise an error:

ex.py:2: error: Need type annotation for "__slots__" (hint: "__slots__: List[<type>] = ...")

To Reproduce

Run mypy on this code:

class A:
    __slots__ = []

Expected Behavior
I expect it to have the same effect as:

class A:
    __slots__ = ()

and to produce no errors.
Moreover, mypy already knows that __slots__ should contain str items only as shown here:

class A:
    __slots__ = [1, 2]

Outputs:

ex.py:2: error: List item 0 has incompatible type "int"; expected "str"
ex.py:2: error: List item 1 has incompatible type "int"; expected "str"

Refs Textualize/rich#1364

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-inferenceWhen to infer types or require explicit annotationstopic-runtime-semanticsmypy doesn't model runtime semantics correctly

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions