跳转到主要内容

为Python类型创建tsukkomi

项目描述


tsukkomi

为Python类型执行tsukkomi。

什么是tsukkomi?

tsukkomi是日语中的一个词,指的是西方文化中喜剧搭档中的直角人。当直角人反应搭档的荒谬行为时,tsukkomi将反应不正确的类型。

如何使用tsukkomi?

tsukkomityping 中获取类型提示。编写带有注解的代码,使用 tsukkomi.typed.typechecked 装饰所有可调用对象。顺便说一句,泛型类型不受支持,请参阅tsukkomi 不支持泛型部分以获取详细信息。

from typing import Sequence

from tsukkomi.typed import typechecked

@typechecked
def greeting(name: str) -> str:
    return name

greeting('a') # it is ok
greeting(1) # this will raise `TypeError`

tsukkomi 不支持泛型

tsukkomi 不支持泛型类型检查,包括已经继承泛型类型的类型,如 typing.Sequencetyping.Mutable 等。以下示例代码可以通过 tsukkomi.typed.typechecked 通过。

import typing

from tsukkomi.typed import typechecked


T = typing.TypeVar('T')


class Boke(typing.Generic[T]):

    @typechecked
    def stupid(self, word: T) -> T:
        return type(word)

    @typechecked
    def correction(self, words: Sequence[T]) -> T:
        return random.sample(words, 1)[0]


@typechecked
def boke_and_tsukkomi(stupid_words: Sequence[str],
                      correction: Sequence[str]) -> bool:
    return any(s == c for s, c in zip(stupid_words, correction)))


boke = Boke[str]()
print(boke.stupid('hello world'))
print(boke.correction([1, 2, 3]))
print(boke_and_tsukkomi([1, 2], [1.0, 2.0]))

变更日志

0.0.5 — 2016-05-19

修改

  • 将 None 解释为 type(None)

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分布

tsukkomi-0.0.6.tar.gz (4.8 kB 查看哈希值)

上传时间

构建分布

tsukkomi-0.0.6-py3-none-any.whl (6.4 kB 查看哈希值)

上传时间 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面