Python type safety with type hints, generics, protocols

Python type safety with type hints, generics, protocols, and strict type checking. Use when adding type annotations, implementing generic classes, defining structural interfaces, or configuring mypy/pyright.
--- name: python-type-safety description: Python type safety with type hints, generics, protocols, and strict type checking. Use when adding type annotations, implementing generic classes, defining structural interfaces, or configuring mypy/pyright. --- # Python Type Safety Leverage Python's type system to catch errors at static analysis time. Type annotations serve as enforced documentation that tooling validates automatically. ## When to Use This Skill - Adding type hints to existing code - Creating generic, reusable classes - Defining structural interfaces with protocols - Configuring mypy or pyright for strict checking - Understanding type narrowing and guards - Building type-safe APIs and libraries
Overview
Leverage Python's type system to catch errors at static analysis time. Type annotations serve as enforced documentation that tooling validates automatically.
Manual install (npx, ZIP)
npx skills add wshobson/agents --skill python-type-safety -g -a claude-codemkdir -p ~/.claude/skills && curl -sL https://codeload.github.com/wshobson/agents/tar.gz/4236bb91f839 \
| tar -xz -C ~/.claude/skills --strip-components=4 agents-4236bb91f839/plugins/python-development/skills/python-type-safetyNew-Item -ItemType Directory -Force "$HOME\.claude\skills" | Out-Null; Invoke-WebRequest "https://m.agentalley.io/zip/5d496fceb8b7575672e9/python-type-safety.zip" -OutFile "$env:TEMP\python-type-safety.zip"; Expand-Archive "$env:TEMP\python-type-safety.zip" -DestinationPath "$HOME\.claude\skills" -ForceConnects with Agent Alley MCP · agentalley.io/mcp
Files included
SKILL.md5.6 KBreferences/1 file · 5.7 KB
Read the files here
Show the full text201 lines
Python Type Safety
Leverage Python's type system to catch errors at static analysis time. Type annotations serve as enforced documentation that tooling validates automatically.
When to Use This Skill
- Adding type hints to existing code
- Creating generic, reusable classes
- Defining structural interfaces with protocols
- Configuring mypy or pyright for strict checking
- Understanding type narrowing and guards
- Building type-safe APIs and libraries
Core Concepts
1. Type Annotations
Declare expected types for function parameters, return values, and variables.
2. Generics
Write reusable code that preserves type information across different types.
3. Protocols
Define structural interfaces without inheritance (duck typing with type safety).
4. Type Narrowing
Use guards and conditionals to narrow types within code blocks.
Quick Start
def get_user(user_id: str) -> User | None:
"""Return type makes 'might not exist' explicit."""
...
# Type checker enforces handling None case
user = get_user("123")
if user is None:
raise UserNotFoundError("123")
print(user.name) # Type checker knows user is User here
Fundamental Patterns
Pattern 1: Annotate All Public Signatures
Every public function, method, and class should have type annotations.
def get_user(user_id: str) -> User:
"""Retrieve user by ID."""
...
def process_batch(
items: list[Item],
max_workers: int = 4,
) -> BatchResult[ProcessedItem]:
"""Process items concurrently."""
...
class UserRepository:
def __init__(self, db: Database) -> None:
self._db = db
async def find_by_id(self, user_id: str) -> User | None:
"""Return User if found, None otherwise."""
...
async def find_by_email(self, email: str) -> User | None:
...
async def save(self, user: User) -> User:
"""Save and return user with generated ID."""
...
Use mypy --strict or pyright in CI to catch type errors early. For existing projects, enable strict mode incrementally using per-module overrides.
Pattern 2: Use Modern Union Syntax
Python 3.10+ provides cleaner union syntax.
# Preferred (3.10+)
def find_user(user_id: str) -> User | None:
...
def parse_value(v: str) -> int | float | str:
...
# Older style (still valid, needed for 3.9)
from typing import Optional, Union
def find_user(user_id: str) -> Optional[User]:
...
Pattern 3: Type Narrowing with Guards
Use conditionals to narrow types for the type checker.
def process_user(user_id: str) -> UserData:
user = find_user(user_id)
if user is None:
raise UserNotFoundError(f"User {user_id} not found")
# Type checker knows user is User here, not User | None
return UserData(
name=user.name,
email=user.email,
)
def process_items(items: list[Item | None]) -> list[ProcessedItem]:
# Filter and narrow types
valid_items = [item for item in items if item is not None]
# valid_items is now list[Item]
return [process(item) for item in valid_items]
Pattern 4: Generic Classes
Create type-safe reusable containers.
from typing import TypeVar, Generic
T = TypeVar("T")
E = TypeVar("E", bound=Exception)
class Result(Generic[T, E]):
"""Represents either a success value or an error."""
def __init__(
self,
value: T | None = None,
error: E | None = None,
) -> None:
if (value is None) == (error is None):
raise ValueError("Exactly one of value or error must be set")
self._value = value
self._error = error
@property
def is_success(self) -> bool:
return self._error is None
@property
def is_failure(self) -> bool:
return self._error is not None
def unwrap(self) -> T:
"""Get value or raise the error."""
if self._error is not None:
raise self._error
return self._value # type: ignore[return-value]
def unwrap_or(self, default: T) -> T:
"""Get value or return default."""
if self._error is not None:
return default
return self._value # type: ignore[return-value]
# Usage preserves types
def parse_config(path: str) -> Result[Config, ConfigError]:
try:
return Result(value=Config.from_file(path))
except ConfigError as e:
return Result(error=e)
result = parse_config("config.yaml")
if result.is_success:
config = result.unwrap() # Type: Config
Detailed worked examples and patterns
Detailed sections (starting with ## Advanced Patterns) live in references/details.md. Read that file when the navigation summary above is insufficient.
Best Practices Summary
- Annotate all public APIs - Functions, methods, class attributes
- Use
T | None- Modern union syntax overOptional[T] - Run strict type checking -
mypy --strictin CI - Use generics - Preserve type info in reusable code
- Define protocols - Structural typing for interfaces
- Narrow types - Use guards to help the type checker
- Bound type vars - Restrict generics to meaningful types
- Create type aliases - Meaningful names for complex types
- Minimize
Any- Use specific types or generics.Anyis acceptable for truly dynamic data or when interfacing with untyped third-party code - Document with types - Types are enforceable documentation
| 1 | |
| 2 | name python-type-safety |
| 3 | description Python type safety with type hints, generics, protocols, and strict type checking. Use when adding type annotations, implementing generic classes, defining structural interfaces, or configuring mypy/pyright. |
| 4 | |
| 5 | |
| 6 | # Python Type Safety |
| 7 | |
| 8 | Leverage Python's type system to catch errors at static analysis time. Type annotations serve as enforced documentation that tooling validates automatically. |
| 9 | |
| 10 | ## When to Use This Skill |
| 11 | |
| 12 | Adding type hints to existing code |
| 13 | Creating generic, reusable classes |
| 14 | Defining structural interfaces with protocols |
| 15 | Configuring mypy or pyright for strict checking |
| 16 | Understanding type narrowing and guards |
| 17 | Building type-safe APIs and libraries |
| 18 | |
| 19 | ## Core Concepts |
| 20 | |
| 21 | ### 1. Type Annotations |
| 22 | |
| 23 | Declare expected types for function parameters, return values, and variables. |
| 24 | |
| 25 | ### 2. Generics |
| 26 | |
| 27 | Write reusable code that preserves type information across different types. |
| 28 | |
| 29 | ### 3. Protocols |
| 30 | |
| 31 | Define structural interfaces without inheritance (duck typing with type safety). |
| 32 | |
| 33 | ### 4. Type Narrowing |
| 34 | |
| 35 | Use guards and conditionals to narrow types within code blocks. |
| 36 | |
| 37 | ## Quick Start |
| 38 | |
| 39 | |
| 40 | def get_user(user_id: str) -> User | None: |
| 41 | """Return type makes 'might not exist' explicit.""" |
| 42 | ... |
| 43 | |
| 44 | # Type checker enforces handling None case |
| 45 | user = get_user("123") |
| 46 | if user is None: |
| 47 | raise UserNotFoundError("123") |
| 48 | print(user.name) # Type checker knows user is User here |
| 49 | |
| 50 | |
| 51 | ## Fundamental Patterns |
| 52 | |
| 53 | ### Pattern 1: Annotate All Public Signatures |
| 54 | |
| 55 | Every public function, method, and class should have type annotations. |
| 56 | |
| 57 | |
| 58 | def get_user(user_id: str) -> User: |
| 59 | """Retrieve user by ID.""" |
| 60 | ... |
| 61 | |
| 62 | def process_batch( |
| 63 | items: list[Item], |
| 64 | max_workers: int = 4, |
| 65 | ) -> BatchResult[ProcessedItem]: |
| 66 | """Process items concurrently.""" |
| 67 | ... |
| 68 | |
| 69 | class UserRepository: |
| 70 | def __init__(self, db: Database) -> None: |
| 71 | self._db = db |
| 72 | |
| 73 | async def find_by_id(self, user_id: str) -> User | None: |
| 74 | """Return User if found, None otherwise.""" |
| 75 | ... |
| 76 | |
| 77 | async def find_by_email(self, email: str) -> User | None: |
| 78 | ... |
| 79 | |
| 80 | async def save(self, user: User) -> User: |
| 81 | """Save and return user with generated ID.""" |
| 82 | ... |
| 83 | |
| 84 | |
| 85 | Use `mypy --strict` or `pyright` in CI to catch type errors early. For existing projects, enable strict mode incrementally using per-module overrides. |
| 86 | |
| 87 | ### Pattern 2: Use Modern Union Syntax |
| 88 | |
| 89 | Python 3.10+ provides cleaner union syntax. |
| 90 | |
| 91 | |
| 92 | # Preferred (3.10+) |
| 93 | def find_user(user_id: str) -> User | None: |
| 94 | ... |
| 95 | |
| 96 | def parse_value(v: str) -> int | float | str: |
| 97 | ... |
| 98 | |
| 99 | # Older style (still valid, needed for 3.9) |
| 100 | from typing import Optional, Union |
| 101 | |
| 102 | def find_user(user_id: str) -> Optional[User]: |
| 103 | ... |
| 104 | |
| 105 | |
| 106 | ### Pattern 3: Type Narrowing with Guards |
| 107 | |
| 108 | Use conditionals to narrow types for the type checker. |
| 109 | |
| 110 | |
| 111 | def process_user(user_id: str) -> UserData: |
| 112 | user = find_user(user_id) |
| 113 | |
| 114 | if user is None: |
| 115 | raise UserNotFoundError(f"User {user_id} not found") |
| 116 | |
| 117 | # Type checker knows user is User here, not User | None |
| 118 | return UserData( |
| 119 | name=user.name, |
| 120 | email=user.email, |
| 121 | ) |
| 122 | |
| 123 | def process_items(items: list[Item | None]) -> list[ProcessedItem]: |
| 124 | # Filter and narrow types |
| 125 | valid_items = [item for item in items if item is not None] |
| 126 | # valid_items is now list[Item] |
| 127 | return [process(item) for item in valid_items] |
| 128 | |
| 129 | |
| 130 | ### Pattern 4: Generic Classes |
| 131 | |
| 132 | Create type-safe reusable containers. |
| 133 | |
| 134 | |
| 135 | from typing import TypeVar, Generic |
| 136 | |
| 137 | T = TypeVar("T") |
| 138 | E = TypeVar("E", bound=Exception) |
| 139 | |
| 140 | class Result(Generic[T, E]): |
| 141 | """Represents either a success value or an error.""" |
| 142 | |
| 143 | def __init__( |
| 144 | self, |
| 145 | value: T | None = None, |
| 146 | error: E | None = None, |
| 147 | ) -> None: |
| 148 | if (value is None) == (error is None): |
| 149 | raise ValueError("Exactly one of value or error must be set") |
| 150 | self._value = value |
| 151 | self._error = error |
| 152 | |
| 153 | @property |
| 154 | def is_success(self) -> bool: |
| 155 | return self._error is None |
| 156 | |
| 157 | @property |
| 158 | def is_failure(self) -> bool: |
| 159 | return self._error is not None |
| 160 | |
| 161 | def unwrap(self) -> T: |
| 162 | """Get value or raise the error.""" |
| 163 | if self._error is not None: |
| 164 | raise self._error |
| 165 | return self._value # type: ignore[return-value] |
| 166 | |
| 167 | def unwrap_or(self, default: T) -> T: |
| 168 | """Get value or return default.""" |
| 169 | if self._error is not None: |
| 170 | return default |
| 171 | return self._value # type: ignore[return-value] |
| 172 | |
| 173 | # Usage preserves types |
| 174 | def parse_config(path: str) -> Result[Config, ConfigError]: |
| 175 | try: |
| 176 | return Result(value=Config.from_file(path)) |
| 177 | except ConfigError as e: |
| 178 | return Result(error=e) |
| 179 | |
| 180 | result = parse_config("config.yaml") |
| 181 | if result.is_success: |
| 182 | config = result.unwrap() # Type: Config |
| 183 | |
| 184 | |
| 185 | ## Detailed worked examples and patterns |
| 186 | |
| 187 | Detailed sections (starting with `## Advanced Patterns`) live in `references/details.md`. Read that file when the navigation summary above is insufficient. |
| 188 | |
| 189 | ## Best Practices Summary |
| 190 | |
| 191 | **Annotate all public APIs** - Functions, methods, class attributes |
| 192 | **Use `T | None`** - Modern union syntax over `Optional[T]` |
| 193 | **Run strict type checking** - `mypy --strict` in CI |
| 194 | **Use generics** - Preserve type info in reusable code |
| 195 | **Define protocols** - Structural typing for interfaces |
| 196 | **Narrow types** - Use guards to help the type checker |
| 197 | **Bound type vars** - Restrict generics to meaningful types |
| 198 | **Create type aliases** - Meaningful names for complex types |
| 199 | **Minimize `Any`** - Use specific types or generics. `Any` is acceptable for truly dynamic data or when interfacing with untyped third-party code |
| 200 | **Document with types** - Types are enforceable documentation |
| 201 |
Details
- Updated
- Sep 21, 2026
- License
- MIT
- Stars on the repo
- ★ 39,857
- Checked
- Source
- wshobson/agents ↗
- Creator
- wshobson · 179 listings
Similar skills
See all →
SkillExtract text, tables, metadataXberg Document Extraction
SkillMove code from one implementation to another functionCode migration
SkillManages Ahrefs API usage in Python using ahrefs-python
MCPDocx LLM translatoradeu
SkillCreate an Amazon-compliant white-background main imageAmazon White-Background Main Image · 亚马逊白底主图
MCPOfficial monorepo of SDKs, CLISendmux sdkMore from wshobson
SkillMaster C/.NET backend development patterns for building
SkillImplement WCAG 2.2 compliant interfaces with mobileAccessibility compliance
SkillMaster Material Design 3 and Jetpack Compose patternsAndroid Mobile Design
SkillMigrate from AngularJS to Angular using hybrid modeAngular migration
SkillUnderstand anti-reversing, obfuscationAnti reversing techniques
SkillBuild production Apache Airflow DAGs with best practicesApache Airflow DAG Patterns