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

栏目: 软件资讯 · 发布时间: 5年前

内容简介:Mypy 0.620 已发布,包含一些新特性和 bug 修复。Mypy 是 Python 的可选静态类型检查器,旨在结合动态类型和静态类型的优点。新版本可按如下方式安装:  python3 -m pip install -U mypy  亮点包括: 支持...

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

完整更新内容请查阅:


【声明】文章转载自:开源中国社区 [http://www.oschina.net]


以上所述就是小编给大家介绍的《静态类型检查器 Mypy 0.620 发布,支持 Data Classes》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Responsive Web Design

Responsive Web Design

Ethan Marcotte / Happy Cog / 2011-6 / USD 18.00

From mobile browsers to netbooks and tablets, users are visiting your sites from an increasing array of devices and browsers. Are your designs ready? Learn how to think beyond the desktop and craft be......一起来看看 《Responsive Web Design》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

MD5 加密
MD5 加密

MD5 加密工具