Skip to content

Commit 3be2743

Browse files
committed
Remove documentation changes
1 parent 584abd2 commit 3be2743

File tree

8 files changed

+24
-28
lines changed

8 files changed

+24
-28
lines changed

docs/source/cheat_sheet.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Functions
103103
i += 1
104104
105105
# There's an alternative syntax for functions with many arguments
106-
def send_email(address, # type: str | List[str]
106+
def send_email(address, # type: Union[str, List[str]]
107107
sender, # type: str
108108
cc, # type: Optional[List[str]]
109109
bcc, # type: Optional[List[str]]
@@ -126,8 +126,7 @@ When you're puzzled or when things are complicated
126126
reveal_type(1) # -> Revealed type is 'builtins.int'
127127
128128
# Use Union when something could be one of a few types
129-
# Python 3.9+ (or List[Union[int, str]])
130-
x = [3, 5, "test", "fun"] # type: List[int | str]
129+
x = [3, 5, "test", "fun"] # type: List[Union[int, str]]
131130
132131
# Use Any if you don't know the type of something or it's too
133132
# dynamic to write a type for

docs/source/cheat_sheet_py3.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ When you're puzzled or when things are complicated
143143
reveal_type(1) # -> Revealed type is 'builtins.int'
144144
145145
# Use Union when something could be one of a few types
146-
# Python 3.9+ (or List[Union[int,str]])
147-
x: List[int | str]] = [3, 5, "test", "fun"]
146+
x: List[Union[int, str]] = [3, 5, "test", "fun"]
148147
149148
# Use Any if you don't know the type of something or it's too
150149
# dynamic to write a type for

docs/source/generics.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,7 @@ will reject this function:
443443

444444
.. code-block:: python
445445
446-
def union_concat(x: str | bytes, y: str | bytes) -> str | bytes:
447-
# or def union_concat(x: Union[str, bytes], y: Union[str, bytes]) -> Union[str, bytes]:
446+
def union_concat(x: Union[str, bytes], y: Union[str, bytes]) -> Union[str, bytes]:
448447
return x + y # Error: can't concatenate str and bytes
449448
450449
Another interesting special case is calling ``concat()`` with a

docs/source/getting_started.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,14 @@ ints or strings, but no other types. You can express this using the :py:data:`~t
211211
212212
from typing import Union
213213
214-
def normalize_id(user_id: int | str) -> str:
215-
# or def normalize_id(user_id: Union[int, str]) -> str:
214+
def normalize_id(user_id: Union[int, str]) -> str:
216215
if isinstance(user_id, int):
217216
return 'user-{}'.format(100000 + user_id)
218217
else:
219218
return user_id
220219
221220
Similarly, suppose that you want the function to accept only strings or ``None``. You can
222-
again use :py:data:`~typing.Union` and use ``str | None`` (or ``Union[str, None]``) -- or alternatively, use the type
221+
again use :py:data:`~typing.Union` and use ``Union[str, None]`` -- or alternatively, use the type
223222
``Optional[str]``. These two types are identical and interchangeable: ``Optional[str]``
224223
is just a shorthand or *alias* for ``Union[str, None]``. It exists mostly as a convenience
225224
to help function signatures look a little cleaner:

docs/source/kinds_of_types.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ Python functions often accept values of two or more different
208208
types. You can use :ref:`overloading <function-overloading>` to
209209
represent this, but union types are often more convenient.
210210

211-
Use the ``T1|...|Tn`` (or ``Union[T1, ..., Tn]``) type constructor to construct a union
212-
type. For example, if an argument has type ``int|str`` (or ``Union[int, str]``), both
211+
Use the ``Union[T1, ..., Tn]`` type constructor to construct a union
212+
type. For example, if an argument has type ``Union[int, str]``, both
213213
integers and strings are valid argument values.
214214

215215
You can use an :py:func:`isinstance` check to narrow down a union type to a
@@ -219,7 +219,7 @@ more specific type:
219219
220220
from typing import Union
221221
222-
def f(x: int | str) -> None:
222+
def f(x: Union[int, str]) -> None:
223223
x + 1 # Error: str + int is not valid
224224
if isinstance(x, int):
225225
# Here type of x is int.
@@ -248,7 +248,7 @@ Optional types and the None type
248248

249249
You can use the :py:data:`~typing.Optional` type modifier to define a type variant
250250
that allows ``None``, such as ``Optional[int]`` (``Optional[X]`` is
251-
the preferred shorthand for ``X | None`` or ``Union[X, None]``):
251+
the preferred shorthand for ``Union[X, None]``):
252252

253253
.. code-block:: python
254254
@@ -560,15 +560,15 @@ In certain situations, type names may end up being long and painful to type:
560560

561561
.. code-block:: python
562562
563-
def f() -> List[Dict[Tuple[int, str], Set[int]]] | Tuple[str, List[str]]:
563+
def f() -> Union[List[Dict[Tuple[int, str], Set[int]]], Tuple[str, List[str]]]:
564564
...
565565
566566
When cases like this arise, you can define a type alias by simply
567567
assigning the type to a variable:
568568

569569
.. code-block:: python
570570
571-
AliasType = List[Dict[Tuple[int, str], Set[int]]] | Tuple[str, List[str]]
571+
AliasType = Union[List[Dict[Tuple[int, str], Set[int]]], Tuple[str, List[str]]]
572572
573573
# Now we can use AliasType in place of the full name:
574574

docs/source/literal_types.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ precise type signature for this function using ``Literal[...]`` and overloads:
3030
# provides a regular bool:
3131
3232
@overload
33-
def fetch_data(raw: bool) -> bytes | str: ...
33+
def fetch_data(raw: bool) -> Union[bytes, str]: ...
3434
35-
def fetch_data(raw: bool) -> bytes | str:
35+
def fetch_data(raw: bool) -> Union[bytes, str]:
3636
# Implementation is omitted
3737
...
3838

docs/source/more_types.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
532532
The body of an implementation is type-checked against the
533533
type 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
537537
implementation, then the body is not type checked. If you want to
538538
force mypy to check the body anyways, use the :option:`--check-untyped-defs <mypy --check-untyped-defs>`
539539
flag (:ref:`more details here <untyped-definitions-and-calls>`).

docs/source/type_inference_and_annotations.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ variable type annotation:
3030
3131
from typing import Union
3232
33-
x: int | str = 1
33+
x: Union[int, str] = 1
3434
3535
Without the type annotation, the type of ``x`` would be just ``int``. We
3636
use an annotation to give it a more general type ``Union[int, str]`` (this
@@ -42,15 +42,15 @@ type:
4242

4343
.. code-block:: python
4444
45-
x: int | str = 1.1 # Error!
45+
x: Union[int, str] = 1.1 # Error!
4646
4747
The variable annotation syntax is available starting from Python 3.6.
4848
In earlier Python versions, you can use a special comment after an
4949
assignment statement to declare the type of a variable:
5050

5151
.. code-block:: python
5252
53-
x = 1 # type: int | str
53+
x = 1 # type: Union[int, str]
5454
5555
We'll use both syntax variants in examples. The syntax variants are
5656
mostly interchangeable, but the variable annotation syntax allows

0 commit comments

Comments
 (0)