← Back to index
generics_self_advanced.py
TP: 0
FP: 0
FN: 0
Optional: 0 / 2
2Tests for advanced or special-case usage of the typing.Self type.
5from typing import assert_type, Self
9 # Test for property that returns Self.
11 def prop1(self) -> Self:
12 raise NotImplementedError
18assert_type(ParentA().prop1, ParentA)
19assert_type(ChildA().prop1, ChildA)
22# Test for a child that accesses an attribute within a parent
23# whose type is annotated using Self.
28 def method1(cls) -> Self:
29 raise NotImplementedError
34 def method2(self) -> None:
35 assert_type(self, Self)
36 # Allow type checkers to error here, since Self definitions on
37 # non-final classes are unsound.
39 assert_type(a, list[Self])
40 assert_type(a[0], Self)
41 assert_type(self.method1(), Self)
44 def method3(cls) -> None:
45 assert_type(cls, type[Self])
46 # Allow type checkers to error here, since Self definitions on
47 # non-final classes are unsound.
49 assert_type(a, list[Self])
50 assert_type(a[0], Self)
51 assert_type(cls.method1(), Self)