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
525
votes
4 answers

How can I specify the function type in my type hints?

How can I specify the type hint of a variable as a function type? There is no typing.Function, and I could not find anything in the relevant PEP, PEP 483.
Jon
  • 11,356
  • 5
  • 40
  • 74
229
votes
5 answers

How can mypy ignore a single line in a source file?

I'm using mypy in my python project for type checking. I'm also using PyYAML for reading and writing the project configuration files. Unfortunately, when using the recommended import mechanism from the PyYAML documentation this generates a spurious…
Pridkett
  • 4,883
  • 4
  • 30
  • 47
130
votes
3 answers

How to annotate function that takes a tuple of variable length? (variadic tuple type annotation)

I have a function that takes a tuple of different lengths as an argument: from typing import Tuple def process_tuple(t: Tuple[str]): # Do nasty tuple stuff process_tuple(("a",)) process_tuple(("a", "b")) process_tuple(("a", "b", "c")) When…
Montreal
  • 2,143
  • 5
  • 18
  • 28
121
votes
1 answer

What is the difference between TypeVar and NewType?

TypeVar and NewType seem related but I'm not sure when I'm supposed to use each or what the difference is at runtime and statically.
Hatshepsut
  • 5,962
  • 8
  • 44
  • 80
109
votes
4 answers

mypy, type hint: Union[float, int] -> is there a Number type?

mypy is really handy and catches a lot of bugs, but when I write "scientific" applications, I often end up doing: def my_func(number: Union[float, int]): # Do something number is either a float or int, depending on the user's input. Is there an…
JPFrancoia
  • 4,866
  • 10
  • 43
  • 73
106
votes
7 answers

Python type hints and context managers

How should a context manager be annotated with Python type hints? import typing @contextlib.contextmanager def foo() -> ???: yield The documentation on contextlib doesn't mention types much. The documentation on typing.ContextManager is not…
Peter
  • 3,322
  • 3
  • 27
  • 41
93
votes
3 answers

Type annotation for classmethod returning instance

How should I annotate a @classmethod that returns an instance of cls? Here's a bad example: class Foo(object): def __init__(self, bar: str): self.bar = bar @classmethod def with_stuff_appended(cls, bar: str) -> ???: …
taway
  • 1,099
  • 1
  • 7
  • 9
93
votes
3 answers

Proper type annotation of Python functions with yield

After reading Eli Bendersky's article on implementing state machines via Python coroutines I wanted to... see his example run under Python3 and also add the appropriate type annotations for the generators I succeeded in doing the first part (but…
ttsiodras
  • 10,602
  • 6
  • 55
  • 71
83
votes
4 answers

Specify length of Sequence or List with Python typing module

I'm giving the Python typing module a shot. I know that it's valid to specify the length of a List like the following*: List[float, float, float] # List of 3 floats <-- NOTE: this is not valid Python Is there any shorthand for longer lists? What…
John Brodie
  • 971
  • 1
  • 6
  • 5
80
votes
8 answers

Python equivalent of Typescript interface

Recently I have been working with Typescript a lot, it allows to express things like: interface Address { street: string; housenumber: number; housenumberPostfix?: string; } interface Person { name: string; adresses:…
Otto
  • 1,787
  • 1
  • 17
  • 25
73
votes
2 answers

How to use reveal_type in mypy

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.
Yuval Pruss
  • 8,716
  • 15
  • 42
  • 67
59
votes
4 answers

Python >=3.5: Checking type annotation at runtime

Does the typing module (or any other module) exhibit an API to typecheck a variable at runtime, similar to isinstance() but understanding the type classes defined in typing? I'd like to be to run something akin to: from typing import List assert…
Bertrand Caron
  • 2,525
  • 2
  • 22
  • 49
59
votes
4 answers

How to use type hints in python 3.6?

I noticed Python 3.5 and Python 3.6 added a lot of features about static type checking, so I tried with the following code (in python 3.6, stable version). from typing import List a: List[str] = [] a.append('a') a.append(1) print(a) What surprised…
Searene
  • 25,920
  • 39
  • 129
  • 186
58
votes
2 answers

Specifying a type to be a List of numbers (ints and/or floats)?

How do I specific a function can take a list of numbers which can be ints or floats? I tried making a new type using Union like so: num = Union[int, float] def quick_sort(arr: List[num]) -> List[num]: ... However, mypy didn't like this: …
Solomon Bothwell
  • 1,004
  • 2
  • 12
  • 21
55
votes
9 answers

How do I correctly add type-hints to Mixin classes?

Consider the following example. The example is contrived but illustrates the point in a runnable example: class MultiplicatorMixin: def multiply(self, m: int) -> int: return self.value * m class AdditionMixin: def add(self, b:…
exhuma
  • 20,071
  • 12
  • 90
  • 123
1
2 3
99 100