python typing tool
Project description
功能
安装
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
: 设置类型检查的最大深度,默认值为5
max_sample
: 设置最大采样数,默认值为-1
protocol_type_strict
: 是否严格检查Protocol
类型,默认值为False
dataclass_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"
注意
- NewType 无法在运行时进行 like_isinstance
Project details
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.5.tar.gz
(14.0 kB
view details)
Built Distribution
File details
Details for the file typing_tool-0.1.5.tar.gz
.
File metadata
- Download URL: typing_tool-0.1.5.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.20.0.post1 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a96670b779b07e5b7b0a409ae4b9283744c1166b5347c2abced647618c96784 |
|
MD5 | 5ac5705eb0761de0aede731fe2fe0b46 |
|
BLAKE2b-256 | 41af4b057edf623b417e7ef758be168f1f2f250933315c7d2ffddf67ffc9d7f8 |
File details
Details for the file typing_tool-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: typing_tool-0.1.5-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.20.0.post1 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c17eb2f877cd067c78454159a6987fb39a70f9f6929213bdf923c1f6b82c77b1 |
|
MD5 | e7331dae7cf05435746c9b8a43898970 |
|
BLAKE2b-256 | 41d377af8ba5649db079624cc1ace3d43a68f1a733cbcc8a4a791a10daa62bc4 |