Created
May 3, 2025 11:31
-
-
Save mypy-play/f6cbcc0d99090c28f98aa587331db4ce 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, cast, reveal_type | |
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], Generic[T]): | |
def print(self, t: T): | |
if t == self.t: | |
print("equal") | |
else: | |
Foo.print(self, t) | |
def test(check_equal: bool) -> None: | |
a = (Foo2 if check_equal else Foo)(3) | |
reveal_type(a) | |
a.print(4) | |
a.print("not int") | |
printer = Foo2 if check_equal else Foo | |
reveal_type(printer) | |
p = printer(3) | |
reveal_type(p) | |
p.print(4) | |
p.print("not int") | |
p = cast("type[Foo[int]]", printer)(3) | |
reveal_type(p) | |
p.print(4) | |
p.print("not int") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment