Skip to content

Instantly share code, notes, and snippets.

@brunokim
Last active March 8, 2019 22:10
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 brunokim/e86367ed63cbd52d96b3564f502831b2 to your computer and use it in GitHub Desktop.
Save brunokim/e86367ed63cbd52d96b3564f502831b2 to your computer and use it in GitHub Desktop.
pytype difficulties with classmethod
from typing import Generic, List, TypeVar
T = TypeVar('T')
class Container(Generic[T]):
def __init__(self, x: T):
self.x = x
def __repr__(self):
return f'<Container {self.x}>'
class Thing:
_things = [] # type: List['Thing']
def __init__(self, name: str):
self.name = name
type(self)._things.append(self)
@classmethod
def get(cls, id_: int) -> Container['Thing']:
return Container(cls._things[id_])
def __repr__(self):
return f'<Thing {self.name}>'
if __name__ == '__main__':
a, b, c = Thing('a'), Thing('b'), Thing('c')
print(Thing.get(2))
# (generated with --quick)
from typing import Any, Generic, TypeVar
a: Thing
b: Thing
c: Thing
T = TypeVar('T')
class Container(Generic[T]):
x: Any
def __init__(self, x: T) -> None: ...
class Thing:
name: str
def __init__(self, name: str) -> None: ...
def __repr__(self) -> str: ...
@classmethod
def get(cls, id_: int) -> Any: ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment