23
23
24
24
--------------
25
25
26
- This module provides runtime support for type hints. For the original
27
- specification of the typing system, see :pep: `484 `. For a simplified
28
- introduction to type hints, see :pep: `483 `.
26
+ This module provides runtime support for type hints.
29
27
28
+ Consider the function below::
30
29
31
- The function below takes and returns a string and is annotated as follows::
30
+ def moon_weight(earth_weight: float) -> str:
31
+ return f'On the moon, you would weigh {earth_weight * 0.166} kilograms.'
32
32
33
- def greeting(name: str) -> str:
34
- return 'Hello ' + name
33
+ The function ``moon_weight `` takes an argument expected to be an instance of :class: `float `,
34
+ as indicated by the *type hint * ``earth_weight: float ``. The function is expected to
35
+ return an instance of :class: `str `, as indicated by the ``-> str `` hint.
35
36
36
- In the function `` greeting ``, the argument `` name `` is expected to be of type
37
- :class: ` str ` and the return type :class: ` str `. Subtypes are accepted as
38
- arguments .
37
+ While type hints can be simple classes like :class: ` float ` or :class: ` str `,
38
+ they can also be more complex. The :mod: ` typing ` module provides a vocabulary of
39
+ more advanced type hints .
39
40
40
41
New features are frequently added to the ``typing `` module.
41
42
The `typing_extensions <https://pypi.org/project/typing-extensions/ >`_ package
42
43
provides backports of these new features to older versions of Python.
43
44
44
- For a summary of deprecated features and a deprecation timeline, please see
45
- `Deprecation Timeline of Major Features `_.
46
-
47
45
.. seealso ::
48
46
49
47
`"Typing cheat sheet" <https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html >`_
@@ -61,67 +59,11 @@ For a summary of deprecated features and a deprecation timeline, please see
61
59
62
60
.. _relevant-peps :
63
61
64
- Relevant PEPs
65
- =============
66
-
67
- Since the initial introduction of type hints in :pep: `484 ` and :pep: `483 `, a
68
- number of PEPs have modified and enhanced Python's framework for type
69
- annotations:
70
-
71
- .. raw :: html
72
-
73
- <details >
74
- <summary ><a style =" cursor :pointer ;" >The full list of PEPs</a ></summary >
75
-
76
- * :pep: `526 `: Syntax for Variable Annotations
77
- *Introducing * syntax for annotating variables outside of function
78
- definitions, and :data: `ClassVar `
79
- * :pep: `544 `: Protocols: Structural subtyping (static duck typing)
80
- *Introducing * :class: `Protocol ` and the
81
- :func: `@runtime_checkable<runtime_checkable> ` decorator
82
- * :pep: `585 `: Type Hinting Generics In Standard Collections
83
- *Introducing * :class: `types.GenericAlias ` and the ability to use standard
84
- library classes as :ref: `generic types<types-genericalias> `
85
- * :pep: `586 `: Literal Types
86
- *Introducing * :data: `Literal `
87
- * :pep: `589 `: TypedDict: Type Hints for Dictionaries with a Fixed Set of Keys
88
- *Introducing * :class: `TypedDict `
89
- * :pep: `591 `: Adding a final qualifier to typing
90
- *Introducing * :data: `Final ` and the :func: `@final<final> ` decorator
91
- * :pep: `593 `: Flexible function and variable annotations
92
- *Introducing * :data: `Annotated `
93
- * :pep: `604 `: Allow writing union types as ``X | Y ``
94
- *Introducing * :data: `types.UnionType ` and the ability to use
95
- the binary-or operator ``| `` to signify a
96
- :ref: `union of types<types-union> `
97
- * :pep: `612 `: Parameter Specification Variables
98
- *Introducing * :class: `ParamSpec ` and :data: `Concatenate `
99
- * :pep: `613 `: Explicit Type Aliases
100
- *Introducing * :data: `TypeAlias `
101
- * :pep: `646 `: Variadic Generics
102
- *Introducing * :data: `TypeVarTuple `
103
- * :pep: `647 `: User-Defined Type Guards
104
- *Introducing * :data: `TypeGuard `
105
- * :pep: `655 `: Marking individual TypedDict items as required or potentially missing
106
- *Introducing * :data: `Required ` and :data: `NotRequired `
107
- * :pep: `673 `: Self type
108
- *Introducing * :data: `Self `
109
- * :pep: `675 `: Arbitrary Literal String Type
110
- *Introducing * :data: `LiteralString `
111
- * :pep: `681 `: Data Class Transforms
112
- *Introducing * the :func: `@dataclass_transform<dataclass_transform> ` decorator
113
- * :pep: `692 `: Using ``TypedDict `` for more precise ``**kwargs `` typing
114
- *Introducing * a new way of typing ``**kwargs `` with :data: `Unpack ` and
115
- :data: `TypedDict `
116
- * :pep: `695 `: Type Parameter Syntax
117
- *Introducing * builtin syntax for creating generic functions, classes, and type aliases.
118
- * :pep: `698 `: Adding an override decorator to typing
119
- *Introducing * the :func: `@override<override> ` decorator
120
-
121
- .. raw :: html
122
-
123
- </details >
124
- <br >
62
+ Specification for the Python Type System
63
+ ========================================
64
+
65
+ The canonical, up-to-date specification of the Python type system can be
66
+ found at `"Specification for the Python type system" <https://typing.readthedocs.io/en/latest/spec/index.html >`_.
125
67
126
68
.. _type-aliases :
127
69
0 commit comments