How I write Python
- 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.
- Faster than Beautiful Soup with html5-parser or html5lib
- Easier to install than html5-parser. Doesn’t require disabling binary wheels for lxml.
- The API design helps type safety
- Part of the standard library since Python 3.11
-
configparseris also in the standard library, but TOML is standardized and allows more structure - For writing TOML files, there is tomli-w
- Correctly handles DST
- Uses types to prevent mistakes
See “Ten Python datetime pitfalls, and what libraries are (not) doing about it”, Arie Bovenberg2024.
- Follows the CommonMark standard
- Typed
- Faster than other CommonMark libraries in pure Python
- Fast JSON, MessagePack, TOML, and YAML serialization of dataclass-like classes
- Typed
- 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.
- 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.
- Fast
- Manages Python versions as well as projects
- Can also run single-file scripts with dependencies
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.
- 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, andflake8
shiv.
- A command line utility for building self-contained Python zipapps. However, shiv only packages binary dependencies for the current platform.
-
More readable tests with
assertcompared to the specialized assertions inunittest - 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.
- “Deploying a distributed system? A type system helps a lot”, Spencer Baugh2021.
- “Algebraic Data Types in (typed) Python”, Tin Tvrtković2023
- “Writing Python like it’s Rust”, Jakub Beránek2023. The author’s style resembles the style I have arrived it.
- “Shape typing in Python”, Jim Fisher2024
- 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.
Fast
Good error messages
-
Python type checkers—my GitHub list
- “Awesome Python Typing”—a collection of links to software and articles