When generating Python code, apply the following Python Style Guidelines.
-
General Guidelines
- Strive to generate Pythonic code using
- clean, idiomatic, and efficient Python practices;
- standard library functions where appropriate;
- the Zen of Python guidelines.
- When generating Python code, follow the style guide specified below. If multiple guides are mentioned, prioritize the specified order of precedence.
- If a conflict arises between style guides, default to Black for formatting and to Google or Facebook for structural and type-related decisions.
- Strive to generate Pythonic code using
-
Style Guide Options
- Primary Style Guide: Follow the Google Python Style Guide conventions, unless specified otherwise.
- Alternative Styles (in order of preference):
- Black Code Style: Enforce automated formatting and 88-character line limits.
- Facebook Python Style Guide: Prioritize type annotations and scalability best practices.
- PEP 8.
-
Code Formatting
- Indentation: Use an indentation of
2 spaces
. - Line Length: Limit line length to 88 characters (Black).
- Blank Lines: Use PEP 8 / Black conventions for separating top-level functions, classes, methods, docstrings, and comments.
- Indentation: Use an indentation of
-
Imports
- Organize imports into three groups, in the following order:
- Standard library imports.
- Third-party imports.
- Local application/library-specific imports.
- Avoid wildcard imports (
from module import *
). - Example:
# Standard library imports import os import sys # Third-party imports import numpy as np # Local imports from my_project.module import my_function
- Organize imports into three groups, in the following order:
-
Naming Conventions
- Modules, Functions, and Variables: Use
snake_case
. - Classes: Use
PascalCase
. - Constants: Use
ALL_CAPS
.
- Modules, Functions, and Variables: Use
-
String Formatting
- Prefer f-strings for readability; use
.format()
as a secondary option. - Prefer double quotes for strings.
- Use single quotes for strings containing double quotes to avoid escaping characters.
- Use triple quotes for multi-line strings.
- Prefer f-strings for readability; use
-
Comments and Docstrings
- Write clear detailed comments explaining the why, not just the what.
- Use structured Google-style docstrings. For example:
def add(a: int, b: int) -> int: """ Adds two integers. Args: a (int): The first integer. b (int): The second integer. Returns: int: The sum of the two integers. Raises: ValueError: If either of the inputs is negative. """ if a < 0 or b < 0: raise ValueError("Inputs must be non-negative") return a + b
-
Type Annotations
- Include detailed type hints for all functions, methods, and variables where applicable.
-
Testing
- Follow PEP 8 conventions for test structure.
- Write unit tests using
pytest
. - Use descriptive test function names (e.g.,
test_add_positive_numbers
).