Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created July 2, 2025 09:33
Shared via mypy Playground
from typing import *
class Foo:
class_var: ClassVar[str] = "somestr"
def bar(self) -> str:
return self.class_var
@mypy-play
mypy-play / main.py
Created July 2, 2025 07:57
Shared via mypy Playground
from typing import List, Set
class Wizard:
def __init__(self, name, expert, spells):
self.name = name
self.spells = spells
self.expert = expert
def average_spell_power(self):
return sum(self.spells) / len(self.spells)
@mypy-play
mypy-play / main.py
Created July 2, 2025 07:10
Shared via mypy Playground
type Pair[T] = tuple[T, T]
class Foo[P: Pair]:
def __init__(self, pair: P) -> None:
self.pair: P = pair
foo = Foo((1, "a"))
reveal_type(foo) # Foo[tuple[int, str]] ❌
reveal_type(foo.pair) # tuple[int, str] ❌
@mypy-play
mypy-play / main.py
Created July 2, 2025 07:10
Shared via mypy Playground
type Pair[T] = tuple[T, T]
class Foo[P: Pair]:
def __init__(self, pair: P) -> None:
self.pair: P = pair
foo = Foo((1, "a"))
reveal_type(foo) # Foo[tuple[int, str]] ❌
reveal_type(foo.pair) # tuple[int, str] ❌
@mypy-play
mypy-play / main.py
Created July 2, 2025 00:35
Shared via mypy Playground
from typing import TYPE_CHECKING
class A:
if not TYPE_CHECKING:
def __eq__(self, other: object) -> bool:
return False
A() == 1
@mypy-play
mypy-play / main.py
Created July 2, 2025 00:34
Shared via mypy Playground
class A:
def __eq__(self, other: object) -> bool:
return False
A() == 1
class B: pass
B() == 1
@mypy-play
mypy-play / main.py
Created July 2, 2025 00:34
Shared via mypy Playground
class A:
def __eq__(self, other: object) -> bool:
return False
A() == 1
class B: pass
B() == 1
@mypy-play
mypy-play / main.py
Created July 2, 2025 00:34
Shared via mypy Playground
class A:
def __eq__(self, other: object) -> bool:
return False
A() == 1
@mypy-play
mypy-play / main.py
Created July 2, 2025 00:20
Shared via mypy Playground
import enum
import functools
import typing as t
class Color(enum.Enum):
RED = enum.auto()
@enum.nonmember
@functools.cache
@mypy-play
mypy-play / main.py
Created July 1, 2025 23:52
Shared via mypy Playground
from typing import Never, Self, overload
from warnings import deprecated
class MyClass:
def __init__(self: Self, x: int):
self.x = x
@overload
def __eq__(self, other: Self) -> bool: ...