Skip to content

Commit 859d3d3

Browse files
authored
Allow errors for unsound Self definitions on non-final classes (#2221)
1 parent 418094f commit 859d3d3

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
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:45: 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 45: Unexpected errors ['generics_self_advanced.py:45: 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/results.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ <h3>Python Type System Conformance Test Results</h3>
345345
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;generics_self_advanced</th>
346346
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not infer the type of an unannotated `self` parameter to be type `Self`.</p><p>Does not retain `Self` when calling method that returns `Self`.</p><p>Does not infer the type of an unannotated `cls` parameter to be type `type[Self]`.</p><p>Does not retain `Self` when accessing attribute through `type[Self]`.</p></span></div></th>
347347
<th class="column col2 conformant">Pass</th>
348-
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Doesn't allow accessing `Self` in a classmethod</p></span></div></th>
348+
<th class="column col2 conformant">Pass</th>
349349
<th class="column col2 conformant"><div class="hover-text">Pass*<span class="tooltip-text" id="bottom"><p>Treats attributes not initialized on the class as instance-only</p></span></div></th>
350350
<th class="column col2 conformant">Pass</th>
351351
</tr>
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
conformant = "Partial"
2-
notes = """
3-
Doesn't allow accessing `Self` in a classmethod
4-
"""
5-
conformance_automated = "Fail"
1+
conformance_automated = "Pass"
62
errors_diff = """
7-
Line 43: Unexpected errors ['generics_self_advanced.py:43: error: Access to generic instance variables via class is ambiguous [misc]']
8-
Line 44: Unexpected errors ['generics_self_advanced.py:44: error: Access to generic instance variables via class is ambiguous [misc]']
93
"""
104
output = """
11-
generics_self_advanced.py:43: error: Access to generic instance variables via class is ambiguous [misc]
12-
generics_self_advanced.py:44: 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]
136
"""

conformance/tests/generics_self_advanced.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,19 @@ 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
4144
def method3(cls) -> None:
4245
assert_type(cls, type[Self])
43-
assert_type(cls.a, list[Self])
44-
assert_type(cls.a[0], Self)
46+
# Allow type checkers to error here, since Self definitions on
47+
# non-final classes are unsound.
48+
a = cls.a # E?
49+
assert_type(a, list[Self])
50+
assert_type(a[0], Self)
4551
assert_type(cls.method1(), Self)

0 commit comments

Comments
 (0)