mypy
mypy copied to clipboard
Mypy complains when `[]` is used for `__slots__` definition
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 https://github.com/willmcgugan/rich/pull/1364
I would love to fix this. PR is incomming! 👍
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?
#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 https://github.com/python/typeshed/pull/6800?
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. 👍🏻