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

mypy: argument of method incompatible with supertype

Look at the example code (mypy_test.py): import typing class Base: def fun(self, a: str): pass SomeType = typing.NewType('SomeType', str) class Derived(Base): def fun(self, a: SomeType): pass now mypy complains: mypy…
anon
18
votes
3 answers

Python 3.6: Signature of {method} incompatible with super type {Class}

While trying to update my code to be PEP-484 compliant (I'm using mypy 0.610) I've ran into the following report: $ mypy mymodule --strict-optional --ignore-missing-imports --disallow-untyped-calls --python-version 3.6 myfile.py:154: error:…
Pawel
  • 343
  • 1
  • 2
  • 7
18
votes
2 answers

How to use static type checking using Dict with different value types in Python 3.6?

Trying to use static types in Python code, so mypy can help me with some hidden errors. It's quite simple to use with single variables real_hour: int = lower_hour + hour_iterator Harder to use it with lists and dictionaries, need to import…
sortas
  • 1,527
  • 3
  • 20
  • 29
18
votes
3 answers

How do I get a regex pattern type for MyPy

If I compile a regex >>> type(re.compile("")) And want to pass that regex to a function and use Mypy to type check def my_func(compiled_regex: _sre.SRE_Pattern): I'm running into this problem >>> import _sre >>> from…
AlexLordThorsen
  • 8,057
  • 5
  • 48
  • 103
18
votes
3 answers

Why does defining the argument types for __eq__ throw a MyPy type error?

I'm using Python 3.5.1 and the newly released MyPy v0.4.1 static type analyzer. I have some more complex code that I've reduced down to this simplest possible python class needed to reproduce the error: class MyObject(object): def __init__(self,…
Nick Sweeting
  • 5,364
  • 5
  • 27
  • 37
17
votes
2 answers

How to declare a Protocol with a field which supports both a simple type and property?

(Related, but not duplicated: How to annotate attribute that can be implemented as property?) I want to create a Protocol, in which a field can be implemented by both a simple type and property. For example: class P(Protocol): v:…
tamuhey
  • 2,904
  • 3
  • 21
  • 50
17
votes
3 answers

How can mypy accept pydantic's constr() types?

I have this code: from pydantic import BaseModel, constr DeptNumber = constr(min_length=6, max_length=6) class MyStuff(BaseModel): dept: DeptNumber ms = MyStuff(dept = "123456") deptnr.py:6: error: Variable "deptnr.DeptNumber" is not valid…
Robert
  • 7,394
  • 40
  • 45
  • 64
17
votes
3 answers

How to type hint with an optional import?

When using an optional import, i.e. the package is only imported inside a function as I want it to be an optional dependency of my package, is there a way to type hint the return type of the function as one of the classes belonging to this optional…
dspencer
  • 4,297
  • 4
  • 22
  • 43
17
votes
1 answer

Incompatible types when assigning an empty tuple to a specialised variable

I have a variable path, which should be a tuple of strings. I want to start with it set to an empty tuple, but mypy complains. path: Tuple[str] = () The error is: Incompatible types in assignment (expression has type "Tuple[]", variable has type…
z0r
  • 8,185
  • 4
  • 64
  • 83
17
votes
1 answer

Python type hints - better syntax for cast()?

I recently started using type hints in my code, and have so far found them to be (mostly) very helpful. However, one thing that I really do not like is the syntax to force the type checker to assume that a variable is of a certain type. Given this…
0x5453
  • 12,753
  • 1
  • 32
  • 61
16
votes
1 answer

Python typing: Narrowing type from function that returns a Union

I have difficulties finding return types that satisfy mypy. I have two functions. The first one returns a Union type, since the type depends on the parameter given to the function. The second function calls the first one with a default parameter.…
Auss
  • 451
  • 5
  • 9
16
votes
3 answers

How to get an abstract dataclass to pass mypy?

mypy v0.910 rejects abstract dataclasses in Python 3.9. Here's the Minimal Reproducible Example: from abc import ABC, abstractmethod from dataclasses import dataclass @dataclass class Liquid(ABC): @abstractmethod def drip(self) -> None: …
16
votes
2 answers

What is the type hint for Pytest's "caplog" fixture?

As the title says. I am using the caplog fixture that comes with pytest. I am using mypy for my type checking, and would like to know what the correct type hint for caplog is. For example: def test_validate_regs(caplog: Any) -> None: …
Jamie
  • 1,530
  • 1
  • 19
  • 35
16
votes
1 answer

Copy type signature from another function

Imagine I have a set of functions like below. foo has a lot of arguments of various types, and bar passes all its arguments to that other function. Is there any way to make mypy understand that bar has the same type as foo without explicitly copying…
Kyuuhachi
  • 651
  • 6
  • 15
16
votes
2 answers

Why isn't this function type-annotated correctly (error: Missing type parameters for generic type)?

Is this function type-annotated correctly? import subprocess from os import PathLike from typing import Union, Sequence, Any def run(shell_command: Union[bytes, str, Sequence[Union[bytes, str, PathLike]]], **subprocess_run_kwargs: Any) -> int: …