4 ms·
Of course you can do this with Typescript but what about Python if you're stuck using it for $dayjob. Any tips?
by fud101 9mo ago
Of course you can do this with Typescript but what about Python if you're stuck using it for $dayjob. Any tips?
- rastrian 9mo agoYou can get most of the “ADT/state-machine reliability” benefits in Python by combining static checking + tagged unions + boundary validation: Model states as tagged unions (Union + Literal + dataclass(frozen=True)), use match (Py3.10+) and add assert_never so type checkers complain when you forget a case. Run Pyright (strict) or mypy –strict in CI so “illegal states” show up as build failures, not incidents. Validate/parsing at boundaries (HTTP/queues) with Pydantic discriminated unions (tagged unions at runtime), then keep internals typed. For expected failures, prefer an explicit Result (e.g., returns) over exceptions-as-control-flow. Use Ruff for lint/consistency (it’s not a type checker, but pairs well with one). References here: Pyright: https://microsoft.github.io/pyright/ https://microsoft.github.io/pyright/ mypy --strict: https://mypy.readthedocs.io/en/stable/getting_started.html https://mypy.readthedocs.io/en/stable/getting_started.html PEP 634 (match): https://peps.python.org/pep-0634/ https://peps.python.org/pep-0634/ assert_never & exhaustiveness guide: https://typing.python.org/en/latest/guides/unreachable.html https://typing.python.org/en/latest/guides/unreachable.html typing_extensions (backports): https://typing-extensions.readthedocs.io/ https://typing-extensions.readthedocs.io/ Pydantic discriminated unions: https://docs.pydantic.dev/latest/concepts/unions/ https://docs.pydantic.dev/latest/concepts/unions/ returns Result: https://returns.readthedocs.io/en/latest/pages/result.html https://returns.readthedocs.io/en/latest/pages/result.html Ruff FAQ: https://docs.astral.sh/ruff/faq/ https://docs.astral.sh/ruff/faq/