Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created May 11, 2024 11:39
Show Gist options
  • Save mypy-play/0a76458c339cf43130ce0972dd7538d9 to your computer and use it in GitHub Desktop.
Save mypy-play/0a76458c339cf43130ce0972dd7538d9 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
from typing import Generic, Any, overload
from typing_extensions import TypeVar
class C:
pass
class CustomC(C):
pass
_C = TypeVar('_C', bound=C, default=C)
class A(Generic[_C]):
@overload
def __init__(self: 'A[C]') -> None: ...
@overload
def __init__(self: 'A[_C]', c: type[_C]) -> None: ...
def __init__(self, c: Any = C) -> None:
self._c: type[_C] = c
@property
def c(self) -> type[_C]:
return self._c
@c.setter
def c(self, c: type[_C]) -> None:
self._c = c
plain = A()
reveal_type(plain)
reveal_type(plain.c)
custom = A(CustomC)
reveal_type(custom)
reveal_type(custom.c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment