Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
sobolevn opened this issue Jul 25, 2021 · 4 comments
Open

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

sobolevn opened this issue Jul 25, 2021 · 4 comments
Labels
bug mypy got something wrong topic-inference When to infer types or require explicit annotations topic-runtime-semantics mypy doesn't model runtime semantics correctly

Comments

@sobolevn
Copy link
Member

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

@sobolevn sobolevn added the bug mypy got something wrong label Jul 25, 2021
@sobolevn
Copy link
Member Author

I would love to fix this. PR is incomming! 👍

@sobolevn
Copy link
Member Author

sobolevn commented Jul 25, 2021

Ok, we can generalize this problem to this case:

from typing import List

class Super:
    a: List[int]

class Child(Super):
    a = []

Right now it reports:

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

The question is: should partial type be resolved in this general case? If yes, then I can modify the type checker to do it.
If not, should __slots__ be the special case? If yes, what other props should be special cased?

@AlexWaygood AlexWaygood added topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-inference When to infer types or require explicit annotations labels Mar 30, 2022
bkeryan added a commit to ni/nidaqmx-python that referenced this issue Jun 5, 2023
Work around python/mypy#10870 - Mypy complains
when [] is used for __slots__ definition

generated\nidaqmx\_base_interpreter.py:8: error: Need type annotation for "__slots__" (hint: "__slots__: List[<type>] = ...")  [var-annotated]

Signed-off-by: Brad Keryan <brad.keryan@ni.com>
bkeryan added a commit to ni/nidaqmx-python that referenced this issue Jun 6, 2023
Work around python/mypy#10870 - Mypy complains
when [] is used for __slots__ definition

generated\nidaqmx\_base_interpreter.py:8: error: Need type annotation for "__slots__" (hint: "__slots__: List[<type>] = ...")  [var-annotated]

Signed-off-by: Brad Keryan <brad.keryan@ni.com>
@smheidrich
Copy link

#13494 which, AFAICT, should have fixed the generalized case mentioned by @sobolevn has been merged by now, but this is still broken for __slots__ specifically, I guess because __slots__ was removed from object again in python/typeshed#6800?

@herebebeasties
Copy link

This is breaking default protobuf generated code type checking which is a shame. (See mentioned issues above.) Would be lovely to get this sorted out. 👍🏻

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug mypy got something wrong topic-inference When to infer types or require explicit annotations topic-runtime-semantics mypy doesn't model runtime semantics correctly
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants