综合编程

静态类型检查器 Mypy 0.620 发布,支持 Data Classes

微信扫一扫,分享到朋友圈

静态类型检查器 Mypy 0.620 发布,支持 Data Classes
0

Mypy 0.620 已发布,包含一些新特性和 bug 修复。Mypy 是 Python 的可选静态类型检查器,旨在结合动态类型和静态类型的优点。新版本可按如下方式安装:

 python3 -m pip install -U mypy

亮点包括:

支持 Python 3.7 的 data classes

    from dataclasses import dataclass    from typing import List    
    @dataclass
    class FitResult:
        optimum: List[float]
        chi: float
        method: str = "TRF"
    
    FitResult([0.1, 0.2], 1.2)  # OK
    FitResult([0.1, 0.2], 1.2, "LM")  # Also OK
    FitResult(1, 2)  # Error!

泛型类型过载和其他过载改进

    from typing import List, Union, overload    
    @overload
    def summarize(data: List[int]) -> float: ...    @overload
    def summarize(data: List[str]) -> str: ...    def summarize(data):
        # Implementation goes here
        ...
    
    gen_data: Union[List[int], List[str]]
    res = summarize(gen_data)  # OK, inferred type is Union[float, str]
    
    bad_data: int
    summarize(bad_data)    # error: No overload variant of "summarize" matches argument type "int"
    # note: Possible overload variants:
    # note:     def summarize(data: List[int]) -> float
    # note:     def summarize(data: List[str]) -> str

Incomplete and partial packages

    # pack/__init__.pyi
    from typing import Any    def __getattr__(arrr: str) -> Any: ...    
    # pack/subpack/__init__.pyi
    # empty
    
    # pack/subpack/mod.pyi
    class Test: ...    
    # main.py
    from pack import other  # OK, pack is incomplete
    other.func(1, 2)  # OK, all types in incomplete packages are Any
    
    from pack.subpack import mod  # OK
    from pack.subpack import another  # Error: missing library stub file

完整更新内容请查阅:

阅读原文...


微信扫一扫,分享到朋友圈

静态类型检查器 Mypy 0.620 发布,支持 Data Classes
0

开源中国

Datepicker not showing the current value when editing the cakephp form

上一篇

无需WiFi,在户外也能智能交流,DOSS超级蓝牙音箱体验

下一篇

评论已经被关闭。

插入图片

热门分类

往期推荐

静态类型检查器 Mypy 0.620 发布,支持 Data Classes

长按储存图像,分享给朋友