<< All versions
Skill v1.0.0
currentAutomated scan100/100asafelobotomy/copilot-instructions-template/python-typing
──Details
PublishedApril 28, 2026 at 02:22 AM
Content Hashsha256:5e861a6565deda7b...
Git SHA335fbd3ef6ad
──Files
Files (1 file, 3.0 KB)
SKILL.md3.0 KBactive
SKILL.md · 132 lines · 3.0 KB
version: "1.0.0" name: python-typing description: Apply Python type annotations effectively — mypy/pyright configuration, common patterns, generic types, and stub guidance compatibility: ">=1.4"
Python Typing
Skill metadata: version "1.0"; license MIT; tags [python, typing, mypy, pyright, type-checking]; recommended tools [codebase, runCommands, editFiles].
When to use
- Adding or reviewing type annotations in Python code
- Configuring mypy or pyright for a project
- Resolving type checker errors
- Writing or consuming type stubs
Core principles
- Annotate function signatures (parameters and return types) at minimum.
- Use
from __future__ import annotationsfor forward references (Python 3.7+). - Prefer built-in generics (
list[str],dict[str, int]) overtyping.List,typing.Dict(Python 3.9+). - Use
X | Noneinstead ofOptional[X](Python 3.10+).
Common patterns
Function signatures
python
def process_items(items: list[str], limit: int = 10) -> dict[str, int]:...
Union and optional
python
# Python 3.10+def find_user(user_id: int) -> User | None:...# Python 3.7–3.9from __future__ import annotationsdef find_user(user_id: int) -> User | None:...
TypedDict for structured dictionaries
python
from typing import TypedDictclass UserProfile(TypedDict):name: stremail: strage: int | None
Protocol for structural subtyping
python
from typing import Protocolclass Serializable(Protocol):def to_json(self) -> str: ...
Generics
python
from typing import TypeVarT = TypeVar("T")def first(items: list[T]) -> T | None:return items[0] if items else None
Configuration
mypy in pyproject.toml
toml
[tool.mypy]python_version = "3.12"strict = truewarn_return_any = truewarn_unused_configs = truedisallow_untyped_defs = true[[tool.mypy.overrides]]module = "tests.*"disallow_untyped_defs = false
pyright in pyproject.toml
toml
[tool.pyright]pythonVersion = "3.12"typeCheckingMode = "standard"reportMissingTypeStubs = "warning"
Type stubs
- Install stubs from
types-*packages on PyPI (e.g.,types-requests). - Create
py.typedmarker file to mark a package as typed. - For internal stubs, place
.pyifiles alongside source files.
Common pitfalls
- Annotating
selfandcls— not needed (mypy and pyright infer them). - Using
Any— minimize its use; it defeats the purpose of type checking. - Mutable default arguments — use
Noneas default and assign in the body. - Circular imports for type checking — use
TYPE_CHECKINGguard:
python
from __future__ import annotationsfrom typing import TYPE_CHECKINGif TYPE_CHECKING:from .models import User
Verify
- [ ] Public APIs have explicit parameter and return annotations
- [ ] Type checker config exists and runs in CI or local verification flow
- [ ]
Anyusage is intentional and justified - [ ] Stub strategy (
types-*,.pyi,py.typed) matches dependency reality