Type Hints for ML Code

Add precise type hints to PyTorch code with Mypy configuration.

Updated Mar 2, 2025
One-click install
npx skills add https://github.com/apassuello/multimodal_insight_engine --skill type-hints-for-ml-code
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Type Hints for ML Code
Source: https://github.com/apassuello/multimodal_insight_engine/tree/main/.claude/skills/ml-type-hints
Command: npx skills add https://github.com/apassuello/multimodal_insight_engine --skill type-hints-for-ml-code

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

ML projects frequently suffer from unclear typing, leading to runtime bugs and maintenance challenges. This skill provides a structured approach for adding precise type hints to ML code, helping developers catch issues earlier and improve readability.

Core Features & Use Cases

  • Public API typing: Annotate functions and classes that form the public interfaces of ML modules.
  • Configuration typing: Use dataclasses or TypeAlias for training and evaluation configs.
  • Torch typing patterns: Provide patterns for tensors, models, optimizers, and HF transformers with safe Any usage.
  • Real-world scenario: Annotate a training step to clarify inputs/outputs and reduce mypy churn.

Quick Start

Start by annotating a simple model forward method with explicit tensor and output types, then extend to a small training loop with typed configurations.

Frequently Asked Questions about Type Hints for ML Code

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

FAQPage Schema
How do I add type hints to PyTorch tensors and models without breaking mypy?

To add type hints to PyTorch code, use practical typing patterns like Type Aliases and Protocols for tensors and models, employing safe Any usage to align with your mypy configuration and reduce churn.

What is the best way to type hint ML training configurations?

The best way to type hint ML training configurations is using dataclasses or TypeAlias, which clarifies training and evaluation configs to catch type-related bugs earlier and improve maintainability.

Does adding static typing to ML code help reduce type-related runtime bugs?

Adding static typing to ML code helps reduce runtime bugs by clarifying public APIs and configuration structures, catching type issues earlier during development rather than at runtime.

How do I type hint a PyTorch training step to clarify inputs and outputs?

To type hint a PyTorch training step, annotate the model's forward method with explicit tensor and output types, then extend typing to the training loop using typed configurations.

When should I use Any versus strict type hints for Hugging Face transformers in ML code?

Use Any for Hugging Face transformers when strict typing causes excessive mypy churn, balancing precise static typing for public APIs with pragmatic Any usage for complex external ML components.