| description | Python conventions following PEP 8 and modern best practices | |
|---|---|---|
| globs |
|
- Follow PEP 8: 4-space indentation, 88-character line limit (Black-compatible).
- Use f-strings for string interpolation. Do not use
%formatting or.format(). - Use double quotes for strings unless the string contains a double quote.
- Add type hints to all function signatures (parameters and return type).
- Use
from __future__ import annotationsfor forward references. - Use
Optional[X]orX | None(Python 3.10+) for nullable types. Never leave them unannotated. - Use
TypeVarand generics when writing reusable utilities.
- Use
dataclassesfor plain data containers. UsepydanticBaseModelwhen validation or serialization is needed. - Do not use bare
dictortupleto represent structured data.
- Use
pathlib.Pathfor all file-system operations. Never useos.path.
- Prefer list comprehensions for simple one-to-one or filter transforms.
- Use generator expressions when the result is consumed only once.
- Avoid nested comprehensions deeper than two levels.
- Use the
loggingmodule. Never useprintfor program output or debugging. - Configure loggers at the module level:
logger = logging.getLogger(__name__).
- Catch specific exception types. Never use a bare
except:orexcept Exception:without re-raising or logging. - Raise
ValueErrorfor invalid input,RuntimeErrorfor unexpected state. - Use context managers (
with) for resource management.
- Group imports: standard library, third-party, local. Separate each group with a blank line.
- Do not use wildcard imports (
from module import *).