2Tests for basic usage of the typing.Self type.
5# Specification: https://typing.readthedocs.io/en/latest/spec/generics.html#self.
7from typing import Callable, Generic, Self, TypeVar, assert_type
13 def set_scale(self, scale: float) -> Self:
14 assert_type(self, Self)
18 def method2(self) -> Self:
19 # This should result in a type error.
[invalid-return-type] Return type does not match returned value: expected `Self@method2`, found `Shape`
22 def method3(self) -> "Shape":
26 def from_config(cls, config: dict[str, float]) -> Self:
27 assert_type(cls, type[Self])
31 def cls_method2(cls) -> Self:
32 # This should result in a type error.
[invalid-return-type] Return type does not match returned value: expected `Self@cls_method2`, found `Shape`
36 def cls_method3(cls) -> "Shape":
39 def difference(self, other: Self) -> float:
40 assert_type(other, Self)
43 def apply(self, f: Callable[[Self], None]) -> None:
51assert_type(Shape().set_scale(1.0), Shape)
52assert_type(Circle().set_scale(1.0), Circle)
54assert_type(Shape.from_config({}), Shape)
55assert_type(Circle.from_config({}), Circle)
58class Container(Generic[T]):
61 def __init__(self, value: T) -> None:
64 def set_value(self, value: T) -> Self:
65 raise NotImplementedError
67 # This should generate an error because Self isn't subscriptable.
68 def foo(self, other: Self[int]) -> None: # E
[invalid-type-form] Special form `typing.Self` expected no type parameter
72def object_with_concrete_type(
73 int_container: Container[int], str_container: Container[str]
75 assert_type(int_container.set_value(42), Container[int])
76 assert_type(str_container.set_value("hello"), Container[str])
79def object_with_generic_type(
80 container: Container[T],
83 val = container.set_value(value)
84 assert_type(val, Container[T])