-
Notifications
You must be signed in to change notification settings - Fork 32
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
_TSource in Option should be covariant #171
Comments
I didn't see the above failing the type check in either from expression import Some, Option
class A:
pass
class B(A):
pass
def test_covariance() -> None:
x: Option[B] = Some(B())
y: Option[A] = x |
This isn't so easy to fix because covariant types can't be passed as parameters to other functions. |
It also looks good to use from typing_extensions import TypeVar
_T = TypeVar("_T", infer_variance=True) This parameter is supported by |
This looks really interesting. Will try. Look forward to when we can eventually be >= Python 3.12 😀 |
My understanding of
So I don't think it helps here. The problem still exists that covariant types can't be passed as parameters to other functions. |
I locally updated |
I've tried to make some progress on this one by making methods that takes @staticmethod
def Some(value: _T1) -> Option[_T1]:
"""Create a Some option."""
return Option(some=value) Then for some more tricky methods like def default_value(self, value: _T1) -> _TSource | _T1: I'm just unsure if the rest is ok, or false negatives. E.g: def default_with(self, getter: Callable[[], _TSource]) -> _TSource: It type checks, but is it ok to use |
From what i understand, I don't think we can have input paramters in python type hints as covariant. i think they usually are contravariant while returns are covariant. The type hints don't seem to let you know this and just has a generic error. |
Describe the bug
_TSource type in Option should be covariant.
Now since the _TSource is invariant, pyright will complain that B is not the same as A, but actually Option type should be covariant. Is
Some[Dog]
aSome[Animal]
? I think you'll agree that the answer is yes. Also Option type is not actually mutable, so there is no way to assign aSome[Dog]
to a variable typedSome[Aninmal]
and then make it becomeSome[Cat]
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: