python-type-safety

Implement Python type annotations, generics, protocols, and strict mypy/pyright checking.

Updated Apr 23, 2026
One-click install
npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill python-type-safety-sanketadlak
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-type-safety
Source: https://github.com/SanketAdlak/PDMProjectDesign/tree/main/.agents/skills/python-type-safety
Command: npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill python-type-safety-sanketadlak

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 adding type hints, generics, and protocols so static analysis tools like mypy and pyright catch errors before execution. ## Core Features & Use Cases - Type Annotations & Narrowing: Annotate all public function signatures and use guards to narrow union types like User | None within code blocks. - Generics & Protocols: Build reusable generic classes (e.g., Result[T, E], Repository[T, ID]) and define structural interfaces with Protocol without requiring inheritance. - Strict Checker Configuration: Configure mypy --strict or pyright in pyproject.toml, with incremental per-module adoption for existing codebases. - Use Case: When refactoring a data access layer, define a generic Repository[T, ID] abstract class, implement UserRepository(Repository[User, str]), and let the type checker verify every get, save, and delete call across the codebase. ## Quick Start Add type annotations to my Python module and configure mypy strict mode in pyproject.toml.

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.

Should I use Optional[T] or T | None in Python?

Use `T | None` (modern union syntax) for Python 3.10 and later, as it is cleaner and preferred. Use `Optional[T]` from the typing module only when supporting Python 3.9 or earlier.

What is the difference between Protocol and ABC in Python?

Protocol provides structural typing where a class satisfies the interface by matching method signatures without inheriting anything. ABC requires explicit inheritance. Use Protocol for duck-typed interfaces and ABC when you need shared implementation.

Does the type statement syntax work in Python 3.10?

No, the `type Alias = ...` statement (PEP 695) requires Python 3.12. For Python 3.10 and 3.11, use `TypeAlias` from the typing module, e.g., `UserId: TypeAlias = str`.

Why does mypy still complain after I check for None?

Type narrowing only works when the guard is visible to the type checker in the same scope. After `if user is None: raise ...`, mypy narrows the type to the non-None branch, but narrowing is lost across function calls or when mutating captured variables.

When should I use Any instead of a specific type?

Use `Any` only for truly dynamic data or when interfacing with untyped third-party libraries. Prefer specific types, generics, or TypeVar bounds elsewhere, since `Any` disables type checking for that value entirely.