Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created May 9, 2024 00:44
Shared via mypy Playground
from typing import Literal, override
class Foo:
@override
def __eq__(self, value: object, /) -> bool:
return True
def fun(a: Foo | Literal["a"]) -> None:
@mypy-play
mypy-play / main.py
Created May 9, 2024 00:43
Shared via mypy Playground
from typing import Literal, override
class Foo:
@override
def __eq__(self, value: object, /) -> bool:
return True
def fun(a: Foo | Literal["a"]) -> None:
@mypy-play
mypy-play / main.py
Created May 8, 2024 23:10
Shared via mypy Playground
from typing import Iterator
def fib(n: int) -> Iterator[int]:
a, b = 0, 1
while a < n:
yield a
a, b = b, a + b
@mypy-play
mypy-play / main.py
Created May 8, 2024 22:20
Shared via mypy Playground
from typing import NamedTuple
class Record(NamedTuple):
id: int
name: str
age: int
reveal_type(Record.name) # str
@mypy-play
mypy-play / main.py
Created May 8, 2024 22:19
Shared via mypy Playground
from __future__ import annotations
from dataclasses import dataclass
import os
from n2t.core.vm_translator.writer import Writer
from n2t.core.vm_translator.vm_parser import INSTRUCTIONS, VmParser
@dataclass
@mypy-play
mypy-play / main.py
Created May 8, 2024 21:26
Shared via mypy Playground
import typing
class NDArray:
shape: tuple
def __setitem__(self, idx: NDArray, v: object) -> None: ...
def random(x: object) -> NDArray: return NDArray()
Image = typing.NewType('Image', NDArray)
Mask = typing.NewType('Mask', NDArray)
@mypy-play
mypy-play / main.py
Created May 8, 2024 19:55
Shared via mypy Playground
from typing import NewType, TYPE_CHECKING
class Weight(int):
if TYPE_CHECKING:
def __iadd__(self, x: object): ...
x = Weight(10)
y = Weight(2)
@mypy-play
mypy-play / main.py
Created May 8, 2024 19:48
Shared via mypy Playground
from typing import NewType
Weight = NewType("Weight", int)
x = Weight(10)
y = Weight(2)
# This is what I want to do
x += y
@mypy-play
mypy-play / main.py
Created May 8, 2024 19:48
Shared via mypy Playground
from typing import NewType
Weight = NewType("Weight", int)
x = Weight(10)
y = Weight(2)
# This is what I want to do
x += y
@mypy-play
mypy-play / main.py
Created May 8, 2024 19:47
Shared via mypy Playground
from typing import NewType
Weight = NewType("Weight", int)
x = Weight(10)
y = Weight(2)
x += y
x = Weight(x + y)