@@ -256,11 +256,34 @@ method receives an integer we return a single item. If it receives a
256256``slice ``, we return a :py:class: `~typing.Sequence ` of items.
257257
258258We can precisely encode this relationship between the argument and the
259- return type by using overloads like so:
259+ return type by using overloads like so (Python 3.12 syntax) :
260260
261261.. code-block :: python
262262
263- from typing import Sequence, TypeVar, Union, overload
263+ from collections.abc import Sequence
264+ from typing import overload
265+
266+ class MyList[T](Sequence[T]):
267+ @overload
268+ def __getitem__ (self , index : int ) -> T: ...
269+
270+ @overload
271+ def __getitem__ (self , index : slice ) -> Sequence[T]: ...
272+
273+ def __getitem__ (self , index : int | slice ) -> T | Sequence[T]:
274+ if isinstance (index, int ):
275+ # Return a T here
276+ elif isinstance (index, slice ):
277+ # Return a sequence of Ts here
278+ else :
279+ raise TypeError (... )
280+
281+ Here is the same example using the legacy syntax (Python 3.11 and earlier):
282+
283+ .. code-block :: python
284+
285+ from collections.abc import Sequence
286+ from typing import TypeVar, Union, overload
264287
265288 T = TypeVar(' T' )
266289
@@ -697,14 +720,13 @@ Restricted methods in generic classes
697720-------------------------------------
698721
699722In generic classes some methods may be allowed to be called only
700- for certain values of type arguments:
723+ for certain values of type arguments (Python 3.12 syntax) :
701724
702725.. code-block :: python
703726
704- T = TypeVar(' T' )
705-
706- class Tag (Generic[T]):
727+ class Tag[T]:
707728 item: T
729+
708730 def uppercase_item (self : Tag[str ]) -> str :
709731 return self .item.upper()
710732
@@ -714,18 +736,18 @@ for certain values of type arguments:
714736 ts.uppercase_item() # This is OK
715737
716738 This pattern also allows matching on nested types in situations where the type
717- argument is itself generic:
739+ argument is itself generic (Python 3.12 syntax) :
718740
719741.. code-block :: python
720742
721- T = TypeVar(' T' , covariant = True )
722- S = TypeVar(' S' )
743+ from collections.abc import Sequence
723744
724- class Storage (Generic [T]) :
745+ class Storage[T]:
725746 def __init__ (self , content : T) -> None :
726- self .content = content
727- def first_chunk (self : Storage[Sequence[S]]) -> S:
728- return self .content[0 ]
747+ self ._content = content
748+
749+ def first_chunk[S](self : Storage[Sequence[S]]) -> S:
750+ return self ._content[0 ]
729751
730752 page: Storage[list[str ]]
731753 page.first_chunk() # OK, type is "str"
@@ -734,13 +756,13 @@ argument is itself generic:
734756 # "first_chunk" with type "Callable[[Storage[Sequence[S]]], S]"
735757
736758 Finally, one can use overloads on self-type to express precise types of
737- some tricky methods:
759+ some tricky methods (Python 3.12 syntax) :
738760
739761.. code-block :: python
740762
741- T = TypeVar( ' T ' )
763+ from typing import overload, Callable
742764
743- class Tag (Generic [T]) :
765+ class Tag[T]:
744766 @overload
745767 def export (self : Tag[str ]) -> str : ...
746768 @overload
@@ -799,23 +821,22 @@ Precise typing of alternative constructors
799821------------------------------------------
800822
801823Some classes may define alternative constructors. If these
802- classes are generic, self-type allows giving them precise signatures:
824+ classes are generic, self-type allows giving them precise
825+ signatures (Python 3.12 syntax):
803826
804827.. code-block :: python
805828
806- T = TypeVar(' T' )
807-
808- class Base (Generic[T]):
809- Q = TypeVar(' Q' , bound = ' Base[T]' )
829+ from typing import Self
810830
831+ class Base[T]:
811832 def __init__ (self , item : T) -> None :
812833 self .item = item
813834
814835 @ classmethod
815- def make_pair (cls : Type[Q] , item : T) -> tuple[Q, Q ]:
836+ def make_pair (cls , item : T) -> tuple[Self, Self ]:
816837 return cls (item), cls (item)
817838
818- class Sub (Base[T]):
839+ class Sub[T] (Base[T]):
819840 ...
820841
821842 pair = Sub.make_pair(' yes' ) # Type is "tuple[Sub[str], Sub[str]]"
0 commit comments