python-typing-patterns
有效应用 Python 类型提示
또한 다음에서 사용할 수 있습니다: 0xDarkMatter
Python 类型提示可提高代码质量并在早期发现错误。本技能提供了 TypeVar、Generic、Protocol、TypedDict 的模式,以及使用 Pydantic 和 typeguard 进行运行时验证的方法。
스킬 ZIP 다운로드
Claude에서 업로드
설정 → 기능 → 스킬 → 스킬 업로드로 이동
토글을 켜고 사용 시작
테스트해 보기
"python-typing-patterns" 사용 중입니다. undefined
예상 결과:
- Use TypeVar to preserve the input type:
- ```python
- from typing import TypeVar
- T = TypeVar('T')
- def identity(x: T) -> T:
- return x
- ```
- Result: identity(42) returns int, identity('hello') returns str
"python-typing-patterns" 사용 중입니다. undefined
예상 결과:
- Use Pydantic with EmailStr:
- ```python
- from pydantic import BaseModel, EmailStr
- class User(BaseModel):
- email: EmailStr
- ```
- This automatically validates and normalizes email addresses when creating User instances.
보안 감사
안전This is a documentation/reference skill containing only Python typing patterns. All 293 static findings are false positives. The scanner misidentified markdown code formatting (backticks) as shell execution, TOML config keys as cryptographic algorithms, and Python typing library names as sensitive keywords. No actual network calls, credential handling, or command execution exists.
위험 요인
🌐 네트워크 접근 (15)
⚙️ 외부 명령어 (218)
⚡ 스크립트 포함 (1)
📁 파일 시스템 액세스 (3)
품질 점수
만들 수 있는 것
为代码添加类型提示
学习 Python 3.10+ 中类型注解的正确语法,包括泛型和协议。
设计类型安全的 API
使用 TypeVar 和 Protocol 创建灵活的 API,实现无需继承的结构化类型。
验证 API 数据
使用 Pydantic 和 typeguard 在运行时强制执行类型检查,适用于 API 请求和配置。
이 프롬프트를 사용해 보세요
Show me how to write type hints for a function that takes a list of strings and returns an integer.
How do I use TypeVar to create a function that works with any type but preserves the type information?
Explain how Protocol works in Python and show an example where an object without inheritance satisfies an interface.
How do I validate function arguments at runtime using Pydantic v2 with field constraints?
모범 사례
- 使用 Python 3.10+ 的联合语法(X | Y)而非 Optional[X],以提高可读性
- 对于结构化类型,优先使用 Protocol 而非 ABC,以允许鸭子类型
- 在 mypy 或 pyright 中启用严格模式,以便尽早发现问题
피하기
- 大量使用 Any 会削弱类型检查的意义
- 避免对前向引用使用引号(使用 from __future__ import annotations)
- 不要在同一代码库中混合使用 Optional[X] 和 X | None