mypy

How should I type-hint an integer variable that can also be infinite?

安稳与你 提交于 2020-01-04 08:21:11
问题 Searching for this topic I came across the following: How to represent integer infinity? I agree with Martijn Peeters that adding a separate special infinity value for int may not be the best of ideas. However, this makes type hinting difficult. Assume the following code: myvar = 10 # type: int myvar = math.inf # <-- raises a typing error because math.inf is a float However, the code behaves everywhere just the way as it should. And my type hinting is correct everywhere else. If I write the

Subclassing Sequence with proper type hints in Python

跟風遠走 提交于 2020-01-04 02:39:08
问题 I'm trying to implement a kind of custom sequence class in Python: from typing import Sequence, TypeVar, List T = TypeVar('T') class MySequence(Sequence[T]): def __init__(self): self._container: Sequence[T] = [] def __getitem__(self, idx): return self._container[idx] def __len__(self): return len(self._container) Now I want to check that mypy is aware that elements of MySequence are items of type T : foo: MySequence[str] = MySequence() reveal_type(foo[0]) # Revealed type is 'Any' So it fails:

super().__init__(…) in mixins fails with `Too many arguments for “__init__” of “object” `

回眸只為那壹抹淺笑 提交于 2020-01-02 19:28:09
问题 When I create a mixin class that extends the logic of __init__ , the regular thing to do is: class ExtraValuemixin: def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) # some extra initialization self._extra_value = 1 def retrieve_extra_value(self): return self._extra_value However this looks wrong to mypy, as it says: Too many arguments for "__init__" of "object" I get it, there's no *args or **kwargs in the object 's constructor signature; but this is a mixin, and

mypy error, overload with Union/Optional, “Overloaded function signatures 1 and 2 overlap with incompatible return types”

ぃ、小莉子 提交于 2020-01-02 03:59:06
问题 So, let's start with an example. Suppose we have several types that can be combined together, let's say we are using __add__ to implement this. Unfortunately, due to circumstances beyond our control, everything has to be "nullable", so we are forced to use Optional everywhere. from typing import Optional, List, overload class Foo: value: int def __init__(self, value: int) -> None: self.value = value def __add__(self, other: 'Foo') -> 'Optional[Foo]': result = self.value - other.value if

mypy error, overload with Union/Optional, “Overloaded function signatures 1 and 2 overlap with incompatible return types”

拥有回忆 提交于 2020-01-02 03:59:04
问题 So, let's start with an example. Suppose we have several types that can be combined together, let's say we are using __add__ to implement this. Unfortunately, due to circumstances beyond our control, everything has to be "nullable", so we are forced to use Optional everywhere. from typing import Optional, List, overload class Foo: value: int def __init__(self, value: int) -> None: self.value = value def __add__(self, other: 'Foo') -> 'Optional[Foo]': result = self.value - other.value if

Python: All type hints errors in subclass constructure seems ignored

↘锁芯ラ 提交于 2019-12-25 01:33:14
问题 I have the following code with python type hints It has a bunch of errors. All erros in code are found by mypy but not the errors in constructor of S. Why? I cannot find out what is happening thanks code: import typing class T(object): def __init__(self, a: int, b: str = None) -> None: self.a = a self.b: typing.Union[str, None] = b self._callback_map: typing.Dict[str, str] = {} class S(T): def __init__(self): super().__init__(self, 1, 2) self._callback_map[1] = "TOTO" s = T(1, 1) t = T(1, b=2

Why is the mypy FAQ mentioning performance impact?

﹥>﹥吖頭↗ 提交于 2019-12-24 06:29:24
问题 As far as I understood, mypy is a tool that will check python code that includes type annotations. However, in the FAQ, I read the following: Mypy only does static type checking and it does not improve performance. It has a minimal performance impact. In the second sentence, "minimal" seems to imply that there is a performance impact, (albeit minimal). Why would mypy impact performance? I thought that in the end, the code still had to be run by the python interpreter, so mypy (or any other

mypy: how to define a generic subclass

无人久伴 提交于 2019-12-23 12:45:57
问题 I have a subclass of queue.Queue like so: class SetQueue(queue.Queue): """Queue which will allow a given object to be put once only. Objects are considered identical if hash(object) are identical. """ def __init__(self, maxsize=0): """Initialise queue with maximum number of items. 0 for infinite queue """ super().__init__(maxsize) self.all_items = set() def _put(self): if item not in self.all_items: super()._put(item) self.all_items.add(item) I am trying to use mypy for static type checking.

mypy: Signature of “__getitem__” incompatible with supertype “Sequence”

白昼怎懂夜的黑 提交于 2019-12-23 09:40:34
问题 I have a class that inherits from MutableSequence like this: class QqTag(MutableSequence): def __init__(self): self._children = [] def __getitem__(self, idx: int) -> 'QqTag': return self._children[idx] mypy complains that Signature of "__getitem__" incompatible with supertype "Sequence" . In Sequence , this method is defined as: @abstractmethod def __getitem__(self, index): raise IndexError So, what's the problem and why mypy isn't happy with my implementation? 回答1: As mentioned in comments,

Remove error from mypy for attributes set dynamically in a Python class

旧巷老猫 提交于 2019-12-21 13:10:15
问题 I am using mypy to check my Python code. I have a class where I set dynamically some attributes and mypy keep on complaining about it: error:"Toto" has no attribute "age" This is my code: class Toto: def __init__(self, name:str) -> None: self.name = name for attr in ['age', 'height']: setattr(self, attr, 0) toto = Toto("Toto") toto.age = 10 # "Toto" has no attribute "age" :( Obviously, there could be 3 ways to solve the issue Ignoring the issue with # type: ignore : toto.age = 10 # type: