Questions tagged [mypy]

Mypy is an optional static type checker for Python.

Mypy is an optional static type checker for Python. Type hints conforming to PEP484 can be added to Python programs using Python's built-in type annotations (since Python 3) or a comment-based annotation syntax for Python 2.

2299 questions
11
votes
2 answers

Why is mypy complaining about list comprehension when it can't be annotated?

Why does Mypy complain that it requires a type annotation for a list comprehension variable, when it is impossible to annotate such a variable using MyPy? Specifically, how can I resolve the following error: from enum import EnumMeta def spam( y:…
c z
  • 7,726
  • 3
  • 46
  • 59
11
votes
0 answers

mypy django rest framework - Unsupported left operand type when multiple permission classes are used

I am integrating mypy on a existing codebase(using django, drf frameworks). Sample code in view.py: from rest_framework.permissions import IsAdminUser, IsAuthenticatedOrReadOnly @api_view() @permission_classes([IsAuthenticatedOrReadOnly |…
partha biswas
  • 204
  • 1
  • 7
11
votes
1 answer

MyPy type annotation for a bound method?

I'm writing some tests for a library using pytest. I want to try a number of test cases for each function exposed by the library, so I've found it convenient to group the tests for each method in a class. All of the functions I want to test have the…
Soren Bjornstad
  • 1,292
  • 1
  • 14
  • 25
11
votes
1 answer

How to type Python mixin with superclass calls?

I'm trying to use the FieldMixin from this answer in my project, but I'm having trouble getting it to pass mypy checks. The current code: class DynamicFieldsMixin(Serializer): context: Dict[str, Any] def get_field_names( self,…
l0b0
  • 55,365
  • 30
  • 138
  • 223
11
votes
1 answer

Check type hint coverage in Python

I would like to enforce type hinting everywhere in my Python package and configure CI to fail builds in case it's not 100%. Is there a way for me to report type hint coverage using mypy or any other package in Python similarly to how I can check…
mrapacz
  • 889
  • 8
  • 22
11
votes
1 answer

mypy: "__eq__" incompatible with supertype "object"

this is my code: class Person: def __init__(self, id): self.id = id def __eq__(self, other: 'Person') -> bool: return self.id == other.id def compare(self, other: 'Person') -> bool: return self.id ==…
jr6tp
  • 485
  • 1
  • 5
  • 15
11
votes
3 answers

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

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.…
exhuma
  • 20,071
  • 12
  • 90
  • 123
11
votes
2 answers

How to use Generic (higher-level) type variables in type hinting system?

Suppose I want to write a generic class using mypy, but the type argument for the class is itself a generic type. For example: from typing import TypeVar, Generic, Callable A = TypeVar("A") B = TypeVar("B") T = TypeVar("T") class…
11
votes
1 answer

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

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…
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
11
votes
2 answers

Does a default parameters overwrite type hints for mypy?

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…
Tobias Hermann
  • 9,936
  • 6
  • 61
  • 134
11
votes
1 answer

Confusion of annotating generator function as iterator

In the python typing documentation it is written: Alternatively, annotate your generator as having a return type of either Iterable[YieldType] or Iterator[YieldType]: def infinite_stream(start: int) -> Iterator[int]: while True: yield…
David Michael Gang
  • 7,107
  • 8
  • 53
  • 98
11
votes
3 answers

Class cannot subclass 'QObject' (has type 'Any') using mypy

I have a class that subclasses QObject. Everyting works fine but when I run mypy on it I get the error: "error: Class cannot subclass 'QObject' (has type 'Any')" At the moment I am totally stuck. I Have been reading the mypy docs but couldn't find…
Notbad
  • 5,936
  • 12
  • 54
  • 100
10
votes
2 answers

Type hint for an exhaustive dictionary with Enum/Literal keys

I'm working on code bases with extensive type hints, checked by mypy. There's several instances where we have a mapping from an enum.Enum or other small finite set of statically known values (typing.Literal) to fixed values, and thus using a…
huon
  • 94,605
  • 21
  • 231
  • 225
10
votes
1 answer

Why doesn't mypy pass when TypedDict calls update method

Example: from typing import TypedDict class MyType(TypedDict): a: int b: int t = MyType(a=1, b=2) t.update(b=3) mypy toy.py complains toy.py:9:1: error: Unexpected keyword argument "b" for "update" of "TypedDict" Found 1 error in 1 file…
zyxue
  • 7,904
  • 5
  • 48
  • 74
10
votes
1 answer

How do I suppress "has no attribute" errors from mypy when assigning a new attribute to a function?

I often use the following idiom for static initialization: def compute_answer() -> int: if compute_answer.ret is None: # Do stuff that only happens the first time compute_answer.ret = 42 return…
Vic
  • 487
  • 8
  • 17