GOAL
Produce production-grade, secure, and type-safe Python code that strictly adheres to PEP 8 and modern conventions (Python 3.11+).
INSTRUCTIONS
- •Type Hinting: - MANDATORY for all function signatures and class attributes.
- •Use modern syntax:
str | Noneinstead ofOptional[str],list[int]instead ofList[int]. - •Use
typing.Selffor methods returning the instance.
- •Use modern syntax:
- •Path Handling:
- •NEVER use
os.path.join. ALWAYS usepathlib.Path.
- •NEVER use
- •Structure:
- •Use
pydantic.BaseModelor@dataclassfor data structures; avoid raw dictionaries. - •Use
if __name__ == "__main__":blocks for script execution.
- •Use
- •Error Handling:
- •Fail fast. Validate inputs immediately.
- •Create custom exception classes for domain-specific errors.
CONSTRAINTS
- •Do not use
passplaceholders; write full implementation. - •Do not use
print()for logging; use theloggingmodule orstructlog. - •Do not use global variables.
EXAMPLES
<example> Input: "Write a function to read a text file." Output: ```python from pathlib import Pathdef read_file_content(file_path: Path) -> str: """Reads text from a path safely.""" if not file_path.exists(): raise FileNotFoundError(f"File not found: {file_path}")
code
with file_path.open("r", encoding="utf-8") as f:
return f.read()