@@ -203,7 +203,7 @@ Our first attempt at writing this function might look like this:
203203 def mouse_event (x1 : int ,
204204 y1 : int ,
205205 x2 : Optional[int ] = None ,
206- y2 : Optional[int ] = None ) -> ClickEvent | DragEvent:
206+ y2 : Optional[int ] = None ) -> Union[ ClickEvent, DragEvent] :
207207 if x2 is None and y2 is None :
208208 return ClickEvent(x1, y1)
209209 elif x2 is not None and y2 is not None :
@@ -247,7 +247,7 @@ to more accurately describe the function's behavior:
247247 def mouse_event (x1 : int ,
248248 y1 : int ,
249249 x2 : Optional[int ] = None ,
250- y2 : Optional[int ] = None ) -> ClickEvent | DragEvent:
250+ y2 : Optional[int ] = None ) -> Union[ ClickEvent, DragEvent] :
251251 if x2 is None and y2 is None :
252252 return ClickEvent(x1, y1)
253253 elif x2 is not None and y2 is not None :
@@ -281,7 +281,7 @@ return type by using overloads like so:
281281 @overload
282282 def __getitem__ (self , index : slice ) -> Sequence[T]: ...
283283
284- def __getitem__ (self , index : int | slice ) -> T | Sequence[T]:
284+ def __getitem__ (self , index : Union[ int , slice ] ) -> Union[T, Sequence[T] ]:
285285 if isinstance (index, int ):
286286 # Return a T here
287287 elif isinstance (index, slice ):
@@ -378,7 +378,7 @@ matching variant returns:
378378
379379.. code-block :: python
380380
381- some_list: List[int ] | List[str ]
381+ some_list: Union[ List[int ], List[str ] ]
382382
383383 # output3 is of type 'Union[float, str]'
384384 output3 = summarize(some_list)
@@ -466,7 +466,7 @@ the following unsafe overload definition:
466466 @overload
467467 def unsafe_func (x : object ) -> str : ...
468468
469- def unsafe_func (x : object ) -> int | str :
469+ def unsafe_func (x : object ) -> Union[ int , str ] :
470470 if isinstance (x, int ):
471471 return 42
472472 else :
@@ -532,8 +532,8 @@ Type checking the implementation
532532The body of an implementation is type-checked against the
533533type hints provided on the implementation. For example, in the
534534``MyList `` example up above, the code in the body is checked with
535- argument list ``index: int | slice `` and a return type of
536- ``T | Sequence[T] ``. If there are no annotations on the
535+ argument list ``index: Union[ int, slice] `` and a return type of
536+ ``Union[T, Sequence[T] ] ``. If there are no annotations on the
537537implementation, then the body is not type checked. If you want to
538538force mypy to check the body anyways, use the :option: `--check-untyped-defs <mypy --check-untyped-defs> `
539539flag (:ref: `more details here <untyped-definitions-and-calls >`).
0 commit comments