python typing tool
Project description
功能
- 类型检查增强: 扩展 isinstance 和 issubclass 函数,支持复杂类型检查
- 自动重载: 基于类型提示的函数重载功能
- 依赖注入: 基于类型提示的自动依赖注入系统
安装
pip install typing_tool
Or
pdm add typing_tool
入门指南
typing_tool 是一个用于增强 Python 类型检查能力的工具库。特别地,它扩展了 isinstance 和 issubclass 函数的能力,使其能够处理更复杂的类型检查需求。
支持类型
like_isinstance
- 基础类型 str/int/...
- 容器泛型 list[T]/dict[K, V]/...
- Union 类型类型
- Type
- TypeVar 类型变量
- 泛型类 Generic[T]
- Annotated/Field 注解类型
- Protocol 协议类型
- Protocol[T] 泛型协议类型
- TypedDict 字典类型
- dataclass 数据类
- dataclass[T] 泛型数据类
like_issubclass
- 基础类型 str/int
- 容器泛型 list[T]/dict[K, V]
- Union 类型类型
- NewType 新类型
- Type
- TypeVar 类型变量
- 泛型类 Generic[T]
- Protocol 协议类型
- Protocol[T] 泛型协议类型
Check Config
depth: 设置类型检查的最大深度,默认值为5max_sample: 设置最大采样数,默认值为-1protocol_type_strict: 是否严格检查Protocol类型,默认值为Falsedataclass_type_strict: 是否严格检查dataclass类型,默认值为False
自动重载
from typing import Any
from typing_extensions import overload
from typing_tool import auto_overload
@overload
def process(response: None) -> None:
return None
@overload
def process(response1: int, response2: str) -> tuple[int, str]:
return response1, response2
@overload
def process(response: bytes) -> str:
return response.decode()
@auto_overload()
def process(*args, **kwargs) -> Any: ...
assert process(None) is None
assert process(1, "2") == (1, "2")
assert process(b"test") == "test"
依赖注入
typing_tool 提供了强大的依赖注入功能,支持基于类型提示的自动依赖解析和注入。
基本用法
from typing_tool import auto_inject, create_injector, register_dependency
# 定义服务
class Logger:
def log(self, message: str):
print(f"[LOG] {message}")
class Database:
def query(self, sql: str):
return [{"id": 1, "data": "test"}]
# 创建依赖容器
dependencies = {
'logger': Logger(),
'db': Database()
}
# 使用 auto_inject 直接调用
def process_data(logger: Logger, db: Database):
logger.log("Processing data...")
return db.query("SELECT * FROM users")
result = auto_inject(process_data, dependencies)
装饰器方式
# 创建注入器装饰器
injector = create_injector(dependencies)
@injector
def service_function(logger: Logger, db: Database):
logger.log("Service called")
return db.query("SELECT * FROM products")
# 直接调用,依赖会自动注入
result = service_function()
依赖注册
# 使用装饰器注册依赖
@register_dependency(dependencies)
class UserService:
def __init__(self, logger: Logger, db: Database):
self.logger = logger
self.db = db
def get_users(self):
self.logger.log("Getting users...")
return self.db.query("SELECT * FROM users")
# UserService 实例会自动创建并注册到依赖容器中
匹配策略
typing_tool 支持多种依赖匹配策略:
type_like: 基于类型匹配(默认)name_like: 基于参数名匹配any_like: 类型或名称匹配(参数优先)
from typing_tool import name_like, any_like
# 使用名称匹配
name_injector = create_injector(dependencies, like=name_like)
# 使用混合匹配
any_injector = create_injector(dependencies, like=any_like)
注意
- NewType 无法在运行时进行 like_isinstance
- 依赖注入功能需要函数参数有明确的类型提示
- 支持位置参数、关键字参数和混合参数类型
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
typing_tool-0.1.10.tar.gz
(28.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file typing_tool-0.1.10.tar.gz.
File metadata
- Download URL: typing_tool-0.1.10.tar.gz
- Upload date:
- Size: 28.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.25.2 CPython/3.13.3 Linux/6.11.0-1015-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1b196703b065310a0925e12623780fc5d7f9a0f55f13d6b2870728498def4e09
|
|
| MD5 |
4ca7f355700dae4dae6d7322b82589cd
|
|
| BLAKE2b-256 |
4788643c209c72df1311626950c70542d13cb0e8b3e2ca2d510104e60405eba9
|
File details
Details for the file typing_tool-0.1.10-py3-none-any.whl.
File metadata
- Download URL: typing_tool-0.1.10-py3-none-any.whl
- Upload date:
- Size: 16.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.25.2 CPython/3.13.3 Linux/6.11.0-1015-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ca74dcd5d2d5ca41e43a705eaf7888936f0dd7f647183679877e6f6beff711c3
|
|
| MD5 |
a1ad2254ff9c4ac0b1899fa2a469f1f4
|
|
| BLAKE2b-256 |
0539c31f0a56a15b9362154648bb7e3f9b5146fe38f20728565aa9dc6eac0cbf
|