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
(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.
Is there a way to express this?
The text was updated successfully, but these errors were encountered:
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).
(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:
mypy won't let me add
Mod5
s toMod6
s, hooray. But what if the relevant class is itself generic? This doesn't work: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.Is there a way to express this?
The text was updated successfully, but these errors were encountered: