Skip to content

Instantly share code, notes, and snippets.

@nbraud
Created October 21, 2018 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbraud/a592985bb700ac74894efd8243cb08ce to your computer and use it in GitHub Desktop.
Save nbraud/a592985bb700ac74894efd8243cb08ce to your computer and use it in GitHub Desktop.
$ mypy test.bad.py
test.bad.py:22: error: "V2" has no attribute "method"
$ mypy /tmp/test.good.py
[no errors returned]
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()
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