Python Coding Guidelines
Philosophy:
We have exceptionally strict coding standards.
But we explicitly use them in tandem with LLMs.
No brittle linters. The LLM is the living compliance checker, code reviewer, and first-pass automator.
We achieve uncompromising quality control, reliability, and portability - while moving faster than ever before.
Guidelines
General Principles
- One file = one module = one purpose: Each file must be explicitly importable as a module and serve exactly one main purpose.
- Portability to C++: Code must be directly portable to C++ (avoid dynamic, Python-specific idioms).
Python Version Policy
- All code must run and be tested on both Python 2 and Python 3.
- Version-conditional logic: Only use
sys.version_infofor version checks. Do not useexcept ImportError,sys.version, or similar.
String Types
- Encoding/decoding UTF-8, URI, stdin/out, file names etc. can use our
textcompatpackage. - Only use the legacy
%formatting syntax. No.format()or f-strings.
Input Handling
- For interactive input, use our
unicode-raw-inputpackage. - Use
open(...)for binary files;codecs.open(...)for Unicode text files.
- Use
from __future__ import print_functionif usingprint().
Typing
- All APIs fully typed with type comments only (
# type: ...)- No inline type annotations, no
AnnAssign.
- No inline type annotations, no
- Use only typing features as in Python 3.5 / PEP 484.
- Absolutely no dependent typing. The return type of a function must not vary with different parameter types and/or values.
- No use of
@overloadpermitted.
Classes
- No
attrs,dataclasses, ornamedtuple. - All classes must have:
- Declared
__slots__ - Explicit
objectbase class - Use
six.with_metaclass(meta, *bases)for metaclasses - Mutable:
__init__; Immutable:__new__
- Declared
Enums
- Only manual/explicit values; do not use
auto()even withenum34.
Restricted Language Features
- Never use:
async,await,yield from, walrus (:=), structural pattern matching (match/case).
Platform Policy
- All code must run and be tested on both NT and POSIX.
- Determine platform with
posix-or-nt(returns'nt'or'posix'). - Never use the
os,os.path,subprocess, orplatformmodules. Overly complicated and convoluted codebases, and large parts are not available or behave differently on NT, Android, and iOS, etc.- Path manipulations: Conditionally import
ntpathorposixpathas needed. - Process launching: Use our
ctypes-unicode-proclaunch. - Env vars: Use our
read-unicode-environment-variables-dictionary.
- Path manipulations: Conditionally import
- Use
sys.platformif you absolutely need an OS name.
System API Access
- The
threadingandmultiprocessingmodules are not allowed. - Direct system calls via
ctypesare strictly limited as follows:- On NT:
- Only functions in MSVCRT (Microsoft C Runtime) and the standard Win32 API may be called.
- On POSIX:
- Only standardized POSIX functions (from libc or standard headers) may be called.
- Should be factored as a standalone, well-tested PyPI package similar to our other infra tools.
- On NT:
- If code needs capabilities beyond these, use PyQt/PySide and the Qt API as your system abstraction layer.
- Examples: advanced file I/O, process control, IPC, networking, device enumeration, drag-and-drop, clipboard, graphics, etc.
- Use our
detect-qt-bindingto automatically detect which Qt binding is available in your environment.
File and Folder Structure
- Import all files (modules) via absolute import. No relative import, no
sys.pathmanipulation. - All files (modules) must only have public functions and classes - no private/internal APIs.
- All directories must include an explicit
__init__.pywithin them.
Utility Code: No "utils.py" - Publish Generalized Tools
- No local "utils.py" files: Do not keep grab-bag or miscellaneous utility functions/classes in a project-private
utils.pyfile. - General-purpose tools must be published as standalone packages to PyPI, each focusing on one well-defined function, class, or concept.
- Move helpers that would otherwise go into "utils.py" into their own properly documented, versioned packages.
- These utility packages should be:
- Named after their actual purpose;
- Well-tested and actively maintained;
- Equipped with a README, proper docstrings, testable usage examples, semantic versioning, and a clear license.
- All projects share utilities via explicit dependencies rather than duplicating or copying helpers.
- When a utility is improved or a bug is fixed, updating the package ensures all dependent projects benefit automatically - "write once, run everywhere."
- This approach avoids hidden technical debt and promotes code quality, documentation, reuse, and maintainability across your entire codebase.
- It also contributes to the wider Python ecosystem.
Regular Expressions
- Regular expressions only for simple parsing.
- Use Unix-style/basic regex features:
.: any single character[ ]: character set/class[^ ]: negated class^, $: line start/end( ): grouping/subexpression*: zero or more
- For complicated input:
- Use a context-free grammar parser (e.g. Lark), hand-written parser, or an LLM.
Argument Parsing
- Use
argparse. - Use ambiguous
strfor all argument values (default API behavior). - All arguments should have a
help=...string.
Flag Arguments
- Presence only:
action='store_true'->bool - Counted:
action='count'->int - Single value:
type=str,required=True/False, explicitdefault=... - Multiple occurrences:
action='append'->Optional[List[str]]
Positional Arguments
- Exactly one: simple positional
- 0/1 (optional):
nargs='?', must be last positional argument - 0 or more/1 or more:
nargs='*'/nargs='+', with plural argument name and singularmetavar, must be last positional argument
Testing & Documentation
- Tests must:
- Run successfully on both Python 2 and 3, POSIX and NT.
- Be suitable for inclusion in
README.mdunder "Usage" or "Quickstart".- Simultaneously serve as usage documentation (idiomatic examples).
- Self-contained and runnable as a script or documentation block.
Python Packaging & Distribution
File Layout
- All files start with a copyright and license block:
- Boilerplate:
# Copyright (c) 2025 Jifeng Wu\n# Licensed under the <license> License. See LICENSE file in the project root for full license information.- simple infrastructure: MIT/BSD
- complex infra: Apache-2.0
- applications: AGPL-3.0
- Boilerplate:
Required Files & Metadata
README.mdwith standard boilerplate (see below)LICENSErequirements.txt(see example)pyproject.toml(see template)setup.cfg(see template)
README.md Boilerplate
1 | |
Example requirements.txt
1 | |
pyproject.toml Template
1 | |
Replace <project-name>, <version>, <license>, and requirements as appropriate.
setup.cfg Template
1 | |
Checking
Ensure you meet the following prerequisites:
- Your Python project is a Git repository.
- You have
pbpasteproperly set up.
Execute the following to generate an LLM prompt:
1 | |
Copy some or all of the above guidelines.
1 | |
Feed that LLM prompt into your LLM of choice. Then rm ${LLM_PROMPT_FILE}.
Python Coding Guidelines
https://jifengwu2k.github.io/2025/08/29/Python-Coding-Guidelines/