Skip to content

Instantly share code, notes, and snippets.

@bryanmylee
Created March 15, 2022 13:00
Show Gist options
  • Save bryanmylee/af4138b8bad14f755613ef8e7c52df41 to your computer and use it in GitHub Desktop.
Save bryanmylee/af4138b8bad14f755613ef8e7c52df41 to your computer and use it in GitHub Desktop.
Add type hints to a Python factory method
from typing import Type, TypeVar
T = TypeVar('T', bound='TrivialClass')
class TrivialClass:
# ...
@classmethod
def from_int(cls: Type[T], int_arg: int) -> T:
# ...
return cls(...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment