Skip to content
Back to skills

Python Best Practices

ASecurity

Python 3.13+ conventions for typed application and library boundaries. Use when writing or reviewing Python contracts, packages, validation, or async resource ownership.

  • 14 stars
  • 0 votes
  • 1 copy
  • 19 views
  • Added September 6, 2026
developmentpythonrustgoexpressgit

Works with

  • cli

Security analysis

A100/100

Pro scans all 19 files and shows the line behind each finding

Scanned November 15, 2026

npx -y skills add Casper-Studios/casper-marketplace --skill python-best-practices --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Python Best Practices?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Python Best Practices
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/casper-studios-python-best-practices/badge)](https://www.skillsdirectory.com/skills/casper-studios-python-best-practices)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: python-best-practices
description: Python 3.13+ conventions for typed application and library boundaries. Use when writing or reviewing Python contracts, packages, validation, or async resource ownership.
license: MPL-2.0
metadata:
  author: 'Basti Ortiz <ortiz@bastidood.dev>'
  source: 'https://github.com/BastiDood/skills'
---

# Python Best Practices

> [!IMPORTANT]
> Apply the `codebase-architecture:code-style` principles through modern Python mechanisms without annotation or packaging ceremony: favor inference, precise structural contracts, explicit runtime validation, side-effect-free imports, and context-managed resource ownership.

## Effective Strategies for Python

Read the linked guidance that governs the current task before writing or reviewing Python.

1. Make type contracts precise without annotation ceremony.
   - Let the checker infer obvious implementation results; add [annotations](./references/inference-and-annotations.md) only when inference cannot express the contract.
   - Depend on the smallest required behavior with [protocols](./references/protocols.md), not a vendor's concrete client.
   - In Python 3.13+, express caller-relevant relationships with [PEP 695 generics](./references/pep-695-generics.md), avoiding module-level `TypeVar` declarations and needless generics.
   - Make a new finite-state member a type-checking failure with [exhaustive handling](./references/finite-state-exhaustiveness.md).
   - Narrow [optional values](./references/optional-narrowing.md) before use so the owning layer—not a cast or fabricated fallback—decides whether absence is preserved or rejected.
2. Validate untrusted data and make invalid states explicit.
   - Parse serialized input once at the controlled [boundary](./references/boundary-validation.md) so untrusted mappings do not spread inward.
   - Do not turn missing required data into a plausible value; [preserve absence or fail](./references/no-fabricated-defaults.md) at the owning boundary.
   - Keep required runtime checks active under optimization; [do not use assertions](./references/no-runtime-asserts.md) for validation.
   - Catch only expected exceptions at the operation that raises them through [narrow exception handling](./references/exception-handling.md).
3. Keep packages explicit, inert, and independently distributable.
   - Keep imports inert and consumer-facing names deliberate with [explicit public exports](./references/public-exports.md).
   - Prevent checkout-dependent imports by following the [Python naming and `src` layout](./references/naming-and-src-layout.md).
   - Publish intentional inline types with [`py.typed`, reserving third-party stubs](./references/py-typed-and-stubs.md) for dependencies that lack complete inline types instead of adding markers merely to silence diagnostics.
   - Give every independently buildable distribution its own declared [metadata](./references/project-metadata.md) and direct dependencies.
   - Make packaging errors visible by [not mutating import paths](./references/no-import-path-mutation.md).
4. Keep asynchronous resource and traversal ownership visible.
   - Make async cleanup inseparable from acquisition with [async context managers](./references/async-context-managers.md).
   - Preserve page boundaries and consumer control over remote traversal through [async pagination](./references/async-pagination.md).

Files in this skill

  • LICENSE.txt16.3 KB
  • SKILL.md3.3 KB
  • agents/openai.yaml268 B
  • references/async-context-managers.md1.1 KB
  • references/async-pagination.md1.3 KB
  • references/boundary-validation.md767 B
  • references/exception-handling.md899 B
  • references/finite-state-exhaustiveness.md968 B
  • references/inference-and-annotations.md1.2 KB
  • references/naming-and-src-layout.md679 B
  • references/no-fabricated-defaults.md644 B
  • references/no-import-path-mutation.md718 B
  • references/no-runtime-asserts.md675 B
  • references/optional-narrowing.md795 B
  • references/pep-695-generics.md557 B
  • references/project-metadata.md652 B
  • references/protocols.md992 B
  • references/public-exports.md743 B
  • references/py-typed-and-stubs.md646 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.