Python Patterns Review
By Spencer Garcia · Published · Updated
Reviews Python code for typing, data modelling, exception handling, packaging, and test layout, with fixes that make it easier to read and safer to change.
Prompt template
## Python Patterns Review Review this Python code and show how to make it more idiomatic and maintainable. - The code: [textarea: Paste the module or project layout] - Purpose: [text: What it does and who uses it] - Python version: [text: e.g. 3.12] - Focus: [multi: Type hints, Dataclasses, Exceptions, Packaging, Test layout, Async code, Dependencies] - Type checker in use: [dropdown: mypy, pyright, None yet] Begin with typing: public functions should have complete hints, dict-shaped data that has a fixed set of fields should become a dataclass or TypedDict, and Any should be rare and justified. Then exceptions: catch specific types, never a bare except, raise with a message that names the bad value, and define a small custom hierarchy only when callers need to tell cases apart. For packaging, check the project layout, entry points, pinned and unpinned dependencies, and whether a pyproject file is the single source of truth. For tests, check that they mirror the package, use fixtures instead of copied setup, and test behaviour rather than internals. Present findings by impact with the line, the problem in a sentence, and a before and after snippet. Close with a short migration order so the changes can land as small commits.