2019-03-04 04:53:46 -05:00
|
|
|
PEP: 585
|
2019-09-17 17:54:57 -04:00
|
|
|
Title: Type Hinting Generics In Standard Collections
|
2019-03-04 04:53:46 -05:00
|
|
|
Version: $Revision$
|
|
|
|
Last-Modified: $Date$
|
2019-04-16 10:50:15 -04:00
|
|
|
Author: Łukasz Langa <lukasz@python.org>
|
2019-09-17 17:54:57 -04:00
|
|
|
Discussions-To: Typing-Sig <typing-sig@python.org>
|
2019-03-04 04:53:46 -05:00
|
|
|
Status: Draft
|
|
|
|
Type: Standards Track
|
|
|
|
Content-Type: text/x-rst
|
2019-04-16 10:50:15 -04:00
|
|
|
Created: 03-Mar-2019
|
2019-09-17 17:54:57 -04:00
|
|
|
Python-Version: 3.9
|
2019-03-04 04:53:46 -05:00
|
|
|
|
|
|
|
Abstract
|
|
|
|
========
|
|
|
|
|
|
|
|
Static typing as defined by PEPs 484, 526, 544, 560, and 563 was built
|
|
|
|
incrementally on top of the existing Python runtime and constrained by
|
2019-09-17 17:54:57 -04:00
|
|
|
existing syntax and runtime behavior. This led to the existence of
|
|
|
|
a duplicated collection hierarchy in the ``typing`` module due to
|
|
|
|
generics (for example ``typing.List`` and the built-in ``list``).
|
2019-03-04 04:53:46 -05:00
|
|
|
|
2019-09-17 17:54:57 -04:00
|
|
|
This PEP proposes to enable support for the generics syntax in all
|
2019-09-17 18:12:42 -04:00
|
|
|
standard collections currently available in the ``typing`` module.
|
2019-03-04 04:53:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
Rationale and Goals
|
|
|
|
===================
|
|
|
|
|
2019-09-17 17:54:57 -04:00
|
|
|
This change removes the necessity for a parallel type hierarchy in the
|
|
|
|
``typing`` module, making it easier for users to annotate their programs
|
|
|
|
and easier for teachers to teach Python.
|
|
|
|
|
|
|
|
|
|
|
|
Terminology
|
|
|
|
===========
|
|
|
|
|
2020-02-24 23:18:17 -05:00
|
|
|
Generic (n.) -- a type that can be parameterized, typically a container.
|
2019-09-17 17:54:57 -04:00
|
|
|
Also known as a *parametric type* or a *generic type*. For example:
|
|
|
|
``dict``.
|
|
|
|
|
2020-02-24 23:18:17 -05:00
|
|
|
parameterized generic -- a specific instance of a generic with the
|
2019-09-20 11:46:18 -04:00
|
|
|
expected types for container elements provided. Also known as
|
2020-02-24 23:18:17 -05:00
|
|
|
a *parameterized type*. For example: ``dict[str, int]``.
|
2019-09-17 17:54:57 -04:00
|
|
|
|
2019-03-04 04:53:46 -05:00
|
|
|
|
|
|
|
Backwards compatibility
|
|
|
|
=======================
|
|
|
|
|
|
|
|
Tooling, including type checkers and linters, will have to be adapted to
|
2019-09-20 11:46:18 -04:00
|
|
|
recognize standard collections as generics.
|
2019-09-17 17:54:57 -04:00
|
|
|
|
2019-09-20 11:46:18 -04:00
|
|
|
On the source level, the newly described functionality requires
|
|
|
|
Python 3.9. For use cases restricted to type annotations, Python files
|
|
|
|
with the "annotations" future-import (available since Python 3.7) can
|
2020-02-24 23:21:22 -05:00
|
|
|
parameterize standard collections, including builtins. To reiterate,
|
2019-09-20 11:46:18 -04:00
|
|
|
that depends on the external tools understanding that this is valid.
|
2019-03-04 04:53:46 -05:00
|
|
|
|
|
|
|
Implementation
|
|
|
|
==============
|
|
|
|
|
|
|
|
Starting with Python 3.7, when ``from __future__ import annotations`` is
|
2020-02-24 23:21:22 -05:00
|
|
|
used, function and variable annotations can parameterize standard
|
2019-09-17 18:12:42 -04:00
|
|
|
collections directly. Example::
|
2019-03-04 04:53:46 -05:00
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
def find(haystack: dict[str, list[int]]) -> int:
|
|
|
|
...
|
|
|
|
|
2019-09-20 11:46:18 -04:00
|
|
|
Usefulness of this syntax before PEP 585 is limited as external tooling
|
|
|
|
like Mypy does not recognize standard collections as generic. Moreover,
|
|
|
|
certain features of typing like type aliases or casting require putting
|
2019-09-17 18:12:42 -04:00
|
|
|
types outside of annotations, in runtime context. While these are
|
2019-09-17 17:54:57 -04:00
|
|
|
relatively less common than type annotations, it's important to allow
|
2019-09-17 18:12:42 -04:00
|
|
|
using the same type syntax in all contexts. This is why starting with
|
|
|
|
Python 3.9, the following collections become generic using
|
2020-02-24 23:21:22 -05:00
|
|
|
``__class_getitem__()`` to parameterize contained types:
|
2019-09-17 17:54:57 -04:00
|
|
|
|
|
|
|
* ``tuple`` # typing.Tuple
|
|
|
|
* ``list`` # typing.List
|
|
|
|
* ``dict`` # typing.Dict
|
|
|
|
* ``set`` # typing.Set
|
|
|
|
* ``frozenset`` # typing.FrozenSet
|
|
|
|
* ``type`` # typing.Type
|
|
|
|
* ``collections.deque``
|
|
|
|
* ``collections.defaultdict``
|
|
|
|
* ``collections.OrderedDict``
|
|
|
|
* ``collections.Counter``
|
|
|
|
* ``collections.ChainMap``
|
|
|
|
* ``collections.abc.Awaitable``
|
|
|
|
* ``collections.abc.Coroutine``
|
|
|
|
* ``collections.abc.AsyncIterable``
|
|
|
|
* ``collections.abc.AsyncIterator``
|
|
|
|
* ``collections.abc.AsyncGenerator``
|
|
|
|
* ``collections.abc.Iterable``
|
|
|
|
* ``collections.abc.Iterator``
|
|
|
|
* ``collections.abc.Generator``
|
|
|
|
* ``collections.abc.Reversible``
|
|
|
|
* ``collections.abc.Container``
|
|
|
|
* ``collections.abc.Collection``
|
|
|
|
* ``collections.abc.Callable``
|
|
|
|
* ``collections.abc.Set`` # typing.AbstractSet
|
|
|
|
* ``collections.abc.MutableSet``
|
|
|
|
* ``collections.abc.Mapping``
|
|
|
|
* ``collections.abc.MutableMapping``
|
|
|
|
* ``collections.abc.Sequence``
|
|
|
|
* ``collections.abc.MutableSequence``
|
|
|
|
* ``collections.abc.ByteString``
|
|
|
|
* ``collections.abc.MappingView``
|
|
|
|
* ``collections.abc.KeysView``
|
|
|
|
* ``collections.abc.ItemsView``
|
|
|
|
* ``collections.abc.ValuesView``
|
|
|
|
* ``contextlib.AbstractContextManager`` # typing.ContextManager
|
|
|
|
* ``contextlib.AbstractAsyncContextManager`` # typing.AsyncContextManager
|
2020-02-06 11:37:46 -05:00
|
|
|
* ``re.Pattern`` # typing.Pattern, typing.re.Pattern
|
|
|
|
* ``re.Match`` # typing.Match, typing.re.Match
|
2019-09-17 17:54:57 -04:00
|
|
|
|
2019-09-20 11:46:18 -04:00
|
|
|
Importing those from ``typing`` is deprecated. Due to PEP 563 and the
|
|
|
|
intention to minimize the runtime impact of typing, this deprecation
|
|
|
|
will not generate DeprecationWarnings. Instead, type checkers may warn
|
2019-09-17 17:54:57 -04:00
|
|
|
about such deprecated usage when the target version of the checked
|
2019-09-20 11:46:18 -04:00
|
|
|
program is signalled to be Python 3.9 or newer. It's recommended to
|
|
|
|
allow for those warnings to be silenced on a project-wide basis.
|
|
|
|
|
|
|
|
The deprecated functionality will be removed from the ``typing`` module
|
|
|
|
in the first Python version released 5 years after the release of
|
|
|
|
Python 3.9.0.
|
2019-09-17 17:54:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
Parameters to generics are available at runtime
|
|
|
|
-----------------------------------------------
|
|
|
|
|
|
|
|
Preserving the generic type at runtime enables introspection of the type
|
|
|
|
which can be used for API generation or runtime type checking. Such
|
|
|
|
usage is already present in the wild.
|
|
|
|
|
2020-02-24 23:18:17 -05:00
|
|
|
Just like with the ``typing`` module today, the parameterized generic
|
2019-09-17 17:54:57 -04:00
|
|
|
types listed in the previous section all preserve their type parameters
|
|
|
|
at runtime::
|
|
|
|
|
|
|
|
>>> list[str]
|
|
|
|
list[str]
|
|
|
|
>>> tuple[int, ...]
|
|
|
|
tuple[int, ...]
|
|
|
|
>>> ChainMap[str, list[str]]
|
|
|
|
collections.ChainMap[str, list[str]]
|
|
|
|
|
|
|
|
This is implemented using a thin proxy type that forwards all method
|
|
|
|
calls and attribute accesses to the bare origin type with the following
|
|
|
|
exceptions:
|
|
|
|
|
2020-02-24 23:18:17 -05:00
|
|
|
* the ``__repr__`` shows the parameterized type;
|
|
|
|
* the ``__origin__`` attribute points at the non-parameterized
|
2019-09-17 17:54:57 -04:00
|
|
|
generic class;
|
2020-02-06 11:37:46 -05:00
|
|
|
* the ``__args__`` attribute is a tuple (possibly of length
|
2019-09-17 17:54:57 -04:00
|
|
|
1) of generic types passed to the original ``__class_getitem__``;
|
2020-02-06 11:37:46 -05:00
|
|
|
* the ``__parameters__`` attribute is a lazily computed tuple
|
|
|
|
(possibly empty) of unique type variables found in ``__args__``;
|
|
|
|
* the ``__getitem__`` raises an exception to disallow mistakes
|
|
|
|
like ``dict[str][str]``. However it allows e.g. ``dict[str, T][int]``
|
|
|
|
and in that case returns ``dict[str, int]``.
|
2019-09-17 17:54:57 -04:00
|
|
|
|
|
|
|
This design means that it is possible to create instances of
|
2020-02-24 23:18:17 -05:00
|
|
|
parameterized collections, like::
|
2019-09-17 17:54:57 -04:00
|
|
|
|
|
|
|
>>> l = list[str]()
|
|
|
|
[]
|
|
|
|
>>> list is list[str]
|
|
|
|
False
|
|
|
|
>>> list == list[str]
|
2020-02-06 11:37:46 -05:00
|
|
|
False
|
|
|
|
>>> list[str] == list[str]
|
2019-09-17 17:54:57 -04:00
|
|
|
True
|
2020-02-06 11:37:46 -05:00
|
|
|
>>> list[str] == list[int]
|
|
|
|
False
|
2020-02-19 06:17:19 -05:00
|
|
|
>>> isinstance([1, 2, 3], list[str])
|
2020-02-24 23:18:17 -05:00
|
|
|
TypeError: isinstance() arg 2 cannot be a parameterized generic
|
2020-02-19 06:17:19 -05:00
|
|
|
>>> issubclass(list, list[str])
|
2020-02-24 23:18:17 -05:00
|
|
|
TypeError: issubclass() arg 2 cannot be a parameterized generic
|
2020-02-19 06:17:19 -05:00
|
|
|
>>> isinstance(list[str], types.GenericAlias)
|
|
|
|
True
|
2019-09-17 17:54:57 -04:00
|
|
|
|
2020-02-24 23:18:17 -05:00
|
|
|
Objects created with bare types and parameterized types are exactly the
|
2019-09-17 17:54:57 -04:00
|
|
|
same. The generic parameters are not preserved in instances created
|
2020-02-24 23:18:17 -05:00
|
|
|
with parameterized types, in other words generic types erase type
|
2019-09-17 17:54:57 -04:00
|
|
|
parameters during object creation.
|
|
|
|
|
|
|
|
One important consequence of this is that the interpreter does **not**
|
|
|
|
attempt to type check operations on the collection created with
|
2020-02-24 23:18:17 -05:00
|
|
|
a parameterized type. This provides symmetry between::
|
2019-09-17 17:54:57 -04:00
|
|
|
|
|
|
|
l: list[str] = []
|
|
|
|
|
|
|
|
and::
|
|
|
|
|
|
|
|
l = list[str]()
|
|
|
|
|
2020-02-06 11:37:46 -05:00
|
|
|
For accessing the proxy type from Python code, it will be exported
|
|
|
|
from the ``types`` module as ``GenericAlias``.
|
|
|
|
|
|
|
|
Pickling or (shallow- or deep-) copying a ``GenericAlias`` instance
|
|
|
|
will preserve the type, origin, attributes and parameters.
|
|
|
|
|
2019-09-17 17:54:57 -04:00
|
|
|
|
|
|
|
Forward compatibility
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
Future standard collections must implement the same behavior.
|
2019-03-04 04:53:46 -05:00
|
|
|
|
|
|
|
|
2020-02-06 11:37:46 -05:00
|
|
|
Reference implementation
|
|
|
|
========================
|
|
|
|
|
|
|
|
A proof-of-concept or prototype `implementation
|
|
|
|
<https://bugs.python.org/issue39481>`__ exists.
|
|
|
|
|
|
|
|
|
2019-09-17 17:54:57 -04:00
|
|
|
Rejected alternatives
|
|
|
|
=====================
|
2019-03-04 04:53:46 -05:00
|
|
|
|
2019-09-17 17:54:57 -04:00
|
|
|
Do nothing
|
|
|
|
----------
|
2019-03-04 04:53:46 -05:00
|
|
|
|
2019-09-17 17:54:57 -04:00
|
|
|
Keeping the status quo forces Python programmers to perform book-keeping
|
|
|
|
of imports from the ``typing`` module for standard collections, making
|
|
|
|
all but the simplest annotations cumbersome to maintain. The existence
|
|
|
|
of parallel types is confusing to newcomers (why is there both ``list``
|
|
|
|
and ``List``?).
|
|
|
|
|
|
|
|
The above problems also don't exist in user-built generic classes which
|
|
|
|
share runtime functionality and the ability to use them as generic type
|
|
|
|
annotations. Making standard collections harder to use in type hinting
|
|
|
|
from user classes hindered typing adoption and usability.
|
|
|
|
|
|
|
|
Generics erasure
|
|
|
|
----------------
|
|
|
|
|
|
|
|
It would be easier to implement ``__class_getitem__`` on the listed
|
|
|
|
standard collections in a way that doesn't preserve the generic type,
|
|
|
|
in other words::
|
|
|
|
|
|
|
|
>>> list[str]
|
|
|
|
<class 'list'>
|
|
|
|
>>> tuple[int, ...]
|
|
|
|
<class 'tuple'>
|
|
|
|
>>> collections.ChainMap[str, list[str]]
|
|
|
|
<class 'collections.ChainMap'>
|
|
|
|
|
|
|
|
This is problematic as it breaks backwards compatibility: current
|
|
|
|
equivalents of those types in the ``typing`` module **do** preserve
|
|
|
|
the generic type::
|
|
|
|
|
|
|
|
>>> from typing import List, Tuple, ChainMap
|
|
|
|
>>> List[str]
|
|
|
|
typing.List[str]
|
|
|
|
>>> Tuple[int, ...]
|
|
|
|
typing.Tuple[int, ...]
|
|
|
|
>>> ChainMap[str, List[str]]
|
|
|
|
typing.ChainMap[str, typing.List[str]]
|
|
|
|
|
|
|
|
As mentioned in the "Implementation" section, preserving the generic
|
|
|
|
type at runtime enables runtime introspection of the type which can be
|
|
|
|
used for API generation or runtime type checking. Such usage is already
|
|
|
|
present in the wild.
|
|
|
|
|
|
|
|
Additionally, implementing subscripts as identity functions would make
|
2019-09-20 11:46:18 -04:00
|
|
|
Python less friendly to beginners. Say, if a user is mistakenly passing
|
|
|
|
a list type instead of a list object to a function, and that function is
|
|
|
|
indexing the received object, the code would no longer raise an error.
|
2019-09-17 17:54:57 -04:00
|
|
|
|
|
|
|
Today::
|
|
|
|
|
|
|
|
>>> l = list
|
|
|
|
>>> l[-1]
|
|
|
|
TypeError: 'type' object is not subscriptable
|
|
|
|
|
|
|
|
With ``__class_getitem__`` as an identity function::
|
|
|
|
|
|
|
|
>>> l = list
|
|
|
|
>>> l[-1]
|
|
|
|
list
|
|
|
|
|
|
|
|
The indexing being successful here would likely end up raising an
|
2019-09-20 11:46:18 -04:00
|
|
|
exception at a distance, confusing the user.
|
2019-09-17 17:54:57 -04:00
|
|
|
|
2020-02-24 23:18:17 -05:00
|
|
|
Disallowing instantiation of parameterized types
|
|
|
|
------------------------------------------------
|
2019-09-17 17:54:57 -04:00
|
|
|
|
|
|
|
Given that the proxy type which preserves ``__origin__`` and
|
2020-02-06 11:37:46 -05:00
|
|
|
``__args__`` is mostly useful for runtime introspection purposes,
|
2020-02-24 23:18:17 -05:00
|
|
|
we might have disallowed instantiation of parameterized types.
|
2019-09-17 17:54:57 -04:00
|
|
|
|
2020-02-24 23:18:17 -05:00
|
|
|
In fact, forbidding instantiation of parameterized types is what the
|
2019-09-20 11:46:18 -04:00
|
|
|
``typing`` module does today for types which parallel builtin
|
2020-02-24 23:18:17 -05:00
|
|
|
collections (instantiation of other parameterized types is allowed).
|
2019-09-17 17:54:57 -04:00
|
|
|
|
|
|
|
The original reason for this decision was to discourage spurious
|
2020-02-24 23:21:22 -05:00
|
|
|
parameterization which made object creation up to two orders of magnitude
|
2019-09-17 18:12:42 -04:00
|
|
|
slower compared to the special syntax available for those builtin
|
|
|
|
collections.
|
2019-09-17 17:54:57 -04:00
|
|
|
|
|
|
|
This rationale is not strong enough to allow the exceptional treatment
|
2020-02-24 23:18:17 -05:00
|
|
|
of builtins. All other parameterized types can be instantiated,
|
2019-09-17 17:54:57 -04:00
|
|
|
including parallels of collections in the standard library. Moreover,
|
|
|
|
Python allows for instantiation of lists using ``list()`` and some
|
|
|
|
builtin collections don't provide special syntax for instantiation.
|
|
|
|
|
2020-02-19 06:17:19 -05:00
|
|
|
Making ``isinstance(obj, list[str])`` perform a check ignoring generics
|
|
|
|
-----------------------------------------------------------------------
|
|
|
|
|
2020-02-24 23:18:17 -05:00
|
|
|
An earlier version of this PEP suggested treating parameterized generics
|
|
|
|
like ``list[str]`` as equivalent to their non-parameterized variants
|
2020-02-19 06:17:19 -05:00
|
|
|
like ``list`` for purposes of ``isinstance()`` and ``issubclass()``.
|
|
|
|
This would be symmetrical to how ``list[str]()`` creates a regular list.
|
|
|
|
|
|
|
|
This design was rejected because ``isinstance()`` and ``issubclass()``
|
2020-02-24 23:18:17 -05:00
|
|
|
checks with parameterized generics would read like element-by-element
|
2020-02-19 06:17:19 -05:00
|
|
|
runtime type checks. The result of those checks would be surprising,
|
|
|
|
for example::
|
|
|
|
|
|
|
|
>>> isinstance([1, 2, 3], list[str])
|
|
|
|
True
|
|
|
|
|
|
|
|
Note the object doesn't match the provided generic type but
|
|
|
|
``isinstance()`` still returns ``True`` because it only checks whether
|
|
|
|
the object is a list.
|
|
|
|
|
2020-02-24 23:18:17 -05:00
|
|
|
If a library is faced with a parameterized generic and would like to
|
2020-02-19 06:17:19 -05:00
|
|
|
perform an ``isinstance()`` check using the base type, that type can
|
2020-02-24 23:18:17 -05:00
|
|
|
be retrieved using the ``__origin__`` attribute on the parameterized
|
2020-02-19 06:17:19 -05:00
|
|
|
generic.
|
|
|
|
|
2019-09-17 17:54:57 -04:00
|
|
|
Making ``isinstance(obj, list[str])`` perform a runtime type check
|
|
|
|
------------------------------------------------------------------
|
2019-03-04 04:53:46 -05:00
|
|
|
|
2019-09-17 17:54:57 -04:00
|
|
|
This functionality requires iterating over the collection which is
|
|
|
|
a destructive operation in some of them. This functionality would have
|
|
|
|
been useful, however implementing the type checker within Python that
|
|
|
|
would deal with complex types, nested type checking, type variables,
|
2019-09-20 11:46:18 -04:00
|
|
|
string forward references, and so on is out of scope for this PEP.
|
2019-03-04 04:53:46 -05:00
|
|
|
|
2020-02-25 18:54:58 -05:00
|
|
|
Naming the type ``GenericType`` instead of ``GenericAlias``
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
|
|
|
We considered a different name for this type, but decided
|
|
|
|
``GenericAlias`` is better -- these aren't real types, they are
|
|
|
|
aliases for the corresponding container type with some extra metadata
|
|
|
|
attached.
|
|
|
|
|
2019-03-04 04:53:46 -05:00
|
|
|
|
2019-09-17 17:54:57 -04:00
|
|
|
Note on the initial draft
|
|
|
|
=========================
|
2019-03-04 04:53:46 -05:00
|
|
|
|
2019-09-17 17:54:57 -04:00
|
|
|
An early version of this PEP discussed matters beyond generics in
|
|
|
|
standard collections. Those unrelated topics were removed for clarity.
|
2019-03-04 04:53:46 -05:00
|
|
|
|
|
|
|
|
2019-09-17 17:54:57 -04:00
|
|
|
Copyright
|
|
|
|
=========
|
2019-03-04 04:53:46 -05:00
|
|
|
|
2019-09-17 17:54:57 -04:00
|
|
|
This document is placed in the public domain or under the
|
|
|
|
CC0-1.0-Universal license, whichever is more permissive.
|