-
Notifications
You must be signed in to change notification settings - Fork 242
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
Lower bound for TypeVars #674
Comments
Can you elaborate with an example? I'm not going to try and understand the Scala docs, and there's too much discussion in #59 to know what specifically you meant. |
The argument type cannot be T since it would then occur in the contravariant position. |
(A little confusing since OK, so assuming How common do you think this use case would be in real-world Python code? (I'm not interested in alternative realities where Python is a functional language. :-) |
This feature appears from time to time in various contexts, see e.g. python/mypy#3737. So I can see the value in it, but I don't think it is a priority ATM. |
Yeah, this would be nice every once in a while. This might help with some I agree that this is pretty low priority. We'd probably need to change |
I just hit this issue while building some custom data structure types on top of the popular pyrsistent library. I was creating custom immutable list and set types that are covariant in the element type. Without lower type bounds, I could not logically implement (at least) the following methods:
To provide a better example (not using the invariant from typing import *
K = TypeVar("K")
V = TypeVar("V", covariant=True)
B = TypeVar("B")
class ImmutableMap(Mapping[K, V]):
def updated(self, key: K, value: B) -> "ImmutableMap[K, ?]":
""" Returns a new `ImmutableMap` where the `key` is associated with a new `value`."""
... The question is: what is the return type of
Since we are discussing typed collections here, scenario 3 is very rare and probably irrelevant. Scenario 1 and 2 are both logical and (in my experience) fairly common. If we could declare Without lower bounds, we can only support scenario 1 by annotating I understand this might stay low-priority. I just wanted to hopefully clear up some of the motivation and document a specific use case that hit this limitation. |
I'm having a similar problem. I've written a dependency injector for python (I know the code is horrible) and I'm adding type hints to its code so that it passes mypy checks. I have a method with the signature like this: def bind(self, types: Tuple[Type[T], ...], instance: S, name: Optional[str] = None, singleton: bool = False) -> None: The idea being that Is there any way this relationship between |
Lower bounds are useful when implementing type safe methods of (classes that are covariant with some type) that take argument of some other type. For example, the method
updated
of scala'sSeq
reference: #59
The text was updated successfully, but these errors were encountered: