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
13
votes
3 answers

Type hinting with descriptors

In this pull request it looks like type hinting support for descriptors was added. However it looks like no finalized "correct" usage example was ever posted, nor does it looks like any documentation was ever added to the typing module or to…
shadowtalker
  • 12,529
  • 3
  • 53
  • 96
13
votes
1 answer

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

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: …
Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107
13
votes
1 answer

Avoid type warnings when mocking objects in unit tests?

Assuming I have a function that takes a complex object and does something with it: def foo(bar: SomeComplexObject): ... In unit tests bar will be replaced by a mock object, but that of courses now raises type warnings. Should I simply ignore or…
gmolau
  • 2,815
  • 1
  • 22
  • 45
13
votes
4 answers

How to declare multiple variables with type annotation syntax in Python?

As far as I know, now we can declare variables using type annotation syntax in Python 3.6 as following code. def printInt(): a: int = 0 b: int = 1 c: int = 2 print(a, b, c) What I want to do is declaring variables a, b, c in one…
sdf3w
  • 261
  • 2
  • 6
13
votes
2 answers

mypy: how to define a generic subclass

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,…
blokeley
  • 6,726
  • 9
  • 53
  • 75
12
votes
1 answer

How to tell mypy that a class decorator adds a method to the decorated-class

The Python library pure_protobuf forces its users to use dataclasses, and decorate them with another decorator: # to be clear: these two decorators are library code (external) @message @dataclass class SearchRequest: query: str = field(1,…
zerohedge
  • 3,185
  • 4
  • 28
  • 63
12
votes
2 answers

Can I inform mypy that an expression will not return an Optional?

I have the following code: def extract_table_date(bucket_path: str) -> str: event_date = re.search(r"date=([^/]+)", bucket_path) return event_date.group(1)[0:10].replace("-", "") mypy throws error on the last line: Item "None" of…
jamiet
  • 10,501
  • 14
  • 80
  • 159
12
votes
2 answers

How should a NamedTemporaryFile be annotated?

I tried typing.IO as suggested in Type hint for a file or file-like object?, but it doesn't work: from __future__ import annotations from tempfile import NamedTemporaryFile from typing import IO def example(tmp: IO) -> str: print(tmp.file) …
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
12
votes
2 answers

mypy: Why is "int" a subtype of "float"?

Why does "mypy" consider "int" as a subtype of "float"? A subtype shall support all methods of its supertype, but "float" has methods, which "int" does not support: test.py: def f(x : float) -> bool: return…
12
votes
3 answers

How to implement an interface in a way that is compatible with static type checks?

I have two base classes, Foo and Bar, and a Worker class which expects objects that behave like Foo. Then I add another class that implements all relevant attributes and methods from Foo but I didn't manage to communicate this successfully to static…
a_guest
  • 34,165
  • 12
  • 64
  • 118
12
votes
1 answer

How do I correctly set MYPYPATH to pick up stubs for mypy?

I can't for the life of me get MyPy to find stubs that aren't colocated with their source code. Here's the structure I have for my project: trymypy/ |- stubs/ | \- foo.pyi |- __init__.py |- usefoo.py \- foo.py # foo.py def foofunc(x): return…
12
votes
2 answers

Specify keys for mypy in python dictionary

Suppose I have some code like def get_x(d: dict) -> int: d["x"] However, I want to tell mypy that d should only contain certain keys (only the "x" key for example). That way, if I make a mistake lower in the code trying to reference an invalid…
Daniel Kats
  • 5,141
  • 15
  • 65
  • 102
12
votes
2 answers

Is there a way to totally ignore all of the MyPy errors in specific project packages?

Is there a way to ignore all of the errors in certain packages within my project? Some of the code in my project is compiled Protocol Buffers code which doesn't pass a MyPy check. It all lives within a directory /myproj/generated/proto. Here's what…
Salim Fadhley
  • 6,975
  • 14
  • 46
  • 83
12
votes
1 answer

mypy: creating a type that accepts list of instances of subclasses

Suppose I have a Child class that is a subclass of Parent class, and a function that accepts a list of instances of Parent subclasses: from typing import List class Parent: pass class Child(Parent): pass def func(objects: List[Parent])…
kurtgn
  • 8,140
  • 13
  • 55
  • 91
12
votes
3 answers

Type hints: when to annotate

I'm using type hints and mypy more and more. I however have some questions about when I should explicitly annotate a declaration, and when the type can be determined automatically by mypy. Ex: def assign_volume(self, volume: float) -> None: …
JPFrancoia
  • 4,866
  • 10
  • 43
  • 73