-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
PEP 695: Introduce in
and out
keywords for specifying variance
#2724
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
Comments
This has been discussed in the typing-sig. Please refer to Appendix A for a survey of other languages. Contrary to what you've said, most languages do not require the specification of variance and do not provide a way to specify it explicitly. It introduces additional cognitive burden on language users, so absent a strong justification for including it, we should not include this. In all the discussions so far, we haven't identified such a reason. There is some justification for including |
It also doesn't mention Dart, which uses |
Kotlin supports use-site variance, so |
The pep doesn't mention sub types at all: class A[T]:
def foo(self) -> T: ...
class B[T](A[T]): # is this an error?
def bar(self, t: T): ... I like the explicit nature of Kotlin/Dart, just from the class def I can tell how I can use it. |
Dart, C# and typescript have |
OK, thanks for your input. I don't think most share your view on this matter. I definitely don't. Unnecessarily increasing cognitive load for developers is generally undesirable. |
in
and out
keywords for specifying variancein
and out
keywords for specifying variance
If the variance is explicit, then it makes it impossible to accidentally break the api of a library |
i agree with @KotlinIsland that
i'm on the fence as to whether or not they should be mandatory. i understand the reasoning behind wanting them to be inferred, but i definitely think we should have the option specify them explicitly. |
I've opened #2725 to resolve some of the errors in the pep. |
I agree that explicit variance not only increase readability/reduce cognitive load, but they also increase discover-ability of generic variance. I know experienced TypeScript developers that do not completely understand variance, due to it being implicitly inferred. |
Discussions about this PEP should go to the typing-sig mailing list (defined in the Discussions-To header). |
in
and out
keywords for specifying variancein
and out
keywords for specifying variance
In particular, it should go to the PEP's canonical discussion thread there — @erictraut , now that the PEP is live, could you link the appropriate thread in the |
@erictraut Many other languages (including the beloved TypeScript) have keywords for specifying generic variance of
in
andout
. I think it would be beneficial to introduce these for Python as well.An analysis of other languages are detailed in the pep here, but it contains some factual errors which are being addressed in PR #2725 .
proposal:
If the variance is explicit, then it makes it impossible to accidentally break the api of a library.
To be consistent with other languages (except the beloved TypeScript) , I would expect these to be mandatory and all generics should default to being invariant.
I agree with the beloved TypeScript maintainers that explicit variance not only increase readability/reduce cognitive load, but I also believe that they increase discover-ability of generic variance. Also TypeScript is the only language I know of that infers the variance of type parameters.
If variance is a hidden/inferred property, then I believe it will lead to the "common user" being stricken with confusion when they encounter it, explicit keywords make it much more consumable.
I know experienced TypeScript developers that do not completely understand variance, due to it being implicitly inferred.
Read the typing-sig thread here
The text was updated successfully, but these errors were encountered: