给 cpython 加个校验 type 的语法糖可以吗 ?

python 有 typing hint !

def upload(content: str) -> bool:
    pass

但是 content 可以传递任意类型,比如 str、bytes、int、float 等等

所以,我会这样做:

def upload(content: str) -> bool:
    assert isinstance(content, str) # 💩
    pass

可以解决类型校验的问题!🤡 但是这样非常的不优雅!因为每个函数都要加 assert,代码会变的非常的丑陋和多余!💩💩💩💩💩💩💩💩💩💩💩💩💩

所以我想让 cpython 加一个语法糖

def upload(content:: str) -> bool:
    pass

把 typing hint 的单冒号 : 改成双冒号 ::

双冒号 :: 表示自动根据后面的 typing hint 做类型校验!

单冒号表示纯粹的 typing hint
双冒号表示 typing hint + check type

我觉得这是一个非常伟大的语法糖,我应该怎么做才能让搞 cpython 的那帮人 100% 实现这个新功能?

让 python 再次伟大 ✅
阅读 1.3k
推荐问题