How I write Python

HTTPX.

  • One library for both sync and async
  • Easy to use a SOCKS proxy
  • Similar to requests but typed

Fallback: urllib.request in the standard library.

Selectolax.

tomllib.

  • Part of the standard library since Python 3.11
  • configparser is also in the standard library, but TOML is standardized and allows more structure
  • For writing TOML files, there is tomli-w

Whenever.

  • Correctly handles DST
  • Uses types to prevent mistakes

See “Ten Python datetime pitfalls, and what libraries are (not) doing about it”, Arie Bovenberg2024.

markdown-it-py.

  • Follows the CommonMark standard
  • Typed
  • Faster than other CommonMark libraries in pure Python

msgspec.

  • Fast JSON, MessagePack, TOML, and YAML serialization of dataclass-like classes
  • Typed

Jinja.

  • Expressive flexible templates for HTML and other text
  • Macros have proven very useful

yarl.

  • Percent-decodes paths
  • Nice API:
    • Fields are named well
    • Query parameters are stored in a MultiDict

Fallback: urllib.parse in the standard library. It can quote as well as parse.

xmltodict.

  • More fun than any other XML library
  • Parses and generates XML. (Despite the name, xmltodict is bidirectional.)

I have a Copier template that packages my preferred development tooling. Type checkers are covered in the static-typing section.

uv.

Fallback: Poetry. Poetry is capable and uses standard metadata since version 2. It works on platforms where uv doesn’t, like NetBSD. Poetry has problems with PyTorch.

codespell.

  • Checks for common misspellings in code

Ruff.

  • Fast Python linter and code formatter written in Rust
  • Can replace many other tools like black, isort, pylint, and flake8

shiv.

pytest.

  • More readable tests with assert compared to the specialized assertions in unittest
  • Has a coverage plugin, pytest-cov

Fallback: unittest in the standard library isn’t bad.

Python has robust optional static typing. If you took a break from Python in the mid-2010s like I did, it may come as a surprise. This page links to resources for writing statically-typed Python, plus a few that make use of type hints at runtime.

  • jsonargparse—“Implement minimal boilerplate CLIs derived from type hints and parse from command line, config files and environment variables”
  • Typed Argument Parser (Tap)—“Typed argument parser for Python”
  • Typer—“Typer, build great CLIs. Easy to code. Based on Python type hints.”
  • tyro—“Zero-effort CLI interfaces & config objects, from types”

  • poltergeist—“Rust-like error handling in Python, with type-safety in mind.”

My preferred type checker is Pyright.