python-tighten-types

Reviews Python source files and tightens type annotations with Pydantic models and overloads.

1|2|Updated Nov 25, 2017
One-click install
npx skills add https://github.com/asarchami/dotfiles --skill python-tighten-types-asarchami
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-tighten-types
Source: https://github.com/asarchami/dotfiles/tree/main/dot_config/opencode/skills/python/tighten-types
Command: npx skills add https://github.com/asarchami/dotfiles --skill python-tighten-types-asarchami

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Python codebases often accumulate weak or missing type annotations—untyped class attributes, loosely-typed dicts passed between functions, and vague union return types—that reduce type checker effectiveness and hide bugs. ## Core Features & Use Cases - Systematic Annotation Audit: Surveys Python files, finds missing class attribute annotations, redundant in-body annotations, and outdated typing styles like Optional[X]. - Structured Dict Conversion: Detects dicts with consistent key shapes and converts them to Pydantic BaseModels or TypedDicts depending on whether they cross system boundaries. - Precision Typing: Adds @overload signatures for narrowable unions and imports concrete third-party types (FastAPI, PyTorch, numpy) via TYPE_CHECKING guards. - Use Case: Point it at a legacy module where functions pass around loosely-typed config dicts; it proposes TypedDicts or Pydantic models, modernizes syntax, then runs the type checker to verify no new errors. ## Quick Start Ask the agent to tighten the type annotations in the Python files of the current project or a specific module.

Frequently Asked Questions about python-tighten-types

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

FAQPage Schema
How do I add missing type annotations to a Python class?

Declare attribute types at class level, such as name: str or _cache: dict[str, Any], then annotate __init__ parameters and add a -> None return type. This skill surveys each file first, then applies these annotations systematically.

When should I use Pydantic BaseModel vs TypedDict for structured dicts?

Prefer BaseModel when the dict crosses a system boundary such as an API, config file, or serialization layer, since it adds runtime validation. Prefer TypedDict for internal data structures that are never validated or serialized.

How do I type a function that returns str or bytes depending on a flag?

Use @overload with Literal types: one overload for as_bytes: Literal[False] returning str, another for Literal[True] returning bytes, followed by the implementation signature with the union return type.

Does tightening type annotations change runtime behavior?

Pure annotations do not, but converting a dict to a Pydantic model does change runtime behavior and can break public API compatibility. The skill flags these cases and asks before making breaking changes.

How do I avoid circular imports when adding type annotations?

Use from __future__ import annotations combined with a TYPE_CHECKING guard block for imports only needed by the type checker. This keeps third-party types like spacy's Language available for checking without runtime import cycles.