python-type-safety

Implement Python type hints, generics, protocols, and strict mypy or pyright checking.

Updated Apr 13, 2026
One-click install
npx skills add https://github.com/scoots31/engineering-playbook --skill python-type-safety-scoots31
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-type-safety
Source: https://github.com/scoots31/engineering-playbook/tree/main/references/python-type-safety
Command: npx skills add https://github.com/scoots31/engineering-playbook --skill python-type-safety-scoots31

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Untyped Python code lets type errors slip through to runtime, causing bugs that only surface in production. This Skill provides patterns for annotating code so static type checkers catch errors before execution. ## Core Features & Use Cases - Type Annotations: Annotate function signatures, return values, and class attributes using modern union syntax like User | None. - Generics and Protocols: Build reusable generic classes with TypeVar bounds and define structural interfaces with Protocol instead of inheritance. - Strict Type Checking: Configure mypy or pyright strict mode in pyproject.toml, with incremental per-module adoption for existing codebases. - Use Case: When refactoring a data access layer, apply the generic Repository pattern so Repository[User, str] preserves entity types across get, save, and delete operations, and let the type checker verify every call site. ## Quick Start Add type annotations to my Python module and configure mypy strict mode to catch type errors in CI.

Frequently Asked Questions about python-type-safety

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add type hints to existing Python code?

Annotate all public function parameters, return types, and class attributes first, then run mypy or pyright to find gaps. For existing projects, enable strict mode incrementally using per-module overrides in pyproject.toml rather than converting everything at once.

What is the difference between Protocol and abstract base classes in Python?

Protocols define structural interfaces through duck typing, so a class satisfies them without explicit inheritance. Abstract base classes require explicit subclassing. Use Protocol when you want type-safe interfaces for classes you do not control.

Should I use Optional or the new union syntax in Python?

Python 3.10+ supports the cleaner `User | None` union syntax, which is preferred for new code. The older `Optional[User]` from the typing module remains valid and is required when targeting Python 3.9 or earlier.

Does mypy strict mode work with existing untyped codebases?

Yes, through incremental adoption. Enable strict mode per-module using `# mypy: strict` comments or per-module overrides in pyproject.toml, then expand coverage gradually instead of fixing the entire codebase at once.

When should I use TypeVar with bounds in Python generics?

Use bounded TypeVars like `TypeVar("ModelT", bound=BaseModel)` when a generic function or class only works with specific types. This lets the type checker reject invalid arguments, such as passing str where a BaseModel subclass is required.