Created
October 21, 2018 03:03
-
-
Save nbraud/a592985bb700ac74894efd8243cb08ce to your computer and use it in GitHub Desktop.
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
$ mypy test.bad.py | |
test.bad.py:22: error: "V2" has no attribute "method" | |
$ mypy /tmp/test.good.py | |
[no errors returned] |
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
import typing | |
VectorLike = typing.Union[ | |
'V2', | |
typing.Tuple[int, int], | |
] | |
class V2: | |
def __init__(self, x: int, y: int) -> None: | |
pass | |
def __add__(self, other: VectorLike) -> V2: | |
return self | |
class V(V2): | |
def method(self) -> typing.NoReturn: | |
raise NotImplementedError | |
x = V(1, 0) + (0, 1) | |
x.method() |
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 numbers import Real | |
import typing | |
VectorLike = typing.Union[ | |
'V2', | |
typing.Tuple[int, int], | |
] | |
class V2: | |
def __init__(self, x: int, y: int) -> None: | |
pass | |
T = typing.TypeVar('T', bound=V2) | |
def __add__(self: T, other: VectorLike) -> T: | |
return self | |
class V(V2): | |
def method(self) -> typing.NoReturn: | |
raise NotImplementedError | |
x = V(1, 0) + (0, 1) | |
x.method() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment