Created
July 1, 2020 15:00
-
-
Save mypy-play/70d59f208548878654505b0b9c89f0a2 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import TypeVar, Generic, Literal | |
Tco = TypeVar("Tco", covariant=True) | |
class C(Generic[Tco]): | |
def __init__(self, x : Tco) -> None: | |
self.x = x | |
def f(self) -> Tco: | |
return self.x | |
Tcon = TypeVar("Tcon", contravariant=True) | |
class C2(Generic[Tcon]): | |
def f(self, x: Tcon) -> None: | |
pass | |
class C3(Generic[Tco]): | |
def __init__(self, x: Tco) -> None: | |
self.x = x | |
def f (self, x: Tco) -> None: | |
pass | |
class C4(Generic[Tcon]): | |
def __init__(self, x : Tcon) -> None: | |
self.x = x | |
def f(self) -> Tcon: | |
return self.x | |
class X: | |
pass | |
class Y(X): | |
pass | |
A = TypeVar('A', bound = X) | |
class Tensor(Generic[A]): | |
def __init__(self, a : A) -> None: ... | |
def fun(tensor : Tensor[A]) -> None : ... | |
v = X() | |
x = Tensor(v) | |
reveal_type(x) # main.Tensor[main.X*] | |
fun(x) | |
w = Y() | |
x0 = Tensor(w) | |
reveal_type(x0) | |
fun(x0) | |
y : Tensor[X] | |
reveal_type(y) # main.Tensor[main.X] | |
fun(y) | |
z : Tensor[Y] | |
reveal_type(z) | |
fun(z) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment