Description
Problem description
The typing
module docs have a problem.
typing.rst
is littered with references to the many typing-related PEPs that have been accepted by the Steering Council. Often these references to PEPs are used in lieu of detailed examples or explanation as to how a feature is supposed to work.
However, PEPs are not supposed to be living pieces of documentation. As such, proposals to update old typing PEPs to reflect changes in implementation details have been rejected. But when these PEPs are the closest thing we have to a full Python-typing specification, this means that our specifications are slowly growing out of date.
This unfortunate situation has also been recognised by the Steering Council in a recent email to the typing-sig mailing list.
Proposed solution
I propose that we slowly start to convert various typing PEPs into a series of Python-typing HOWTOs. In this way, we'd follow the example of other modules such as enum
or socket
. These modules have terse API references in socket.rst
and enum.rst
, but these API references are supplemented with expansive HOWTOs that serve as a guide for users who are new to the respective modules. Given the number of typing PEPs, the scale of the task for typing will be larger; but I still believe that this is a good example to follow.
Specifically, I think we should have something like the following structure:
- "Introduction to type hints in Python". This HOWTO would combine material from the following sources (updating examples where relevant to use modern syntax such as PEP-604 union types).
- PEPs 483 & 484 (introducing type hints to Python)
- PEP 526 (syntax for variable annotations,
ClassVar
) - PEP 613 (explicit type aliases)
- PEP 673 (
typing.Self
) - The sections in
typing.rst
onAny
,Callable
and type aliases. - Some of the material in
typing.rst
onTypeVar
and generics (but only the most basic material).
- "Generics HOWTO". While the intro HOWTO would introduce the basic concept of TypeVars, this HOWTO would tackle the nitty-gritty:
- Using
typing.Generic
to make generic classes. - The differences between bound TypeVars, constrained TypeVars, and TypeVars which are neither.
- Covariance versus contravariance versus invariance.
- Maybe a brief mention of PEP 585 (generic stdlib collections).
ParamSpec
andConcatenate
(material from PEP 612).- Variadic generics (material from PEP 646).
- Using
- "Structural subtyping HOWTO", using material from PEP 544 and the section in
typing.rst
on structural subtyping andtyping.Protocol
. - "TypedDict HOWTO", using material from PEPs 589 & 655, as well as the section on
TypedDict
intyping.rst
. - "Literal types HOWTO", describing how to use PEPs 586 (
typing.Literal
) & 675 (typing.LiteralString
).
Due to the brevity of their PEPs, and the fact that they don't really "fit in" neatly with any of the other proposed HOWTOs, I propose that the following typing features have their full specifications integrated into the typing.rst
API reference, rather than having dedicated HOWTOs:
- "
Final
/@final
(PEP 581) Annotated
(PEP 593)TypeGuard
(PEP 647)
Why not have this information at typing.readthedocs.io?
There's an ongoing effort, spearheaded by @srittau among others, to include a body of typing
documentation at https://typing.readthedocs.io/en/latest/. I am fully in support of this effort, and see the documentation there as complementary to the documentation that currently lives in the typing
PEPs. However, I do not believe that the documentation currently in the typing
PEPs should belong on that site, for several reasons:
- https://typing.readthedocs.io/en/latest/ is a third-party site, rather than being an "official" corpus of Python-language documentation. This is a good thing -- it allows contributors to that site to iterate and improve on the documentation there at a more rapid pace. However, the information in the
typing
PEPs are officially recognised changes to the language and the language's typing framework. As such, -- in my opinion -- it should live in the central docs.python.org documentation along with the documentation for the rest of the language. - https://typing.readthedocs.io/en/latest/ is currently not particularly discoverable via search engines. For example, it does not appear in the first page of Google results for "Python typing".
Questions to be discussed
- What do we think of the general idea of moving material out of PEPs, providing specifications through a series of HOWTOs, and slimming down
typing.rst
into a terser API reference? - What do we think of my specific proposal for how to structure this series of HOWTOs?
Copied to:
typing
module maintainers: @gvanrossum, @Fidget-Spinner, @JelleZijlstra- @srittau, who does a lot of valuable work on typing documentation over at the https://github.com/python/typing repo.
- @hauntsaninja, who recently expressed interest in helping with typing docs.
- Several miscellaneous typing-PEP authors who may be interested in this proposal: @ambv, @JukkaL, @ilevkivskyi, @mrkmndz, @pradeep90, @Gobot1234, @mrahtz, @davidfstr, @erictraut
- @encukou, who recently sent the email to typing-sig on this topic on behalf of the Steering Council, and is also a member of the docs working group.