type-safety

Enforce comprehensive type hints in Python code with mypy-compatible signatures.

2|Updated Nov 13, 2025
One-click install
npx skills add https://github.com/ricardoroche/ricardos-claude-code --skill type-safety-ricardoroche
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: type-safety
Source: https://github.com/ricardoroche/ricardos-claude-code/tree/main/.claude/skills/type-safety
Command: npx skills add https://github.com/ricardoroche/ricardos-claude-code --skill type-safety-ricardoroche

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill enforces comprehensive type hinting in Python code, addressing issues like runtime type errors, unclear function signatures, and poor IDE support. It significantly improves code quality, maintainability, and makes debugging easier by catching type-related bugs early.

Core Features & Use Cases

  • Function & Class Type Hints: Guides on adding type hints to function parameters, return values, class attributes, and methods.
  • Generic & Collection Types: Provides patterns for using generic types (TypeVar, Generic) and modern collection type hints (list[T], dict[K,V]).
  • Optional & None Handling: Emphasizes correct usage of Optional[T] and explicit None checks to prevent NoneType errors.
  • Advanced Typing: Covers TypeAlias, NewType, Protocol, Literal, and TypeGuard for expressing complex type relationships.
  • Mypy Integration: Recommends a strict mypy configuration to ensure full type safety.
  • Use Case: A developer is refactoring a legacy Python codebase. This skill helps them systematically add type hints to functions and classes, enabling mypy to catch potential type errors before runtime, improving code reliability.

Quick Start

Add comprehensive type hints to the process_transaction function, including parameters, return type, and optional metadata.

Frequently Asked Questions about type-safety

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

FAQPage Schema
How do I add type hints to Python functions to catch errors early?

Type hints specify parameter and return types in function signatures. Add them using syntax like `def process(data: str) -> dict:` to enable static analysis tools like mypy to detect type mismatches before runtime, improving code reliability and IDE support.

What's the difference between Optional and Union types in Python typing?

Optional[T] is shorthand for Union[T, None] and explicitly marks a value that may be None. Use Optional for nullable fields and Union when a value can be one of several types. Both prevent NoneType errors by forcing explicit None checks.

How do I use generics and TypeVar for flexible, type-safe code?

TypeVar and Generic enable you to write reusable functions and classes that work with multiple types while preserving type safety. Define `T = TypeVar('T')` and use it in signatures like `def reverse(items: list[T]) -> list[T]` so mypy validates type consistency.

Why should I configure mypy strictly for Python projects?

Strict mypy configuration enforces comprehensive type checking across all code paths, catching edge cases like implicit Optional values and unannotated parameters. It prevents type-related bugs from reaching production and makes refactoring safer.

Can I use advanced typing features like Protocol and Literal for complex types?

Protocol defines structural types for duck typing, Literal restricts values to specific constants, and TypeAlias names complex types for clarity. These advanced features express precise type relationships that mypy validates, reducing runtime surprises in complex codebases.

What's the best way to handle type hints in legacy Python code?

Add type hints incrementally, starting with public APIs and data models. Use mypy's gradual typing to monitor progress without rewriting everything at once. Focus on high-risk functions where type errors cause bugs, building confidence before expanding coverage.