静态类型检查器 Mypy 0.620 发布,支持 Data Classes
2018-07-16 来源:oschina
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
完整更新内容请查阅:
https://mypy-lang.blogspot.com/2018/07/mypy-0620-released.html
标签: isp
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐