skillfed

Python Best Practices Type Safety

This skill diagnoses and resolves Python type checking errors by categorizing them into patterns—missing annotations, type mismatches, Optional handling, generics, and attribute issues—then applies targeted fixes. It works with pyright and mypy output, includes automation scripts for batch corrections, and guides you through verification to maintain strict type safety.

Python Best Practices Type Safety fixes pyright and mypy type errors through systematic categorization and proven fix templates.

AI-generated summary based on this skill's SKILL.md

1 0 unlicensed — metadata only updated by dawiddutoit

Install

dawiddutoit/custom-claude/python-best-practices-type-safety · repository language: Python

git clone https://github.com/dawiddutoit/custom-claude
cp -r custom-claude ~/.claude/skills/python-best-practices-type-safety

generated, unverified - the skill's exact subdirectory could not be determined; check the repository on GitHub

npx skillfed install dawiddutoit/custom-claude/python-best-practices-type-safety

Frequently asked questions

AI-generated answers based on this skill's SKILL.md and metadata

How do I add types to Python code effectively?

Python Best Practices Type Safety teaches you to use type hints and annotations throughout your codebase. Start by annotating function parameters and return types using the typing module—for example, `def greet(name: str) -> str:`. For variables, use inline annotations like `count: int = 0`. The skill covers Optional types for nullable values, generics for collections, and patterns for complex scenarios. Type annotations are optional at runtime but enable static type checkers to catch errors before execution.

What is static type checking python and how do I set it up?

Python Best Practices Type Safety explains static type checking—analyzing your code without running it to find type errors early. Tools like mypy and pyright scan your annotated code and report mismatches. To set up: install mypy (`pip install mypy`), annotate your code with type hints, then run `mypy your_file.py`. The skill guides configuration via `mypy.ini` or `pyproject.toml`, integration with CI/CD pipelines, and interpreting error messages to fix issues systematically.

What are python type hints and why use them?

Python Best Practices Type Safety covers type hints—annotations that declare expected types for variables, parameters, and return values. They improve code clarity, enable IDE autocompletion, and let static checkers catch bugs like passing a string where an integer is expected. Hints don't enforce types at runtime but document intent. The skill shows practical examples: `def add(x: int, y: int) -> int:` and `users: list[str] = []`, demonstrating how hints make code safer and more maintainable.

How do I enforce type safety in Python projects?

Python Best Practices Type Safety outlines enforcement strategies: add comprehensive type annotations to all functions and module-level variables, configure mypy or pyright with strict settings, integrate type checking into your CI/CD pipeline to block non-compliant commits, and use pre-commit hooks for local validation. The skill addresses common patterns—Optional handling, generics for collections, and attribute issues—and provides automation scripts for batch corrections across large codebases.

What are python typing best practices and patterns?

Python Best Practices Type Safety teaches key patterns: use `Optional[T]` or `T | None` for nullable values, leverage generics like `list[User]` for type-safe collections, define TypedDict for structured dictionaries, and use Protocol for duck typing. Avoid `Any` when possible; prefer Union types for multiple valid types. The skill emphasizes gradual adoption—start with public APIs and critical paths—and shows how to handle third-party libraries lacking annotations using type stubs or `# type: ignore` comments judiciously.

How does mypy python type checking work?

Python Best Practices Type Safety explains mypy as a static type checker that reads your annotated code and configuration, then reports type errors without executing. It infers types from assignments, validates function calls against signatures, and checks attribute access. Run `mypy your_file.py` to scan; mypy outputs line numbers and error descriptions. The skill covers mypy configuration options (strict mode, ignore patterns), integration with editors for real-time feedback, and interpreting common errors like type mismatches and missing Optional handling.

Related skills

Tags

static-analysis code-quality type-annotations mypy-integration runtime-safety development-workflow python-tooling error-prevention