Repository navigation
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.rstonAny,Callableand type aliases. - Some of the material in
typing.rstonTypeVarand 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.Genericto 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).
ParamSpecandConcatenate(material from PEP 612).- Variadic generics (material from PEP 646).
- Using
- "Structural subtyping HOWTO", using material from PEP 544 and the section in
typing.rston structural subtyping andtyping.Protocol. - "TypedDict HOWTO", using material from PEPs 589 & 655, as well as the section on
TypedDictintyping.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
typingPEPs 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.rstinto a terser API reference? - What do we think of my specific proposal for how to structure this series of HOWTOs?
Copied to:
typingmodule 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.
Activity
gvanrossum commentedon Apr 14, 2022
Don’t worry about SEO. If what we place there is good, readers will come.
srittau commentedon Apr 15, 2022
@AlexWaygood I think once we have a fairly stable and good set of documentation on typing.readhedocs.io (and we are a long way off), we should look into moving some or all of it over to docs.python.org, but until then we are more free to experiment. I'd also like to merge your proposal here, the proposal in python/typing#845, and the "proposal" in https://github.com/python/typing/blob/master/docs/index.rst and https://github.com/python/typing/tree/master/docs/source into a coherent whole.
AlexWaygood commentedon Apr 15, 2022
Sure, but I also think we're in rough agreement about the place we want to end up at now, so it's probably time to stop talking about writing and start writing 🙂 we can debate the finer points of "what goes in which document" at a later date.
With that in mind:
TypedDictuser guides? The first draft could simply lift large chunks fromtyping.rst, the mypy docs, and/or the twoTypedDictPEPs (giving credit where appropriate)I'm thinking we can incrementally delete chunks from
typing.rstand add links pointing to https://typing.readthedocs.io/en/latest/ as different pieces get merged into the https://github.com/python/typing repo. We don't need the docs at https://typing.readthedocs.io/en/latest/ to be "finished" before we start linking to them fromtyping.rst; we can just say "here is a nice tutorial on how to useLiteral", etc.AlexWaygood commentedon Apr 15, 2022
@encukou, would the docs working group/community be interested in helping review these pieces of documentation as and when they're ready?
davidfstr commentedon Apr 15, 2022
I'd be happy to.
For my own reference: This initiative appears relevant to Python 3.11 documentation and therefore is more urgent than Python 3.12 items I'm already scheduled for, such as TypeForm/AnnotationType. So I'll probably pick it up next (in my queue of Python-related tasks).
encukou commentedon Apr 19, 2022
Most likely. Please open an issue at https://github.com/python/docs-community/issues when you're ready for the review!
adam-grant-hendry commentedon Sep 17, 2022
Please please please please please!! I am SO in need of this! (I can't possibly be the only newcomer who feels the same way)
adam-grant-hendry commentedon Sep 18, 2022
@AlexWaygood I would love to add this to use cases:
*argsas a Type Variable Tuple?I would also love if another link to the
typeshedreadthedocs could be added in an appropriate place:adam-grant-hendry commentedon Sep 18, 2022
If for nothing else, I think you would help lift the burden on yourselves of having to handle multiple incorrect/false/erroneous issues popping up on the
mypyissue tracker (or intyping).The longer the issues/discussions lists get, the harder it becomes for new users to search through them to find a related issue/discussion, which only serves to add fuel to the fire.
From a 30,000-foot view, this certainly feels like it would be a net win for both sides.
adam-grant-hendry commentedon Sep 18, 2022
After re-reading
Why not have this information at typing.readthedocs.io?, this last one might not be relevant...adam-grant-hendry commentedon Sep 18, 2022
Actually, us new users are saying we don't know where to look:
If we don't know where to go, we can't come...
Providing a new user perspective, what I'm lacking is HOW-TO design principles. These arguably come with experience, but this knowledge is beginning to be needed more and more by new users:
If a "business need" is what's required, I feel fairly safe in claiming you could probably grep the issue tracker and discussions for "that's working as intended" compared with actual bugs and find the percentage of the former greatly outweighs the latter.
Regardless of all of that, I think you'll find new users do want to learn the right way of doing things. I'm certainly in that camp. I want to know how to type hint properly and I want to be able to point others to a proper reference so I don't spread misinformation.
However a HOW-TO can be achieved, I am extremely in favor of it.
pythongh-96917: link to typing.readthedocs.io from typing.rst
pythongh-96917: link to typing.readthedocs.io from typing.rst (python…
gh-96917: link to typing.readthedocs.io from typing.rst (#96921)
pythongh-96917: link to typing.readthedocs.io from typing.rst (python…
[3.10] pythongh-96917: link to typing.readthedocs.io from typing.rst (p…
[3.10] pythongh-96917: link to typing.readthedocs.io from typing.rst (p…
gh-96917: link to typing.readthedocs.io from typing.rst (GH-96921)
[3.10] gh-96917: link to typing.readthedocs.io from typing.rst (GH-96921
gh-96917: link to typing.readthedocs.io from typing.rst (GH-96921)
pythongh-91533: Move much of `typing.rst` to a new 'Guide to typing f…