type-checking-pattern

Resolve F821 undefined name errors with TYPE_CHECKING blocks and quoted annotations.

3|Updated Dec 10, 2025
One-click install
npx skills add https://github.com/smith6jt-cop/Skills_Registry --skill type-checking-pattern
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: type-checking-pattern
Source: https://github.com/smith6jt-cop/Skills_Registry/tree/main/plugins/general/type-checking-pattern/skills/type-checking-pattern
Command: npx skills add https://github.com/smith6jt-cop/Skills_Registry --skill type-checking-pattern

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill eliminates ruff F821 "undefined name" errors when using type hints for packages that may not be installed.

Core Features & Use Cases

  • TYPE_CHECKING Block: Uses Python's TYPE_CHECKING for static analysis without runtime imports.

Quick Start

Resolve the undefined name error for the optional dependency 'dask' in the current code file.

Quick Start

Fix the F821 undefined name error for the current module's type annotations.

Frequently Asked Questions about type-checking-pattern

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

FAQPage Schema
How do I fix F821 undefined name errors for optional dependencies in Python type hints?

F821 undefined name errors occur when type hints reference packages that may not be installed. Use TYPE_CHECKING blocks to guard optional imports—wrap them in `if TYPE_CHECKING:` so static type checkers see the types while runtime code never imports unavailable packages. Quote annotations as strings to defer evaluation and prevent NameErrors when dependencies are absent.

What is the TYPE_CHECKING pattern and why use it for optional dependencies?

TYPE_CHECKING is a Python constant that's True only during static analysis and False at runtime. It lets you import packages solely for type hints without triggering ImportError if they're not installed. Combining TYPE_CHECKING blocks with quoted type annotations preserves IDE autocompletion and ruff linting while keeping code runtime-safe across environments.

Can I use type hints for packages like dask and pandas that may not be installed?

Yes. Place optional package imports inside `if TYPE_CHECKING:` blocks and quote your annotations as strings. This pattern is safe for packages like dask, pandas, and cupy—static tools resolve the types, but your code never tries to import them at runtime if they're missing, eliminating F821 errors.

How do I set up type hints for optional dependencies in a Python project?

Import TYPE_CHECKING from typing, place optional package imports in `if TYPE_CHECKING:` blocks, and use quoted string annotations for those types in function signatures and variable declarations. This resolves ruff F821 errors and enables IDE autocompletion without requiring optional packages at runtime.

Why does ruff report F821 for type hints when optional packages aren't installed?

Ruff checks that all names in type hints are defined, but unguarded imports of optional packages fail at runtime if the package is absent. TYPE_CHECKING blocks hide these imports from the runtime interpreter while exposing them to static analysis, so ruff and type checkers see the definitions without causing ImportError.

Does this pattern work with IDE autocompletion and static type checkers?

Yes. TYPE_CHECKING blocks combined with quoted annotations preserve type information for IDEs, mypy, pyright, and ruff. Static tools evaluate the guarded imports during analysis, enabling full autocompletion and type checking without requiring optional dependencies to be installed.