agentleFS
pauloribeiro16pauloribeiro16/my-skills/python-best-practices/SKILL.md

python-best-practices

Skill1 starsChanged 6 months agoMITView on GitHub

Expert guidance for Python best practices: PEP 8, type hints, testing, error handling, async patterns, modern tooling. Use when writing or refactoring Python code.

19 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use react-best-practices, typescript-best-practices and 1 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • OpenAISkill

    react-best-practices

    React and Next.js performance optimization guidelines from Vercel Engineering. This skill should be used when writing, reviewing, or refactoring React/Next.js code to ensure optimal performance patterns. Triggers on tasks involving React components, Next.js pages, data fetching, bundle optimization, or performance improvements.

    Also in 3 projects that use this

  • jaydubya818Skill

    typescript-best-practices

    Use when TypeScript needs focused type-safety, API, or error-handling guidance.

    Also in 3 projects that use this

  • GoogleSkill

    tdd

    Test-driven development. Use when the user wants to build features or fix bugs test-first, mentions "red-green-refactor", or wants integration tests.

    Also in 3 projects that use this

The file

Show its rules and text

We split this skill into the rules it gives an agent

23 rules. 0 are common, 12 are distinctive, and 11 are very specific to this project.

  • 0

    Common

    Most of the 15 edited versions we read keep these.

  • 12

    Distinctive

    This author's own choices. Worth a look.

  • 11

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Write idiomatic, maintainable, and production-ready Python code following modern standards and type-safe patterns.

    line 8
  2. Distinctive:

    When writing or reviewing Python code, follow this checklist:

    line 12
  3. Very specific:

    Structure: Use src/ layout with pyproject.toml (PEP 621)

    line 14
  4. Distinctive:

    Style: Follow PEP 8 -- Ruff handles formatting automatically

    line 15
  5. Distinctive:

    Types: Add type hints to all function signatures (PEP 484)

    line 16
  6. Distinctive:

    Tests: Write tests with pytest before implementation (TDD)

    line 17
  7. Distinctive:

    Errors: Use specific exceptions and context managers

    line 18
  8. Very specific:

    Tools: Run ruff check, mypy, and pytest before committing

    line 19
  9. Very specific:

    No wildcard imports (from module import *)

    line 29
  10. Distinctive:

    All function signatures must have type hints

    line 33
  11. Very specific:

    Use list[X] not List[X] (Python 3.9+)

    line 34
  12. Very specific:

    Use TypedDict for structured dicts, dataclasses or Pydantic for data models

    line 36
  13. Very specific:

    Run mypy --strict to catch type errors

    line 37
  14. Distinctive:

    Write self-documenting code: descriptive names over comments

    line 41
  15. Very specific:

    Prefer explicit over implicit -- no magic values, no bare except:

    line 43
  16. Very specific:

    Use specific exception classes, never bare except:

    line 47
  17. Distinctive:

    Write tests first, then implement, then refactor

    line 54
  18. Distinctive:

    Use pytest with descriptive test function names

    line 55
  19. Very specific:

    Mutable default arguments: Use None and check inside the function

    line 78
  20. Very specific:

    Bare except clauses: Always specify Exception or a concrete type

    line 79
  21. Distinctive:

    Over-abstracting: Don't add interfaces/ABCs until you have at least two implementations

    line 81
  22. Distinctive:

    Mixing sync and async: Don't call async code from sync without an event loop

    line 82
  23. Very specific:

    Neglecting __pycache__: Add to .gitignore

    line 83

“Common” is measured: kept by at least 6 in 10 of the 15 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Python Best Practices

Write idiomatic, maintainable, and production-ready Python code following modern standards and type-safe patterns.

Quick Start

When writing or reviewing Python code, follow this checklist:

  1. Structure: Use src/ layout with pyproject.toml (PEP 621)
  2. Style: Follow PEP 8 -- Ruff handles formatting automatically
  3. Types: Add type hints to all function signatures (PEP 484)
  4. Tests: Write tests with pytest before implementation (TDD)
  5. Errors: Use specific exceptions and context managers
  6. Tools: Run ruff check, mypy, and pytest before committing

Core Principles

1. PEP 8 -- Style Guide

  • 4 spaces per indentation level (never tabs)
  • 88 char line length (Ruff default), 72 for docstrings
  • snake_case for functions/variables, PascalCase for classes, UPPER_CASE for constants
  • Imports: stdlib, third-party, local (each group separated by blank line)
  • No wildcard imports (from module import *)

2. Type Hints (PEP 484 / 585 / 604)

  • All function signatures must have type hints
  • Use list[X] not List[X] (Python 3.9+)
  • Use X | None not Optional[X] (Python 3.10+)
  • Use TypedDict for structured dicts, dataclasses or Pydantic for data models
  • Run mypy --strict to catch type errors

3. Readability and Clarity

  • Write self-documenting code: descriptive names over comments
  • Docstrings on all public functions (Args, Returns, Raises)
  • Prefer explicit over implicit -- no magic values, no bare except:

4. Error Handling

  • Use specific exception classes, never bare except:
  • Context managers for resource management (with blocks)
  • Log exceptions with logging.exception() or logger.exception()
  • Define custom exception hierarchies for domain errors

5. Testing (TDD)

  • Write tests first, then implement, then refactor
  • Use pytest with descriptive test function names
  • Parametrize tests with @pytest.mark.parametrize
  • One assertion per test where possible
  • Aim for mutation testing coverage (see references/TESTING.md)

6. Modern Tooling

  • uv for package management (replaces pip + venv + pip-tools)
  • Ruff for linting + formatting (replaces flake8 + isort + black)
  • MyPy --strict for type checking
  • pytest for testing
  • pre-commit / prek for git hooks

References

FileWhen to read
references/TOOLING.mdWhen setting up a new Python project. Complete tool configuration (pyproject.toml, Ruff, MyPy, pytest, uv).
references/PATTERNS.mdWhen writing Python code. Patterns for dataclasses, Pydantic, async/await, context managers, logging, CLI.
references/TESTING.mdWhen writing or improving tests. pytest, Hypothesis, mocking, async testing, mutation testing.

Common Pitfalls

  • Mutable default arguments: Use None and check inside the function
  • Bare except clauses: Always specify Exception or a concrete type
  • Ignoring type hints: Any should be a last resort, not the default
  • Over-abstracting: Don't add interfaces/ABCs until you have at least two implementations
  • Mixing sync and async: Don't call async code from sync without an event loop
  • Neglecting __pycache__: Add to .gitignore

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.