Skip to main content

python typing tool

Project description

typing_tool

Typing_Tool 是一个 Python 类型工具

CodeFactor GitHub CodeQL

功能

安装

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.6.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

typing_tool-0.1.6-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file typing_tool-0.1.6.tar.gz.

File metadata

  • Download URL: typing_tool-0.1.6.tar.gz
  • Upload date:
  • Size: 14.5 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

Hashes for typing_tool-0.1.6.tar.gz
Algorithm Hash digest
SHA256 dd092a4e74974669d1311c2b47c0d7af50b9b59dc418b68c2791d2b001f38934
MD5 1db014b31ae41f2852577333db0863c4
BLAKE2b-256 91241a1242156363f51bc8677fc5a44160350bdcf8e658b13273962f1d5870c1

See more details on using hashes here.

File details

Details for the file typing_tool-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: typing_tool-0.1.6-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

Hashes for typing_tool-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 67e5425f798352f3843b6ffbee66539233a6234b6e73a4796b8fd175f677f021
MD5 e1bad0fc14f231366ca7b2bc5e4e175f
BLAKE2b-256 5b1e4a05d2220e231095e096f9ce332583e5b7502ff388eae28adab8028f3ecd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page