1111--------------
1212
1313This module supports type hints as specified by :pep: `484 `. The most
14- fundamental support consists of the type :class : `Any `, :class : `Union `,
15- :class : `Tuple `, :class : `Callable `, :class: `TypeVar `, and
14+ fundamental support consists of the type :data : `Any `, :data : `Union `,
15+ :data : `Tuple `, :data : `Callable `, :class: `TypeVar `, and
1616:class: `Generic `. For full specification please see :pep: `484 `. For
1717a simplified introduction to type hints see :pep: `483 `.
1818
@@ -266,8 +266,8 @@ When inheriting from generic classes, some type variables could be fixed::
266266
267267In this case ``MyDict `` has a single parameter, ``T ``.
268268
269- Subclassing a generic class without specifying type parameters assumes
270- :class : `Any ` for each position. In the following example, ``MyIterable `` is
269+ Using a generic class without specifying type parameters assumes
270+ :data : `Any ` for each position. In the following example, ``MyIterable `` is
271271not generic but implicitly inherits from ``Iterable[Any] ``::
272272
273273 from typing import Iterable
@@ -277,18 +277,20 @@ not generic but implicitly inherits from ``Iterable[Any]``::
277277The metaclass used by :class: `Generic ` is a subclass of :class: `abc.ABCMeta `.
278278A generic class can be an ABC by including abstract methods or properties,
279279and generic classes can also have ABCs as base classes without a metaclass
280- conflict. Generic metaclasses are not supported.
280+ conflict. Generic metaclasses are not supported. The outcome of parameterizing
281+ generics is cached, and most types in the typing module are hashable and
282+ comparable for equality.
281283
282284
283- The :class : `Any ` type
285+ The :data : `Any ` type
284286---------------------
285287
286- A special kind of type is :class : `Any `. A static type checker will treat
287- every type as being compatible with :class : `Any ` and :class : `Any ` as being
288+ A special kind of type is :data : `Any `. A static type checker will treat
289+ every type as being compatible with :data : `Any ` and :data : `Any ` as being
288290compatible with every type.
289291
290292This means that it is possible to perform any operation or method call on a
291- value of type on :class : `Any ` and assign it to any variable::
293+ value of type on :data : `Any ` and assign it to any variable::
292294
293295 from typing import Any
294296
@@ -306,13 +308,13 @@ value of type on :class:`Any` and assign it to any variable::
306308 ...
307309
308310Notice that no typechecking is performed when assigning a value of type
309- :class : `Any ` to a more precise type. For example, the static type checker did
311+ :data : `Any ` to a more precise type. For example, the static type checker did
310312not report an error when assigning ``a `` to ``s `` even though ``s `` was
311313declared to be of type :class: `str ` and receives an :class: `int ` value at
312314runtime!
313315
314316Furthermore, all functions without a return type or parameter types will
315- implicitly default to using :class : `Any `::
317+ implicitly default to using :data : `Any `::
316318
317319 def legacy_parser(text):
318320 ...
@@ -324,12 +326,12 @@ implicitly default to using :class:`Any`::
324326 ...
325327 return data
326328
327- This behavior allows :class : `Any ` to be used as an *escape hatch * when you
329+ This behavior allows :data : `Any ` to be used as an *escape hatch * when you
328330need to mix dynamically and statically typed code.
329331
330- Contrast the behavior of :class : `Any ` with the behavior of :class: `object `.
331- Similar to :class : `Any `, every type is a subtype of :class: `object `. However,
332- unlike :class : `Any `, the reverse is not true: :class: `object ` is *not * a
332+ Contrast the behavior of :data : `Any ` with the behavior of :class: `object `.
333+ Similar to :data : `Any `, every type is a subtype of :class: `object `. However,
334+ unlike :data : `Any `, the reverse is not true: :class: `object ` is *not * a
333335subtype of every other type.
334336
335337That means when the type of a value is :class: `object `, a type checker will
@@ -355,22 +357,13 @@ it as a return value) of a more specialized type is a type error. For example::
355357 hash_b("foo")
356358
357359Use :class: `object ` to indicate that a value could be any type in a typesafe
358- manner. Use :class : `Any ` to indicate that a value is dynamically typed.
360+ manner. Use :data : `Any ` to indicate that a value is dynamically typed.
359361
360362Classes, functions, and decorators
361363----------------------------------
362364
363365The module defines the following classes, functions and decorators:
364366
365- .. class :: Any
366-
367- Special type indicating an unconstrained type.
368-
369- * Any object is an instance of :class: `Any `.
370- * Any class is a subclass of :class: `Any `.
371- * As a special case, :class: `Any ` and :class: `object ` are subclasses of
372- each other.
373-
374367.. class :: TypeVar
375368
376369 Type variable.
@@ -409,79 +402,6 @@ The module defines the following classes, functions and decorators:
409402 for the type variable must be a subclass of the boundary type,
410403 see :pep: `484 `.
411404
412- .. class :: Union
413-
414- Union type; ``Union[X, Y] `` means either X or Y.
415-
416- To define a union, use e.g. ``Union[int, str] ``. Details:
417-
418- * The arguments must be types and there must be at least one.
419-
420- * Unions of unions are flattened, e.g.::
421-
422- Union[Union[int, str], float] == Union[int, str, float]
423-
424- * Unions of a single argument vanish, e.g.::
425-
426- Union[int] == int # The constructor actually returns int
427-
428- * Redundant arguments are skipped, e.g.::
429-
430- Union[int, str, int] == Union[int, str]
431-
432- * When comparing unions, the argument order is ignored, e.g.::
433-
434- Union[int, str] == Union[str, int]
435-
436- * If :class: `Any ` is present it is the sole survivor, e.g.::
437-
438- Union[int, Any] == Any
439-
440- * You cannot subclass or instantiate a union.
441-
442- * You cannot write ``Union[X][Y] ``.
443-
444- * You can use ``Optional[X] `` as a shorthand for ``Union[X, None] ``.
445-
446- .. class :: Optional
447-
448- Optional type.
449-
450- ``Optional[X] `` is equivalent to ``Union[X, None] ``.
451-
452- Note that this is not the same concept as an optional argument,
453- which is one that has a default. An optional argument with a
454- default needn't use the ``Optional `` qualifier on its type
455- annotation (although it is inferred if the default is ``None ``).
456- A mandatory argument may still have an ``Optional `` type if an
457- explicit value of ``None `` is allowed.
458-
459- .. class :: Tuple
460-
461- Tuple type; ``Tuple[X, Y] `` is the type of a tuple of two items
462- with the first item of type X and the second of type Y.
463-
464- Example: ``Tuple[T1, T2] `` is a tuple of two elements corresponding
465- to type variables T1 and T2. ``Tuple[int, float, str] `` is a tuple
466- of an int, a float and a string.
467-
468- To specify a variable-length tuple of homogeneous type,
469- use literal ellipsis, e.g. ``Tuple[int, ...] ``.
470-
471- .. class :: Callable
472-
473- Callable type; ``Callable[[int], str] `` is a function of (int) -> str.
474-
475- The subscription syntax must always be used with exactly two
476- values: the argument list and the return type. The argument list
477- must be a list of types; the return type must be a single type.
478-
479- There is no syntax to indicate optional or keyword arguments,
480- such function types are rarely used as callback types.
481- ``Callable[..., ReturnType] `` could be used to type hint a callable
482- taking any number of arguments and returning ``ReturnType ``.
483- A plain :class: `Callable ` is equivalent to ``Callable[..., Any] ``.
484-
485405.. class :: Generic
486406
487407 Abstract base class for generic types.
@@ -506,7 +426,7 @@ The module defines the following classes, functions and decorators:
506426 except KeyError:
507427 return default
508428
509- .. class :: Type
429+ .. class :: Type(Generic[CT_co])
510430
511431 A variable annotated with ``C `` may accept a value of type ``C ``. In
512432 contrast, a variable annotated with ``Type[C] `` may accept values that are
@@ -538,7 +458,7 @@ The module defines the following classes, functions and decorators:
538458 :pep: `484 `.
539459
540460 The only legal parameters for :class: `Type ` are classes, unions of classes, and
541- :class : `Any `. For example::
461+ :data : `Any `. For example::
542462
543463 def new_non_team_user(user_class: Type[Union[BaseUser, ProUser]]): ...
544464
@@ -713,21 +633,6 @@ The module defines the following classes, functions and decorators:
713633 yield start
714634 start += 1
715635
716- .. class :: AnyStr
717-
718- ``AnyStr `` is a type variable defined as
719- ``AnyStr = TypeVar('AnyStr', str, bytes) ``.
720-
721- It is meant to be used for functions that may accept any kind of string
722- without allowing different kinds of strings to mix. For example::
723-
724- def concat(a: AnyStr, b: AnyStr) -> AnyStr:
725- return a + b
726-
727- concat(u"foo", u"bar") # Ok, output has type 'unicode'
728- concat(b"foo", b"bar") # Ok, output has type 'bytes'
729- concat(u"foo", b"bar") # Error, cannot mix unicode and bytes
730-
731636.. class :: Text
732637
733638 ``Text `` is an alias for ``str ``. It is provided to supply a forward
@@ -848,6 +753,89 @@ The module defines the following classes, functions and decorators:
848753 This wraps the decorator with something that wraps the decorated
849754 function in :func: `no_type_check `.
850755
756+ .. data :: Any
757+
758+ Special type indicating an unconstrained type.
759+
760+ * Every type is compatible with :data: `Any `.
761+ * :data: `Any ` is compatible with every type.
762+
763+ .. data :: Union
764+
765+ Union type; ``Union[X, Y] `` means either X or Y.
766+
767+ To define a union, use e.g. ``Union[int, str] ``. Details:
768+
769+ * The arguments must be types and there must be at least one.
770+
771+ * Unions of unions are flattened, e.g.::
772+
773+ Union[Union[int, str], float] == Union[int, str, float]
774+
775+ * Unions of a single argument vanish, e.g.::
776+
777+ Union[int] == int # The constructor actually returns int
778+
779+ * Redundant arguments are skipped, e.g.::
780+
781+ Union[int, str, int] == Union[int, str]
782+
783+ * When comparing unions, the argument order is ignored, e.g.::
784+
785+ Union[int, str] == Union[str, int]
786+
787+ * When a class and its subclass are present, the former is skipped, e.g.::
788+
789+ Union[int, object] == object
790+
791+ * You cannot subclass or instantiate a union.
792+
793+ * You cannot write ``Union[X][Y] ``.
794+
795+ * You can use ``Optional[X] `` as a shorthand for ``Union[X, None] ``.
796+
797+ .. data :: Optional
798+
799+ Optional type.
800+
801+ ``Optional[X] `` is equivalent to ``Union[X, None] ``.
802+
803+ Note that this is not the same concept as an optional argument,
804+ which is one that has a default. An optional argument with a
805+ default needn't use the ``Optional `` qualifier on its type
806+ annotation (although it is inferred if the default is ``None ``).
807+ A mandatory argument may still have an ``Optional `` type if an
808+ explicit value of ``None `` is allowed.
809+
810+ .. data :: Tuple
811+
812+ Tuple type; ``Tuple[X, Y] `` is the type of a tuple of two items
813+ with the first item of type X and the second of type Y.
814+
815+ Example: ``Tuple[T1, T2] `` is a tuple of two elements corresponding
816+ to type variables T1 and T2. ``Tuple[int, float, str] `` is a tuple
817+ of an int, a float and a string.
818+
819+ To specify a variable-length tuple of homogeneous type,
820+ use literal ellipsis, e.g. ``Tuple[int, ...] ``. A plain :data: `Tuple `
821+ is equivalent to ``Tuple[Any, ...] ``, and in turn to :data: `tuple `.
822+
823+ .. data :: Callable
824+
825+ Callable type; ``Callable[[int], str] `` is a function of (int) -> str.
826+
827+ The subscription syntax must always be used with exactly two
828+ values: the argument list and the return type. The argument list
829+ must be a list of types; the return type must be a single type.
830+
831+ There is no syntax to indicate optional or keyword arguments;
832+ such function types are rarely used as callback types.
833+ ``Callable[..., ReturnType] `` (literal ellipsis) can be used to
834+ type hint a callable taking any number of arguments and returning
835+ ``ReturnType ``. A plain :data: `Callable ` is equivalent to
836+ ``Callable[..., Any] ``, and in turn to
837+ :class: `collections.abc.Callable `.
838+
851839.. data :: ClassVar
852840
853841 Special type construct to mark class variables.
@@ -860,19 +848,34 @@ The module defines the following classes, functions and decorators:
860848 stats: ClassVar[Dict[str, int]] = {} # class variable
861849 damage: int = 10 # instance variable
862850
863- ClassVar accepts only types and cannot be further subscribed.
851+ :data: ` ClassVar ` accepts only types and cannot be further subscribed.
864852
865- ClassVar is not a class itself, and should not
866- be used with isinstance() or issubclass(). Note that ClassVar
867- does not change Python runtime behavior, it can be used by
868- 3rd party type checkers, so that the following code will
869- flagged as an error by those::
853+ :data: ` ClassVar ` is not a class itself, and should not
854+ be used with :func: ` isinstance ` or :func: ` issubclass `.
855+ Note that :data: ` ClassVar ` does not change Python runtime behavior;
856+ it can be used by 3rd party type checkers, so that the following
857+ code might flagged as an error by those::
870858
871859 enterprise_d = Starship(3000)
872860 enterprise_d.stats = {} # Error, setting class variable on instance
873861 Starship.stats = {} # This is OK
874862
875- .. versionadded :: 3.6
863+ .. versionadded :: 3.5.3
864+
865+ .. data :: AnyStr
866+
867+ ``AnyStr `` is a type variable defined as
868+ ``AnyStr = TypeVar('AnyStr', str, bytes) ``.
869+
870+ It is meant to be used for functions that may accept any kind of string
871+ without allowing different kinds of strings to mix. For example::
872+
873+ def concat(a: AnyStr, b: AnyStr) -> AnyStr:
874+ return a + b
875+
876+ concat(u"foo", u"bar") # Ok, output has type 'unicode'
877+ concat(b"foo", b"bar") # Ok, output has type 'bytes'
878+ concat(u"foo", b"bar") # Error, cannot mix unicode and bytes
876879
877880.. data :: TYPE_CHECKING
878881
0 commit comments