Skip to content

Instantly share code, notes, and snippets.

@mypy-play
mypy-play / main.py
Created November 16, 2024 02:13
Shared via mypy Playground
from dataclasses import dataclass, fields
import os
@dataclass(kw_only=False)
class ConveyorInfo:
conveyor_id: str
conveyor_run_id: int
release_identifier: str
revert: bool
@mypy-play
mypy-play / main.py
Created November 16, 2024 02:12
Shared via mypy Playground
from typing import Callable
type ValueConverter[T] = Callable[[str], T]
class EnvVar[T]:
def __init__(self, key: str, convert: ValueConverter[T] = str):
self.key = key
self.convert = convert
class DatasetReference:
@mypy-play
mypy-play / main.py
Created November 16, 2024 02:10
Shared via mypy Playground
from typing import Callable
type ValueConverter[T] = Callable[[str], T]
class EnvVar[T]:
def __init__(self, key: str, convert: ValueConverter[T] = str):
self.key = key
self.convert = convert
class DatasetReference:
@mypy-play
mypy-play / main.py
Created November 16, 2024 02:03
Shared via mypy Playground
from collections.abc import Callable
def foo(x: str, y: str = '') -> str:
return 'foo'
bar: Callable[[str], str] = foo
@mypy-play
mypy-play / main.py
Created November 15, 2024 23:50
Shared via mypy Playground
import enum
from typing import Literal, Protocol, TypeVar
class ISOCode(enum.StrEnum):
CAISO = "CAISO"
ERCOT = "ERCOT"
MISO = "MISO"
@mypy-play
mypy-play / main.py
Created November 15, 2024 16:57
Shared via mypy Playground
from enum import Enum
class E(Enum):
A = 1
B = 2
C = 3
def ret_num(self) -> str:
match self:
case E.A:
@mypy-play
mypy-play / main.py
Created November 15, 2024 16:45
Shared via mypy Playground
from typing import Final, Literal
def fun(x: Literal["a", "b"]) -> None:
print(x)
def use_fun() -> None:
var = "a"
fun(var) # mypy doesn't seem clever enough to infer that var is "a"
@mypy-play
mypy-play / main.py
Created November 15, 2024 16:44
Shared via mypy Playground
from typing import Final, Literal
class Rgb:
RED: Final = "red"
GREEN: Final = "green"
BLUE: Final = "blue"
BLACK: Final = "black"
@mypy-play
mypy-play / main.py
Created November 15, 2024 16:37
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 November 15, 2024 11:57
Shared via mypy Playground
from typing import Final
from dataclasses import dataclass
@dataclass
class A:
a: Final = 1
b: Final[int] = 1