Skip to content

Examples shouldn't use typing_extensions imports for stable typing features #1986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
smheidrich opened this issue Apr 30, 2025 · 4 comments · May be fixed by #1987
Open

Examples shouldn't use typing_extensions imports for stable typing features #1986

smheidrich opened this issue Apr 30, 2025 · 4 comments · May be fixed by #1987
Labels
topic: documentation Documentation-related issues and PRs

Comments

@smheidrich
Copy link

smheidrich commented Apr 30, 2025

Description

There are a few code examples in the docs which import things from typing_extensions that are by now available in Python's typing standard library module:

List

from typing_extensions import Never

from typing_extensions import ParamSpec

... and 4 more occurrences in reference/generics.rst.

from typing_extensions import Protocol

... and 4 more occurrences in reference/protocols.rst.

from typing_extensions import assert_type

It probably makes sense to use typing imports for all of these instead, possibly with the typing_extensions alternative mentioned.

How to fix?

What is the policy for these? Should they just be replaced with bare typing imports or something more like

from typing import Protocol # or from typing_extensions for Python < 3.8

?
I haven't seen the latter in any of the other imports, which just import from typing without comment (EDIT: at least not in the code itself, but usually it is mentioned as an alternative for older versions somewhere around the code), but who knows...

@smheidrich smheidrich added the topic: documentation Documentation-related issues and PRs label Apr 30, 2025
@smheidrich smheidrich linked a pull request Apr 30, 2025 that will close this issue
@JelleZijlstra
Copy link
Member

Maybe import them from typing_extensions until they're in typing in all supported versions (i.e., currently 3.9+)?

@smheidrich
Copy link
Author

smheidrich commented Apr 30, 2025

@JelleZijlstra Hmm that doesn't seem to be the current rule, at least not universally: E.g. the code examples in the Type Narrowing guide import TypeIs from typing despite it having been introduced in Python 3.13, same for Self (3.11) in the Generics reference.

But in both cases there is prose / an info box somewhere near the example to explain that it can be imported from typing_extensions in older Python versions, so maybe that's the rule?

@srittau
Copy link
Collaborator

srittau commented Apr 30, 2025

Personally, I prefer to use typing in examples when a feature has been released in a stable version of Python, as that is the canonical location. Users should be smart enough to replace typing with typing_extensions when required.

@smheidrich
Copy link
Author

So maybe the best of both worlds would be:

  • Features available in all supported Python versions (3.9+) are just imported from typing and that's it.
  • Features not yet available in all supported Python versions are imported from typing in the code examples but get an infobox (or some prose if it fits better) about needing to be imported from typing_extensions if the Python version is too old.

I've changed the PR accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: documentation Documentation-related issues and PRs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants