0159-python-type-safety

Enforce Python static type correctness with strict mypy or Pyright configurations.

Updated Aug 27, 2026
One-click install
npx skills add https://github.com/MrJmpl3/codex_____data_____configuration --skill 0159-python-type-safety
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: 0159-python-type-safety
Source: https://github.com/MrJmpl3/codex_____data_____configuration/tree/main/skills/0159-python-type-safety
Command: npx skills add https://github.com/MrJmpl3/codex_____data_____configuration --skill 0159-python-type-safety

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It helps you prevent bugs caused by incorrect values, mismatched interfaces, and missing null/union handling by enforcing Python type checking during static analysis.

Core Features & Use Cases

  • Type-safe public APIs: Annotate function signatures, methods, and class interfaces so tools can validate correct usage.
  • Generics, Protocols, and type narrowing: Preserve type information across reusable components using Generic, define structural interfaces with Protocol, and narrow T | None safely with guards.
  • Strict type checking configuration: Use mypy --strict or Pyright with strict rules to surface issues early in CI rather than at runtime.
  • Use Case: When building an internal library or service layer, define repository interfaces and response types so consumers cannot accidentally call methods with the wrong entity type or forget to handle missing results.

Quick Start

Configure your project to run strict mypy or pyright and add type annotations to your public functions, then validate that nullable return types like User | None are handled explicitly.

Frequently Asked Questions about 0159-python-type-safety

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

FAQPage Schema
How do I catch Python type errors before runtime?

Catch Python type errors before runtime by enforcing static type correctness through annotations using mypy --strict or Pyright, applying modern union syntax, type narrowing, and bounded TypeVars to surface issues early in CI rather than at runtime.

What is type narrowing and how does it work with Python protocols?

Type narrowing safely resolves T | None types using guards, while Python protocols define structural interfaces so you can preserve type information across reusable components without requiring explicit inheritance.

How do I configure mypy strict mode or Pyright for CI type checking?

Configure mypy strict mode or Pyright for CI type checking by enabling strict rules and adding type annotations to public function signatures, methods, and class interfaces to validate correct usage and surface issues early.

Does mypy strict mode support generics and bounded TypeVars?

Yes, mypy strict mode supports generics and bounded TypeVars, allowing you to preserve type information across reusable components by applying Generic and defining structural interfaces with Protocol.

When do I need Python type checking for a service layer?

You need Python type checking for a service layer when building internal libraries to define repository interfaces and response types, ensuring consumers cannot accidentally call methods with wrong entity types or forget to handle missing results.

What is the best way to handle nullable return types like User | None in Python?

The best way to handle nullable return types like User | None in Python is to use modern union syntax and explicit type narrowing with guards, ensuring missing results are handled explicitly during static analysis.