Created
May 3, 2025 10:58
-
-
Save mypy-play/4095e44649227a1372220d430542f95f to your computer and use it in GitHub Desktop.
Shared via mypy Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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