python
Python software engineering principles, coding standards, and best practices. Use when writing Python code, setting up development environments, or following Python project conventions including typing, async patterns, testing, and documentation.
How do I install this agent skill?
npx skills add https://github.com/deerhide/agent_skills --skill pythonIs this agent skill safe to install?
- Gen Agent Trust Hubfail
The skill provides Python engineering guidelines and utility scripts for environment setup, linting, and testing. It installs the Poetry dependency manager from its official source. The utility scripts use dynamic command construction which is common for development tools but presents a minor risk of command injection if misused.
- Socketpass
No alerts
- Snykwarn
Risk: MEDIUM · 1 issue
What does this agent skill do?
Python Software Engineering Principles
Use Python version defined in pyproject.toml when available.
Core Principles
Follow these fundamental principles in all Python code:
- Clean Architecture: Separate concerns and maintain clear boundaries
- Separation of Concerns: Keep business logic separate from infrastructure
- Dependency Injection: Use dependency injection for loose coupling
- Inversion of Control: Let frameworks manage object lifecycle
- Single Responsibility: Each class/function should have one clear purpose
- Least Privilege: Grant minimum necessary permissions and access
Quick Reference
Development Environment
- Use Poetry for dependency management
- Create virtual environment in
.venvdirectory - Activate with
source .venv/bin/activate(Linux/macOS) or.venv\Scripts\activate(Windows) - Run commands with
poetry runto use virtual environment
Code Style
- Maximum line length: 120 characters
- Use Black and Ruff for formatting and linting
- 4 spaces for indentation (no tabs)
- Double quotes for strings
- Follow PEP 8, except where overridden by project configuration
Import Organization
Organize imports in this order (blank lines between groups):
- Standard library imports
- Third-party imports
- Local application/library imports
Sort alphabetically within each group. Use absolute imports from package root.
Naming Conventions
- Classes: PascalCase (e.g.,
AbstractRepository) - Functions/methods: snake_case (e.g.,
get_config) - Constants: UPPER_SNAKE_CASE (e.g.,
DEFAULT_LOGGING_LEVEL) - Private attributes: Leading underscore (e.g.,
_name) - Type variables: Descriptive names with Generic suffix (e.g.,
DocumentGenericType)
Typing
- Always type everything (variables, functions, classes)
- Annotate all function parameters and return types
- Avoid
Anyunless absolutely necessary - Use
TypeVarfor generic types with descriptive names - Use
TypedDictfor structured dictionary data - Use
collections.abctypes for abstract base classes
Async/Await
- Use
async deffor all I/O operations - Prefer async context managers (
async with) - Use
asyncio.TaskGroup(Python 3.11+) for parallel operations - Use
AsyncGeneratorfor async generator functions - Handle exceptions properly, ensuring cleanup
Logging
- Use
structlogfor all logging operations - Get logger using
get_logger()fromstructlog.stdlib - Use appropriate logging levels (DEBUG, INFO, WARNING, ERROR, CRITICAL)
- Use structured logging with context
- Integrate with OpenTelemetry spans when available
Error Handling
- All custom exceptions should extend base exception class
- Use
TypedDictwithUnpackfor exception parameters - Log exceptions and integrate with OpenTelemetry
- Use proper exception chaining when re-raising
Pydantic
- Use Pydantic v2
BaseModelfor all data models - Use
model_validateandmodel_validate_jsonfor validation - Use
model_dumpandmodel_dump_jsonfor serialization - Use
Fieldfor advanced configuration
Testing
- Use pytest for all tests
- Organize tests by functional scope in classes
- Prioritize unit tests over integration tests
- Use
pytest-asynciofor async tests - Use
pytest.mark.parametrizefor multiple scenarios - Use
unittest.mockfor mocking dependencies
Documentation
- Always add docstrings using Google Python docstring format
- Include module-level docstrings
- Document classes, functions, and methods
- Include Args, Returns, and Raises sections
- Add examples for complex functions
Utility Scripts
The skill includes utility scripts for common development tasks:
scripts/setup-dev-environment.sh- Setup Poetry and virtual environmentscripts/format-code.sh- Format and lint code with Ruffscripts/lint-code.sh- Lint code with Ruff and Pylintscripts/type-check.sh- Run type checking with mypyscripts/run-tests.sh- Run tests with various options (coverage, parallel, etc.)scripts/setup-pre-commit.sh- Setup pre-commit hooks
See the detailed documentation below for script usage examples.
Detailed Documentation
For comprehensive details on all topics, see:
- Principles - Core software engineering principles (Clean Architecture, Separation of Concerns, Dependency Injection, etc.)
- Development Environment - Virtual environment setup and Poetry dependency management
- Code Style - Code formatting, import organization, and naming conventions
- Typing - Type annotations, generic types, TypedDict, and collections
- Async Patterns - Async/await patterns, context managers, parallel operations, and async generators
- Logging - Structlog usage, logging levels, structured logging, and OpenTelemetry integration
- Error Handling - Custom exception hierarchy, exception parameters, logging, and chaining
- Pydantic - Pydantic v2 BaseModel usage, field configuration, and validation patterns
- Testing - Test organization, structure, async tests, parametrized tests, and mocking
- Running Tests - Commands for running tests, coverage, parallel execution, and test selection
- Documentation - Google-style docstring format for modules, classes, functions, and complex functions
- Architecture Patterns - Abstract base classes, plugin pattern, dependency injection, repository pattern, and service layer
- File Structure - Module organization, package structure, and
__init__.pypatterns - Pre-commit - Installing, running, and configuring pre-commit hooks
Related Skills
- python-lint - Linting configuration with Ruff, Pylint, Mypy
- python-docstring - Google-style docstring conventions
- python-test - Testing patterns with pytest
- python-architecture - Backend service architecture
- software-architecture - Design principles (SOLID, DDD, Clean Architecture)
- fastapi-factory-utilities - FastAPI microservice utilities
How can the creator link this skill?
Add the canonical catalog link to the repository README so users can inspect current installs and available audits. The publishing guide covers the complete discovery path.
<a href="https://skillzs.dev/skills/deerhide/agent_skills/python">View python on skillZs</a>