Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created December 5, 2025 10:44
Show Gist options
  • Select an option

  • Save mypy-play/22f7d5fb1f2f5f90634162a9c5d123dd to your computer and use it in GitHub Desktop.

Select an option

Save mypy-play/22f7d5fb1f2f5f90634162a9c5d123dd to your computer and use it in GitHub Desktop.
Shared via mypy Playground
from typing import Final
def foo(foo: Foo[int | str]) -> None:
pass
def bar(bar: Bar[int | str]) -> None:
pass
# Generic is not covariant because it used in mutable state
class Foo[T: int | str]:
def __init__(self, value: T):
self.value = value # mutable variable
def mmethod(self) -> None:
foo(self)
# Generic is covariant because it not used in mutable state
class Bar[T: int | str]:
def __init__(self, value: T):
self.value: Final = value # read-only variable
def method(self) -> None:
bar(self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment