Glossary¶
This section defines a few terms that may be used elsewhere in the specification.
- annotation expression¶
An expression that is valid to use within an annotation. This is usually a type expression, sometimes with additional type qualifiers. See “Type and annotation expression” for details.
- assignable¶
If a type
Bis “assignable to” a typeA, a type checker should not error on the assignmentx: A = b, wherebis some expression whose type isB. Similarly for function calls and returns:f(b)wheredef f(x: A): ...andreturn binsidedef f(...) -> A: ...are both valid (not type errors) if and only ifBis assignable toA. In this caseAis “assignable from”B. For fully static types, “assignable to” is equivalent to “subtype of” and “assignable from” is equivalent to “supertype of”. For gradual types, a typeBis assignable to a typeAif there exist fully static materializationsA'andB'ofAandB, respectively, such thatB'is a subtype ofA'. See Type system concepts.- closed¶
A TypedDict type is closed if it may not contain any additional items beyond those specified in the TypedDict definition. A closed TypedDict can be created using the
closed=Trueargument totyping.TypedDict(), or equivalently by settingextra_items=Never. Compare extra items and open.- consistent¶
Two fully static types are “consistent with” each other if they are equivalent. Two gradual types are “consistent with” each other if they could materialize to the same type. See Type system concepts. If two types are consistent, they are both assignable to and from each other.
- consistent subtype¶
“Consistent subtype” is synonymous with “assignable to” (and “consistent supertype” is synonymous with “assignable from”). See Type system concepts.
- distribution¶
The packaged file which is used to publish and distribute a release. (PEP 426)
- equivalent¶
Two fully static types
AandBare equivalent ifAis a subtype ofBandBis a subtype ofA. This implies thatAandBrepresent the same set of possible runtime objects. Two gradual typesAandBare equivalent if all materializations ofAare also materializations ofB, and all materializations ofBare also materializations ofA.- extra items¶
A TypedDict type with extra items may contain arbitrary additional key-value pairs beyond those specified in the TypedDict definition, but those values must be of the type specified by the
extra_items=argument to the definition. A TypedDict with extra items can be created using theextra_items=argument totyping.TypedDict(). Extra items may or may not be read-only. Compare closed and open.- fully static type¶
A type is “fully static” if it does not contain any gradual form. A fully static type represents a set of possible runtime values. Fully static types participate in the subtype relation. See Type system concepts.
- gradual form¶
A gradual form is an element of a type expression which makes the type it is part of not a fully static type, but rather a representation of a set of possible static types. See Type system concepts. The primary gradual form is Any. The ellipsis (
...) is a gradual form in some, but not all, contexts. It is a gradual form when used in a Callable type, and when used intuple[Any, ...](but not in other tuple types).- gradual type¶
All types in the Python type system are “gradual”. A gradual type may be a fully static type, or it may be Any, or a type that contains
Anyor another gradual form. A gradual type does not necessarily represent a single set of possible runtime values; instead it can represent a set of possible static types (a set of possible sets of possible runtime values). Gradual types which are not fully static do not participate in the subtype relation, but they do participate in consistency and assignability. They can be materialized to a more static, or fully static, type. See Type system concepts.- inhabit¶
A value is said to inhabit a type if it is a member of the set of values represented by that type. For example, the value
42inhabits the typeint, and the value"hello"inhabits the typestr.- inline¶
Inline type annotations are annotations that are included in the runtime code using PEP 526 and PEP 3107 syntax (the filename ends in
.py).- item¶
In the context of a TypedDict, an item consists of a name (the dictionary key) and a type (representing the type that values corresponding to the key must have). Items may be required or non-required, and may be read-only or writable.
- materialize¶
A gradual type can be materialized to a more static type (possibly a fully static type) by replacing Any with any other type, or by replacing the … in a Callable type with a list of types, or by replacing
tuple[Any, ...]with a specific-length tuple type. This materialization relation is key to defining assignability for gradual types. See Type system concepts.- module¶
A file containing Python runtime code or stubbed type information.
- narrow¶
A fully static type
Bis narrower than a fully static typeAifBis a subtype ofAandBis not equivalent toA. This means thatBrepresents a proper subset of the possible objects represented byA. “Type narrowing” is when a type checker infers that a name or expression must have a narrower type at some locations in control flow, due to an assignment or a runtime check of its value.- nominal¶
A nominal type (e.g. a class name) represents the set of values whose
__class__is that type, or any of its subclasses, transitively. In contrast, see structural types.- non-required¶
If an item in a TypedDict is non-required, it may or may not be present on an object of that TypedDict type, but if it is present it must be of the type specified by the TypedDict definition. Items can be marked as non-required using the
typing.NotRequiredqualifier or thetotal=Falseargument totyping.TypedDict(). Compare required.- open¶
A TypedDict type is open if it may contain arbitrary additional items beyond those specified in the TypedDict definition. This is the default behavior for TypedDicts that do not use the
closed=Trueorextra_items=arguments totyping.TypedDict(). Open TypedDicts behave similarly to TypedDicts with extra items of typeReadOnly[object], but differ in some behaviors; see the TypedDict specification chapter for details. Compare extra items and closed.- package¶
A directory or directories that namespace Python modules. (Note the distinction between packages and distributions. While most distributions are named after the one package they install, some distributions install multiple packages.)
- read-only¶
A read-only item in a TypedDict may not be modified. Attempts to delete or assign to that item should be reported as type errors by a type checker. Read-only items are created using the
typing.ReadOnlyqualifier.- required¶
If an item in a TypedDict is required, it must be present in any object of that TypedDict type. Items are required by default, but items can also be explicitly marked as required using the
typing.Requiredqualifier. Compare non-required.- special form¶
A special form is an object that has a special meaning within the type system, comparable to a keyword in the language grammar. Examples include
Any,Generic,Literal, andTypedDict. Special forms can often but not always be used within type expressions. Special forms can usually be imported from thetypingmodule or equivalently fromtyping_extensions, but some special forms are placed in other modules.- structural¶
A structural type (see e.g. Protocols, Typed dictionaries) defines a set of values not by their
__class__, but by their properties (e.g. attributes, methods, dictionary key/value types). Callable types are also structural; a callable type is a subtype of another callable type based on their signatures, not a subclass relationship. In contrast, see nominal types.- stub¶
A file containing only type information, empty of runtime code (the filename ends in
.pyi). See Stub files.- subtype¶
A fully static type
Bis a subtype of a fully static typeAif and only if the set of possible runtime values represented byBis a subset of the set of possible runtime values represented byA. For nominal types (classes), subtyping is defined by inheritance. For structural types, subtyping is defined by a shared set of attributes/methods or keys. Subtype is the inverse of supertype. A type that is not fully static is not a subtype or supertype of any other type, but via materialization can be assignable to another type. See Type system concepts.- supertype¶
A fully static type
Ais a supertype of a fully static typeBif and only if the set of possible runtime values represented byAis a superset of the set of possible runtime values represented byB. Supertype is the inverse of subtype. See Type system concepts.- type expression¶
An expression that represents a type. The type system requires the use of type expressions within annotation expression and also in several other contexts. See “Type and annotation expression” for details.
- type qualifier¶
A type qualifier is a special form that qualifies a type expression to form an annotation expression. For example, the type qualifier Final can be used around a type to indicate that the annotated value may not be overridden or modified. This term is also used for other special forms that modify a type, but using a different syntactic context, such as the @final decorator.
- wide¶
A fully static type
Ais wider than a fully static typeBif and only ifBis a subtype ofAandBis not equivalent toA. This means thatArepresents a proper superset of the possible values represented byB. See also “narrow”.