mypy

Does PyCharm use Mypy?

心已入冬 提交于 2020-08-27 06:57:31
问题 Does PyCharm use Mypy or did JetBrains implement PEP 484 separately? 回答1: It appears that they went with their own implementation. Ultimately I would conjecture whether or not they use another open source library instead matters little; so long as they maintain their support for it, their choice of implementation is inconsequential. 来源: https://stackoverflow.com/questions/50497956/does-pycharm-use-mypy

Pydantic

≯℡__Kan透↙ 提交于 2020-08-17 17:21:28
目录 基本原理 安装 用法 dataclasses 嵌套的dataclasses 选择 验证器 pre和whole验证 always验证 dataclass验证器 字段检查 递归模型 模式创建 错误处理 datetime类型 Exotic类型 Secret类型 JSON类型 自定义数据类型 帮助函数 模型的 Config 类 设置 动态模型创建 与mypy一起使用 严格的可选项 必须字段和mypy 伪不可变性 复制 序列化 JSON序列化 Pickle序列化 抽象基类 延迟注解 Pydantic 是一个使用Python类型提示来进行数据验证和设置管理的库。Pydantic定义数据应该如何使用纯Python规范用并进行验证。PEP 484 从Python3.5开始引入了类型提示的功能,PEP 526 使用Python3.6中的变量注释语法对其进行了拓展。Pydantic使用这些注释来验证不受信任的数据是否采用了您想要的形式。 示例: from datetime import datetime from typing import List from pydantic import BaseModel class User(BaseModel): id: int name = 'John Doe' signup_ts: datetime = None friends: List[int

Mypy/typeshed stubs for Pandas

冷暖自知 提交于 2020-07-28 12:15:27
问题 Just checking to see if anybody listening has already generated a sort-of-working set of mypy/typeshed stubs for Pandas. I naively ran stubgen over the local Pandas install which generated some errors. I can go with what I have to start with, but was hoping someone else had pushed the ball further. (Nothing obvious turned up on GitHub, though there is an old ticket for stubs.) 回答1: I have not yet found stubs for pandas, however someone has created some for NumPy: https://github.com/machinalis

mypy, type hint: Union[float, int] -> is there a Number type?

一个人想着一个人 提交于 2020-07-17 03:07:11
问题 mypy is really handy and catches a lot of bugs, but when I write "scientific" applications, I often end up doing: def my_func(number: Union[float, int]): # Do something number is either a float or int, depending on the user's input. Is there an official way to do that? 回答1: Use float only , as int is implied in that type: def my_func(number: float): PEP 484 Type Hints specifically states that: Rather than requiring that users write import numbers and then use numbers.Float etc., this PEP

How can I add python type annotations to the flask global context g?

江枫思渺然 提交于 2020-07-06 09:41:24
问题 I have a decorator which adds a user onto the flask global context g: class User: def __init__(self, user_data) -> None: self.username: str = user_data["username"] self.email: str = user_data["email"] def login_required(f): @wraps(f) def wrap(*args, **kwargs): user_data = get_user_data() user = User(user_data) g.user = User(user_data) return f(*args, **kwargs) return wrap I want the type (User) of g.user to be known when I access g.user in the controllers. How can I achieve this? (I am using

How to add hint to a factory method?

一曲冷凌霜 提交于 2020-06-17 13:09:48
问题 I'm looking for a way to annotate return type of a factory function. It returns random child of 'AlgorithmBase'. class AlgorithmFactory: _algorithm_types = AlgorithmBase.__subclasses__() def select_random_algorithm(self) -> AlgorithmBase: # Select random algorithm algorithm_class = self._random_generator.choice(AlgorithmFactory._algorithm_types) algorithm = algorithm_class() return algorithm I get error from mypy: The error I'm getting is: Cannot instantiate abstract class 'AlgorithmBase'