mypy

How to statically get TypeVar parameters from a Generic for use in static type checking?

我与影子孤独终老i 提交于 2021-02-08 10:40:36
问题 I have a class that inherits from typing.Generic and passes in one TypeVar as the parameter. Later on in the code, I would like to: Statically (not at runtime) get the TypeVar parameter out from the class Alias it to another type variable Use that alias to type hint the return of a function Is there some way in Python that I can make this happen? The only piece I am missing is step 1, how to get the type parameter out of a type variable My Use Case from abc import ABC, abstractmethod from

Python type hint for specific values of dictionary

帅比萌擦擦* 提交于 2021-02-07 10:27:02
问题 I am a fan of mypy and I would like to add some type hints on a dictionary that has different type of values. My dictionary is the following {'name':'John', 'age':136, 'hobbies':['Python', 'cooking', 'reading']} The current typing I think of is: Dict[str, Union[str, int, List[str]] Is there any way to specify that name is a string, age is an integer, and hobbies is a list (for further specific type checking by mypy )? NOTE: This data is coming from an external source containing a list of such

Can mypy select a method return type based on the type of the current object?

£可爱£侵袭症+ 提交于 2021-02-05 06:55:22
问题 In the following code, calling the clone() method on an instance of A will return an instance of type A, calling it on an instance of B will return an instance of type B, and so on. The purpose is to create a new instance which is identical to the current one but has a different internally generated primary key, so it can be edited from there and safely saved as a new item. ??? is some kind of type qualifier, but I'm not sure what the right choice is. class A: def clone(self) -> ???: cls =

Can mypy select a method return type based on the type of the current object?

 ̄綄美尐妖づ 提交于 2021-02-05 06:55:10
问题 In the following code, calling the clone() method on an instance of A will return an instance of type A, calling it on an instance of B will return an instance of type B, and so on. The purpose is to create a new instance which is identical to the current one but has a different internally generated primary key, so it can be edited from there and safely saved as a new item. ??? is some kind of type qualifier, but I'm not sure what the right choice is. class A: def clone(self) -> ???: cls =

Mypy doesn't throw an error when mixing booleans with integers

亡梦爱人 提交于 2021-02-01 20:41:48
问题 I am trying to use mypy to check a Python 3 project. In the example below, I want mypy to flag the construction of the class MyClass as an error, but it doesn't. class MyClass: def __init__(self, i:int) -> None: pass obj = MyClass(False) Can anyone explain this, please? I.e. explain why mypy does not report an error? 回答1: It’s because — unfortunately! — booleans in Python are integers. As in, bool is a subclass of int : In [1]: issubclass(bool, int) Out[1]: True Hence the code typechecks, and

Mypy doesn't throw an error when mixing booleans with integers

ⅰ亾dé卋堺 提交于 2021-02-01 20:41:26
问题 I am trying to use mypy to check a Python 3 project. In the example below, I want mypy to flag the construction of the class MyClass as an error, but it doesn't. class MyClass: def __init__(self, i:int) -> None: pass obj = MyClass(False) Can anyone explain this, please? I.e. explain why mypy does not report an error? 回答1: It’s because — unfortunately! — booleans in Python are integers. As in, bool is a subclass of int : In [1]: issubclass(bool, int) Out[1]: True Hence the code typechecks, and

How do I annotate a type which has an extra attribute with mypy?

你说的曾经没有我的故事 提交于 2021-01-29 03:54:26
问题 I have an ast.UnaryOp object, but I manually added a parent attribute (see answer). How do I annotate this in a function? I currently simply have: def _get_sim206(node: ast.UnaryOp): if isinstance(node.parent, ast.If): return False return True But mypy complains (rightfully so) that ast.UnaryOp does not have the parent attribute. How can I tell mypy that the node is not ast.UnaryOp but ast.UnaryOp + parent attribute ? My Try I've created my own UnaryOp class which has a parent attribute. I

Factory function for mypy `TypedDict`

≡放荡痞女 提交于 2021-01-29 03:05:29
问题 I want to be able to write a function that checks if a dictionary confirms to my TypedDict , however I can't get the generic type right. So the resulting function should be something like: T = typing.Generic('T', bound=...) # This is `bound=...` something I want to find out def check_typeddict(value: dict, to_type: typing.Type[T]) -> T: # do some type checking return typing.cast(T, value) check_type(MyTypedDict, {'a': 5}) Things like using TypedDict or dict for the bound value do not work, is

Factory function for mypy `TypedDict`

杀马特。学长 韩版系。学妹 提交于 2021-01-29 03:02:48
问题 I want to be able to write a function that checks if a dictionary confirms to my TypedDict , however I can't get the generic type right. So the resulting function should be something like: T = typing.Generic('T', bound=...) # This is `bound=...` something I want to find out def check_typeddict(value: dict, to_type: typing.Type[T]) -> T: # do some type checking return typing.cast(T, value) check_type(MyTypedDict, {'a': 5}) Things like using TypedDict or dict for the bound value do not work, is

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

て烟熏妆下的殇ゞ 提交于 2021-01-22 19:16:44
问题 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