Best practices for Python PEP8 and Type Hinting
.cursorrules in your project root# Python PEP8 and Type Hinting Rules ===================================== ## Core Principles ------------------ * All Python code must adhere to the official Python style guide, [PEP 8](https://www.python.org/dev/peps/pep-0008/). * Type hinting must be used for all function parameters, return types, and variable declarations. * Code must be readable, maintainable, and efficient. ## Code Style Guidelines ----------------------- ### Naming Conventions * Variable names must be in lowercase with words separated by underscores (e.g., `hello_world`). * Function names must be in lowercase with words separated by underscores (e.g., `hello_world`). * Class names must be in CapWords (e.g., `HelloWorld`). * Constant names must be in uppercase with words separated by underscores (e.g., `HELLO_WORLD`). ### Typing * Use type hints for all function parameters and return types (e.g., `def hello_world(name: str) -> None:`). * Use type hints for variable declarations (e.g., `hello_world: str = "Hello, World!"`). * Use the `typing` module for complex types (e.g., `from typing import List, Dict`). ### Spacing and Indentation * Use 4 spaces for indentation. * Use 1 space around operators (e.g., `a = 1 + 2`). * Use 1 blank line between function definitions. * Use 2 blank lines between class definitions. ## Best Practices ----------------- ### Functions * Keep functions short and focused on a single task. * Use descriptive function names and docstrings. * Use type hints for function parameters and return types. ### Classes * Keep classes simple and focused on a single task. * Use descriptive class names and docstrings. * Use type hints for class attributes and methods. ### Error Handling * Use try-except blocks to handle exceptions. * Use descriptive error messages. * Avoid bare `except` clauses. ### Testing * Write unit tests for all functions and classes. * Use a testing framework (e.g., `unittest`). * Test for expected behavior and edge cases. ## Common Pitfalls to Avoid --------------------------- ### Mutable Default Arguments * Avoid using mutable default arguments (e.g., `def hello_world(name: str = [])`). * Instead, use immutable default arguments or None (e.g., `def hello_world(name: str = None)`). ### Shadowing Built-ins * Avoid shadowing built-in functions and variables (e.g., `def len(x: int) -> int:`). * Instead, use descriptive names that do not conflict with built-ins. ### Unused Imports * Avoid unused imports (e.g., `import os` without using `os`). * Remove unused imports to keep code clean and efficient. ### Duplicate Code * Avoid duplicate code (e.g., copying and pasting code). * Instead, extract common code into reusable functions or classes.
Comprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.
Cursor
coding
AI coding rules customize how Cursor generates and refactors code for your project. Follow these steps to install Python PEP8 And Type Hinting.
.cursor/rules, for Windsurf use .windsurfrulesComprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.