mypy

How can I make a recursive Python type defined over several aliases?

ぐ巨炮叔叔 提交于 2021-01-22 19:13:55
问题 I want this logical type structure: ObjectType = Dict[str, 'EntryType'] ListType = List['EntryType'] EntryType = Union[str, 'ListType', 'ObjectType'] mypy reports these errors: mdl/structure.py:7: error: Cannot resolve name "ObjectType" (possible cyclic definition) mdl/structure.py:7: error: Cannot resolve name "EntryType" (possible cyclic definition) mdl/structure.py:8: error: Cannot resolve name "ListType" (possible cyclic definition) ... Is there some way to encode this recursive data type

Does a default parameters overwrite type hints for mypy?

不羁的心 提交于 2021-01-22 06:37:13
问题 The following code is rejected by mypy as expected: def foo(value: int) -> None: print(value, type(value)) foo(None) output: error: Argument 1 to "foo" has incompatible type "None"; expected "int" But after introducing a default parameter of None , there is no error anymore: def foo(value: int=None) -> None: print(value, type(value)) foo(None) I would expect mypy to only allow None (as argument and as the default) if we change value from int to Optional[int] , but it seems like this is not

Refactor to meet mypy requirements

偶尔善良 提交于 2021-01-01 17:49:56
问题 I have a codebase which I would like to validate using mypy. In the current design, it is very common that a class may have a non-primitive member that can be set later after __init__ . So, in __init__ the member is initialized to None and its type becomes Optional accordingly. The problem is that now MyPy requires me to check that the member is not None every time it is being used. As a quick solution I can add assert self._member is not None # MyPy in all the relevant scopes, but it seems

Refactor to meet mypy requirements

独自空忆成欢 提交于 2021-01-01 17:48:29
问题 I have a codebase which I would like to validate using mypy. In the current design, it is very common that a class may have a non-primitive member that can be set later after __init__ . So, in __init__ the member is initialized to None and its type becomes Optional accordingly. The problem is that now MyPy requires me to check that the member is not None every time it is being used. As a quick solution I can add assert self._member is not None # MyPy in all the relevant scopes, but it seems

Why does mypy think library imports are missing?

▼魔方 西西 提交于 2020-12-29 09:45:29
问题 When I run mypy it complains that modules cannot be found: sal@ahfang:~/workspace/ecs/cx-project-skeleton-repo/src/cx-example-function$ pipenv run python -m mypy . example_lambda.py:3: error: Cannot find module named 'aws_xray_sdk.core' But when trying to import that exact same module with the exact same Python interpreter, it seems that the module does exist and is importable. python Python 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or

Why does mypy think library imports are missing?

浪尽此生 提交于 2020-12-29 09:44:26
问题 When I run mypy it complains that modules cannot be found: sal@ahfang:~/workspace/ecs/cx-project-skeleton-repo/src/cx-example-function$ pipenv run python -m mypy . example_lambda.py:3: error: Cannot find module named 'aws_xray_sdk.core' But when trying to import that exact same module with the exact same Python interpreter, it seems that the module does exist and is importable. python Python 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or

How to use reveal_type in mypy

限于喜欢 提交于 2020-12-25 01:36:43
问题 I have read that I can reveal the type of variables by using a function called reveal_type , but I can't find how to use it or from where to import it. 回答1: I found out in the end how to use it: You should just put and use the reveal_type in the code, and run it with the mypy program. Then, it will log a message that look like this: Revealed type is 'builtins.str*' From the mypy documentation: reveal_type is only understood by mypy and doesn’t exist in Python, if you try to run your program.

平庸开发者的生存指南

旧街凉风 提交于 2020-12-06 18:31:56
我个人认识一些非常有才华的开发人员,他们可以一帆风顺地创建极好的软件。正是这些天赋人士,使得外行人对我们这个行业充满了很高的期望。但我要说的一个可悲的事实是: 并非每个人都是忍者/大师/明星开发者 。 我就不是这些闪耀的新星,我只是一名平庸的开发者。 如果你也不是天才玩家,那么本文将指导你如何在这个行业中生存下去。 最简单的事情——只要google一下 我记不了很多东西。像标准库中的函数和方法、参数位置、软件包名称,样板代码等等,都在我脑容量之外。 所以,我必须使用google搜索。我每天都这样做。我也一直在重复使用旧项目的代码。有时我甚至从StackOverflow或Github复制粘贴答案。是的,我的开发其实可称之为: StackOverflow驱动开发 。 但我并不孤单。许多其他开发人员也这样做。有一个受众面很广的twitter讨论就是由Ruby on Rails的创建者所启动的。 那么,为什么一开始会认为这种行径是不好的呢?因为它有若干缺点: 会导致你复制到糟糕的设计决策或易受其他人攻击的代码 会形成一种依赖心态:要是我们不能google到内容,那么只能向人求助 没有网就不能工作 但是,我不认为这些是大问题。它甚至可以作为是你的秘密武器。我有一些建议可用于减少其负面影响。 生存指南: 使用IDE来获得自动完成和建议,所以你不必google编程语言的基础内容;

How to cast a typing.Union to one of its subtypes in Python?

为君一笑 提交于 2020-12-05 06:00:51
问题 I’m using Python 3.6.1, mypy, and the typing module. I created two custom types, Foo and Bar , and then used them in a dict I return from a function. The dict is described as mapping str to a Union of Foo and Bar . Then I want to use values from this dict in a function that names only one argument each: from typing import Dict, Union, NewType Foo = NewType("Foo", str) Bar = NewType("Bar", int) def get_data() -> Dict[str, Union[Foo, Bar]]: return {"foo": Foo("one"), "bar": Bar(2)} def process

What does the asterisk in the output of `reveal_type` mean?

♀尐吖头ヾ 提交于 2020-12-05 04:58:45
问题 reveal_type(1) # Revealed type is 'builtins.int' bla = [1,2,3] reveal_type(bla[0]) # Revealed type is 'builtins.int*' reveal_type(bla[0] * 2) # Revealed type is 'builtins.int' What is the difference between int and int* ? 回答1: It means that particular type was inferred by mypy as a part of performing type variable substitution. For example, blah[0] is actually doing blah.__getitem__(0) : it turns out that the __getitem__ method is defined to return some value of type _T , where _T is whatever