python
Always use this skill when writing or editing python code!
When & Why to Use This Skill
This Claude skill provides a robust framework for Python development, enforcing strict adherence to modern coding standards, type safety, and efficient architectural patterns. It streamlines the process of writing and editing Python code by automating formatting, mandating type hints, and optimizing error handling, resulting in cleaner, more reliable, and highly maintainable software.
Use Cases
- Automated Code Formatting and Linting: Ensuring Python code follows PEP 8 standards using black and isort for professional readability and consistency.
- Type-Safe Development: Leveraging comprehensive type hints and mypy integration to reduce runtime errors and improve code documentation.
- Modern Data Validation: Implementing the latest Pydantic field validators and Annotated types for robust and efficient data modeling.
- Clean Architecture Refactoring: Optimizing code structure by extracting logic into static/class methods and eliminating redundant getters and setters.
- Dependency Management: Streamlining the setup of local development environments and managing requirements.txt without version conflicts.
| name | python |
|---|---|
| description | Always use this skill when writing or editing python code! |
python
- Keep lines short and avoid horizontal alignment.
- isort and black (unless the project specifies otherwise).
- Install them with pip if not already installed.
- Avoid try except Exception that swallows errors.
- Prefer logging, re-raising it, or handling specific exceptions.
- Always add type hints to code; especially anything non-obvious.
- Prefer
|overUnion[]and| NoneoverOptional. - Run a type checker (e.g.:
mypy) and fix all errors after writing the code.
- Prefer
- Whenever possible, extract logic out to staticmethods and classmethods.
- Don't return multiline statements; prefer assigning then returning.
- When using pydantic, prefer the new validators over the older
@validatordecorators.- e.g.:
number: Annotated[int, AfterValidator(is_even)]
- e.g.:
- Don't write useless getters and setters such as:
def get_my_items(self) -> list[MyItem]: return self.my_items- Access the attributes directly.
- Don't setup
loggingin libraries; leave that to the application using the library.
dependencies
- Prefer adding dependencies without version constraints unless absolutely necessary.
- Never try except missing imports; assume users of our code will install all needed dependencies.
- Add them to requirements.txt or equivalent.
- Also, imports should always be at the top of the file.
- When installing packages for source code that's local, use
pip install -e ./path.- So we don't confuse ourselves editing local code and running things from site-packages.