Skip to content

Can typing.TypeVar et al. be deprecated? #1983

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

Closed
sh-at-cs opened this issue Apr 23, 2025 · 3 comments
Closed

Can typing.TypeVar et al. be deprecated? #1983

sh-at-cs opened this issue Apr 23, 2025 · 3 comments
Labels
topic: documentation Documentation-related issues and PRs

Comments

@sh-at-cs
Copy link

sh-at-cs commented Apr 23, 2025

PEP 695 (Type Parameter Syntax) says:

The existing mechanism for allocating TypeVar, TypeVarTuple, and ParamSpec is retained for backward compatibility.

If that is the only reason they are kept around in typing, wouldn't it make sense to mark them as deprecated?

Or are there situations in which they still have advantages over PEP 695's dedicated syntax for type parameters?

The current Python docs just point out that a new syntax for type parameters exists, but don't say anything about deprecation:

Changed in version 3.12: Type variables can now be declared using the type parameter syntax introduced by PEP 695. The infer_variance parameter was added.

Related issues

@sh-at-cs sh-at-cs added the topic: documentation Documentation-related issues and PRs label Apr 23, 2025
@srittau
Copy link
Collaborator

srittau commented Apr 23, 2025

Not only is TypeVar still widely used, it's necessary to use if you want to support any Python version before 3.12. More than a soft-deprecation – like the one in we have in the documentation – is not feasible until Python 3.11 has reached end of life. And even then we'd have to analyze how often TypeVar is still used.

@srittau srittau closed this as not planned Won't fix, can't repro, duplicate, stale Apr 23, 2025
@sh-at-cs
Copy link
Author

sh-at-cs commented Apr 23, 2025

a soft-deprecation – like the one in we have in the documentation

My point is that there is no mention of any sort of deprecation in the docs.

Contrast this with e.g. TypeAlias, which was similarly made unnecessary by PEP 695 - its documentation now states:

Deprecated since version 3.12: TypeAlias is deprecated in favor of the type statement, which creates instances of TypeAliasType and which natively supports forward references. [...] Removal of TypeAlias is not currently planned, but users are encouraged to migrate to type statements.

Why wasn't the same done for TypeVar, TypeVarTuple, and ParamSpec? And can it be done now?

@sh-at-cs
Copy link
Author

I guess I can think of one reason: Just as TypeAliasType isn't deprecated because it's what type keywords desugar to, TypeVar etc. remain the runtime representation of PEP 695 type parameters:

from inspect import signature

def ident[T](x: T) -> T:
  return x

x_annot = signature(ident).parameters["x"].annotation
print(type(x_annot))  # <class 'typing.TypeVar'>

And there is one hint in the docs that PEP 695 syntax should be preferred, just not in the usual "deprecation notice" place but at the start of TypeVar's docs:

The preferred way to construct a type variable is via the dedicated syntax for generic functions, generic classes, and generic type aliases: [...]

So you're right 👍 Issue can remain closed.

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

No branches or pull requests

2 participants