You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
generic methods/generic self don't seem to play with parameterized generics #4395
(There are a couple of other issues that I think might be the same thing as this question, but I'm not sure, because they're posed at a lower level—apologies if this is actually a duplicate. #3153, maybe?)
The generic methods and generic self docs show how write a class with methods that accept/return arguments of the most precise type, so things like this work as intended:
The type of compose shouldn't be (FunctionLike[In, Out], FunctionLike[Out, B]) -> FunctionLike[In, B], because then any two instances of any two subclasses could be composed together. It can't be (F, F) -> F because then all the type parameters are the same.
Many aspects of #2354 and #3153 can be fixed (and I actually wanted to do this soon).
But, your particular feature request is too tricky. This is what is known in other languages as higher kinds. A normal generic is something that expects a type (or several types) and "returns" a type. While in your case, FunctionLike.compose is something that itself expects a type constructor (F in your case) that takes and "returns" a type. If you are familiar wit Haskell, then normal generics (supported by mypy) are * -> *, * -> * -> * (two type variables), etc. You want something like (* -> *) -> * -> * (if I fix In and Out for simplicity). Supporting this is more tricky (especially the type inference), so don't expect this in the near future. Fortunately, in Python it is OK to write Any (or other type less precise than you would want).
Activity
elazarg commentedon Dec 19, 2017
Related: #2354
ilevkivskyi commentedon Dec 19, 2017
Many aspects of #2354 and #3153 can be fixed (and I actually wanted to do this soon).
But, your particular feature request is too tricky. This is what is known in other languages as higher kinds. A normal generic is something that expects a type (or several types) and "returns" a type. While in your case,
FunctionLike.composeis something that itself expects a type constructor (Fin your case) that takes and "returns" a type. If you are familiar wit Haskell, then normal generics (supported by mypy) are* -> *,* -> * -> *(two type variables), etc. You want something like(* -> *) -> * -> *(if I fixInandOutfor simplicity). Supporting this is more tricky (especially the type inference), so don't expect this in the near future. Fortunately, in Python it is OK to writeAny(or other type less precise than you would want).msullivan commentedon Jan 28, 2020
Closing this in favor of the other issues linked