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
Decision gist · record as of 2026-01-26
Python Best Practices Type Safety fixes pyright and mypy type errors through systematic categorization and proven fix templates. 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.
Use it when
- Python Best Practices Type Safety explains static type checking—analyzing your code without running it to find type errors early.
- Python Best Practices Type Safety covers type hints—annotations that declare expected types for variables, parameters, and return values.
Install
dawiddutoit/custom-claude/python-best-practices-type-safety · repository language: Python
generated, unverified - the skill's exact subdirectory could not be determined; check the repository on GitHub
Open directory. Skills are indexed for reading, not audited. Review a skill's body before installing it.
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.
Let your AI agent find skills like this
Example. Real query, live index.
You found this page by searching. An agent finds it by wishing: SkillFed indexes 56,283 agent skills by what they can do, searchable in plain language.
wish › “Learn type hints and annotations for safer Python code”
Give your agent the search over MCP, or paste the wish link into any chat. No install? Search from any chat →
Related skills
Diagnose and resolve ServiceResult pattern mistakes that cause test failures and type errors. This skill fixes mock configuration problems (like returning dicts instead of ServiceResult objects), async/await pattern issues, and improper unwrap operations in Python service implementations.
Quality Run Type Checking combines pyright for rapid development feedback with mypy for comprehensive pre-commit validation. Both tools catch complementary error classes—pyright excels at speed and IDE integration, while mypy provides deeper Pydantic model support and CI/CD authority. Use pyright during coding iterations and mypy before commits.
Learn to use ty, Astral's high-performance Rust-based type checker for Python. This skill covers adding type annotations, resolving type errors, configuring rules, and migrating from mypy or pyright. Explore advanced patterns like intersection types and protocols, plus editor integration for VS Code, Cursor, Neovim, and PyCharm.
This skill automates Definition of Done enforcement by detecting your project type and running all quality gates—type checking, linting, dead code detection, and tests—in sequence. It captures failures by category, provides specific fixes, and re-runs gates until everything passes, ensuring code truly meets your standards before task completion.
Add type annotations to test functions and fixtures, then configure mypy to validate them during test runs. Use create_autospec for type-safe mocks that enforce interface contracts and catch typos at runtime.
Python Pro is a specialist for building modern Python 3.11+ applications with complete type coverage, async-first design, and robust error handling. It generates type-annotated code, configures strict mypy validation, creates comprehensive pytest suites, and enforces code quality with black and ruff.