Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,17 @@ def check_setattr_method(self, typ: Type, context: Context) -> None:
if not is_subtype(typ, method_type):
self.msg.invalid_signature_for_special_method(typ, context, '__setattr__')

def check_slots_definition(self, typ: Type, context: Context) -> None:
"""Check the type of __slots__."""
str_type = self.named_type("builtins.str")
expected_type = UnionType([str_type,
self.named_generic_type("typing.Iterable", [str_type])])
self.check_subtype(typ, expected_type, context,
message_registry.INVALID_TYPE_FOR_SLOTS,
'actual type',
'expected type',
code=codes.ASSIGNMENT)

def check_match_args(self, var: Var, typ: Type, context: Context) -> None:
"""Check that __match_args__ contains literal strings"""
typ = get_proper_type(typ)
Expand Down Expand Up @@ -2281,6 +2292,9 @@ def check_assignment(self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type
else:
self.check_getattr_method(signature, lvalue, name)

if name == '__slots__':
typ = lvalue_type or self.expr_checker.accept(rvalue)
self.check_slots_definition(typ, lvalue)
if name == '__match_args__' and inferred is not None:
typ = self.expr_checker.accept(rvalue)
self.check_match_args(inferred, typ, lvalue)
Expand Down
1 change: 1 addition & 0 deletions mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def format(self, *args: object, **kwargs: object) -> "ErrorMessage":
'Incompatible types in "async with" for "__aexit__"'
)
INCOMPATIBLE_TYPES_IN_ASYNC_FOR: Final = 'Incompatible types in "async for"'
INVALID_TYPE_FOR_SLOTS: Final = 'Invalid type for "__slots__"'

ASYNC_FOR_OUTSIDE_COROUTINE: Final = '"async for" outside async function'
ASYNC_WITH_OUTSIDE_COROUTINE: Final = '"async with" outside async function'
Expand Down
1 change: 0 additions & 1 deletion mypy/typeshed/stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ class _SupportsAiter(Protocol[_T_co]):
class object:
__doc__: str | None
__dict__: dict[str, Any]
__slots__: str | Iterable[str]
__module__: str
__annotations__: dict[str, Any]
@property
Expand Down
8 changes: 6 additions & 2 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1312,13 +1312,17 @@ f(E)
g(E)

[case testInvalidSlots]
from typing import List
class A:
__slots__ = 1
class B:
__slots__ = (1, 2)
class C:
__slots__: List[int] = []
[out]
_testInvalidSlots.py:2: error: Incompatible types in assignment (expression has type "int", base class "object" defined the type as "Union[str, Iterable[str]]")
_testInvalidSlots.py:4: error: Incompatible types in assignment (expression has type "Tuple[int, int]", base class "object" defined the type as "Union[str, Iterable[str]]")
_testInvalidSlots.py:3: error: Invalid type for "__slots__" (actual type "int", expected type "Union[str, Iterable[str]]")
_testInvalidSlots.py:5: error: Invalid type for "__slots__" (actual type "Tuple[int, int]", expected type "Union[str, Iterable[str]]")
_testInvalidSlots.py:7: error: Invalid type for "__slots__" (actual type "List[int]", expected type "Union[str, Iterable[str]]")

[case testDictWithStarStarSpecialCase]
from typing import Dict
Expand Down