Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created May 3, 2025 10:56
Show Gist options
  • Save mypy-play/36ace5d416f05fd648f7d7a4147f87f4 to your computer and use it in GitHub Desktop.
Save mypy-play/36ace5d416f05fd648f7d7a4147f87f4 to your computer and use it in GitHub Desktop.
Shared via mypy Playground
from typing import Generic, TypeVar
T = TypeVar("T")
class Foo(Generic[T]):
def __init__(self, t: T):
self.t = t
def print(self, t: T):
print(t, self.t)
class Foo2(Foo[T]):
def print(self, t: T):
if t == self.t:
print("equal")
else:
Foo.print(self, t)
check_equal: bool = ...
printer = Foo2 if check_equal else Foo
reveal_type(printer)
a = printer(3)
a.print(...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment