-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
(🎁) Convert code to utilize pep 695 #4617
Comments
Yes, I'd love to support this. (It will take some time, need to start by supporting it in the parser and the semantic model.) |
This is ready to be worked on! I've started in #6289 but there is much more to do :) |
Adds rule to convert type aliases defined with annotations i.e. `x: TypeAlias = int` to the new PEP-695 syntax e.g. `type x = int`. Does not support using new generic syntax for type variables, will be addressed in a follow-up. Added as part of pyupgrade — ~the code 100 as chosen to avoid collision with real pyupgrade codes~. Part of #4617 Builds on #5062
Extends #6289 to support moving type variable usage in type aliases to use PEP-695. Does not remove the possibly unused type variable declaration. Presumably this is handled by other rules, but is not working for me. Does not handle type variables with bounds or variance declarations yet. Part of #4617
Extends astral-sh#6289 to support moving type variable usage in type aliases to use PEP-695. Does not remove the possibly unused type variable declaration. Presumably this is handled by other rules, but is not working for me. Does not handle type variables with bounds or variance declarations yet. Part of astral-sh#4617
Hello! I'm interested in working on this, if that's okay! I think I have enough to go on here to round out support for generic type variables, the only snag is with handling type variables that have explicit variance. If a type var has explicit variance, I don't see a way to translate it to the PEP-695 style and guarantee identical semantics. The issue is that the new syntax for generics doesn't have a facility to explicitly set the variance of a type variable, the variance is always inferred. If we're adhering strictly to the spec (from my reading, at least), only If we're being a bit less strict, we could just assume that the variance inference will usually produce the same result, but I'm not sure how acceptable it is to potentially change the meaning of code. |
Hey @nathanwhit excited you're interested in working on this :) We can use different applicability levels (#4183) to indicate our certainty that the fix will or will not change the meaning of the code. In this case, we could use an I've never seen someone set |
Thanks for such a quick response @zanieb!
Makes sense!
Now that I'm looking, -- A little awkward, but I dug deeper and I think we don't have to worry about variance for type aliases after all! It turns out that the variance of a type variable is actually ignored for type aliases, all that matters is the variance of the type variable used in the generic class. (This makes sense now that I'm thinking about it, since variance is a property of a generic type and generic type aliases aren't actual generic types, just new names for an underlying generic type). For example: from typing import Generic, TypeVar
T = TypeVar("T", covariant=True)
class Covariant(Generic[T]):
pass
S = TypeVar("S", contravariant=True)
Co = Covariant[S]
class MyInt(int):
pass
foo: Covariant[MyInt] = Covariant[MyInt]()
bar: Covariant[int] = foo This typechecks despite the fact that So I think we can safely ignore the variance of a type var used in a type alias. Variance will be relevant again when we support upgrading classes to the new syntax ( |
I wrote this in #12542 MyPy released support for PEP 695 generics: python/mypy#15238 It would be awesome to do any of the following:
|
input:
output:
And also
ParamSpec
andTypeVarTuple
.The text was updated successfully, but these errors were encountered: