Skip to content

Commit ac4622f

Browse files
committed
Allow type checkers to generate an error when list[Self] instance variables are accessed
1 parent ae0d7a1 commit ac4622f

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

conformance/results/mypy/generics_self_advanced.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ Does not retain `Self` when accessing attribute through `type[Self]`.
77
"""
88
output = """
99
generics_self_advanced.py:35: error: Expression is of type "ChildB", not "Self" [assert-type]
10-
generics_self_advanced.py:38: error: Expression is of type "ChildB", not "Self" [assert-type]
11-
generics_self_advanced.py:42: error: Expression is of type "type[ChildB]", not "type[Self]" [assert-type]
12-
generics_self_advanced.py:48: error: Expression is of type "ChildB", not "Self" [assert-type]
10+
generics_self_advanced.py:41: error: Expression is of type "ChildB", not "Self" [assert-type]
11+
generics_self_advanced.py:45: error: Expression is of type "type[ChildB]", not "type[Self]" [assert-type]
12+
generics_self_advanced.py:51: error: Expression is of type "ChildB", not "Self" [assert-type]
1313
"""
1414
conformance_automated = "Fail"
1515
errors_diff = """
1616
Line 35: Unexpected errors ['generics_self_advanced.py:35: error: Expression is of type "ChildB", not "Self" [assert-type]']
17-
Line 38: Unexpected errors ['generics_self_advanced.py:38: error: Expression is of type "ChildB", not "Self" [assert-type]']
18-
Line 42: Unexpected errors ['generics_self_advanced.py:42: error: Expression is of type "type[ChildB]", not "type[Self]" [assert-type]']
19-
Line 48: Unexpected errors ['generics_self_advanced.py:48: error: Expression is of type "ChildB", not "Self" [assert-type]']
17+
Line 41: Unexpected errors ['generics_self_advanced.py:41: error: Expression is of type "ChildB", not "Self" [assert-type]']
18+
Line 45: Unexpected errors ['generics_self_advanced.py:45: error: Expression is of type "type[ChildB]", not "type[Self]" [assert-type]']
19+
Line 51: Unexpected errors ['generics_self_advanced.py:51: error: Expression is of type "ChildB", not "Self" [assert-type]']
2020
"""

conformance/results/zuban/generics_self_advanced.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ conformance_automated = "Pass"
22
errors_diff = """
33
"""
44
output = """
5-
generics_self_advanced.py:45: error: Access to generic instance variables via class is ambiguous [misc]
5+
generics_self_advanced.py:48: error: Access to generic instance variables via class is ambiguous [misc]
66
"""

conformance/tests/generics_self_advanced.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ class ChildB(ParentB):
3333

3434
def method2(self) -> None:
3535
assert_type(self, Self)
36-
assert_type(self.a, list[Self])
37-
assert_type(self.a[0], Self)
36+
# Allow type checkers to error here, since Self definitions on
37+
# non-final classes are unsound.
38+
a = self.a # E?
39+
assert_type(a, list[Self])
40+
assert_type(a[0], Self)
3841
assert_type(self.method1(), Self)
3942

4043
@classmethod

0 commit comments

Comments
 (0)