mypy的额外类型和功能的集合
项目描述
mypy-extras
功能
- 提供增强其可能性的自定义
mypy
插件 - 提供新类型,这些类型可以与我们的插件一起使用
- 使用注释完全类型化,并通过mypy检查,与PEP561兼容
安装
pip install mypy-extras
您还需要正确配置 mypy
并安装我们的自定义插件
# In setup.cfg or mypy.ini:
[mypy]
plugins =
mypy_extras.plugin.entrypoint
我们还建议使用我们使用的相同的mypy
设置我们使用的。
用法
AttrOf
我们提供一个特殊类型来获取其他类型的命名属性,如下所示
from typing_extensions import Literal # or typing on python3.8+
from mypy_extras import AttrOf
class User(object):
def auth(self, username: str, password: str) -> bool:
return False # Just an example
def get_callback(user: User) -> AttrOf[User, Literal['auth']]:
return user.auth
user: User
reveal_type(get_callback(user))
# Revealed type is 'def (username: builtins.str, password: builtins.str) -> builtins.bool'
ensure_attr
我们可以确保某些str
属性在对象上存在
from mypy_extras import ensure_attr
class User(object):
policy = 'update'
reveal_type(ensure_attr(User, 'policy')) # Revealed type is 'Literal['policy']'
reveal_type(ensure_attr(User, 'missing')) # Error: attribute "missing" does not exist on type "User"
当我们根据字符串字段对对象进行任何操作时,这非常有用
DEFAULT_POLICY_FIELD: Final = ensure_attr(User, 'policy') # typesafe
# vs
DEFAULT_POLICY_FIELD: Final = 'policy'
# User can rename the field, and this will blow now!
许可协议
项目详情
下载文件
下载适用于您平台的应用程序。如果您不确定选择哪个,请了解有关安装包的更多信息。
源分发
mypy-extras-0.1.0.tar.gz (7.7 kB 查看哈希值)
构建分发版
mypy_extras-0.1.0-py3-none-any.whl (8.5 kB 查看哈希值)
关闭
mypy-extras-0.1.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 75fc61e95c53ac74d17ce4a746dc37fe7b50acd2c942f1ef462ef17dde65ae94 |
|
MD5 | 2945eeb7c3c5bdbcfaef3bb87f793628 |
|
BLAKE2b-256 | 184b93727b8f4cfbfda6d300df6963a8722ad14c4fb6ffd127a7fd75f610d97b |
关闭
mypy_extras-0.1.0-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b1387e1b696540762512b725cb858f838aab2dc07da7016291fbeaa840634596 |
|
MD5 | 4b90b32088e314181a7119721e102ee1 |
|
BLAKE2b-256 | ac624090038ca61a64dc4e3bc7dd5161b9539c43f15d77d315ed2259e827a40b |