Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created May 3, 2025 10:58
Show Gist options
  • Save mypy-play/4095e44649227a1372220d430542f95f to your computer and use it in GitHub Desktop.
Save mypy-play/4095e44649227a1372220d430542f95f 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) # def [T] (t: T`1) -> __main__.Foo[T`1]
a = printer(3)
a.print(...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment