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
3 changes: 3 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3751,6 +3751,9 @@ def check_assignment_to_slots(self, lvalue: Lvalue) -> None:
return

inst = get_proper_type(self.expr_checker.accept(lvalue.expr))
if isinstance(inst, TypeVarType) and inst.id.is_self():
# Unwrap self type
inst = get_proper_type(inst.upper_bound)
if not isinstance(inst, Instance):
return
if inst.type.slots is None:
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-slots.test
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,19 @@ x = X()
X.a # E: "a" in __slots__ conflicts with class variable access
x.a
[builtins fixtures/tuple.pyi]

[case testSlotsOnSelfType]
from typing_extensions import Self

class X:
__slots__ = ("foo",)
foo: int

def method1(self: Self) -> Self:
self.bar = 0 # E: Trying to assign name "bar" that is not in "__slots__" of type "__main__.X"
return self

def method2(self) -> Self:
self.bar = 0 # E: Trying to assign name "bar" that is not in "__slots__" of type "__main__.X"
return self
[builtins fixtures/tuple.pyi]