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
1
vote
1 answer

A dictionary with a given key type cannot be assigned as a dictionary of objects

It looks like a dictionary of SomeKeyType cannot be assigned as a dictionary of objects, even though all types are subtypes of object: x: dict[str, str] = {"a": "b"} y: dict[object, str] = x Incompatible types in assignment (expression has type…
Vincent
  • 12,919
  • 1
  • 42
  • 64
1
vote
1 answer

Mypy doesn't recognize class decorator

Problem Suppose I want to implement a class decorator that adds some attributes and functions to an existing class. In particular, let's say I have a protocol called HasNumber, and I need a decorator can_add that adds the missing methods to convert…
PIG208
  • 2,060
  • 2
  • 10
  • 25
1
vote
0 answers

use MyPy on unittest setUp and tearDown methods

I wonder how to apply mypy on unittest code. Situation: class TestPoint(unittest.TestCase): def setUp(self) -> None: self.point_origin = Point(0, 0, 0, name="origin") def tearDown(self) -> None: self.point_origin = None …
Johan Tuls
  • 105
  • 7
1
vote
1 answer

PyPI package: found module but no type hints or library stubs

I just made my first python package and uploaded it to PyPI (https://pypi.org/project/pygraphsearch/). I then made some test code that uses it. I ran pip install pygraphsearch to download my package. Everything works fine except mypy complains that…
1
vote
0 answers

Possible to annotate non-Optional field in mypy?

I have a class with some fields being Optional. Some uses of that class don't require those fields to be present, but others do. Is there a way to get mypy to diagnose those misuses? Example: import attr from typing import…
Barry
  • 286,269
  • 29
  • 621
  • 977
1
vote
2 answers

Kwargs in a Protocol implementer: what is a valid signature?

My question is simple. I have this protocol: from typing import Protocol class LauncherTemplateConfig(Protocol): def launch_program_cmd(self, **kwargs) -> list[str]: pass And this implementation of the protocol, which I would expect…
Germán Diago
  • 7,473
  • 1
  • 36
  • 59
1
vote
1 answer

How to use typing hints with an optional first parameter

Introduction I have a function that is either called as fun(start, stop, divisors) or fun(stop, divisors). I want to call the parameters in this specific order. I want to implement it in a way that does not give any type-hinting errors. If I ease…
N3buchadnezzar
  • 471
  • 7
  • 12
1
vote
1 answer

Calling a helper function from inside a class variable

Consider the following code: # foo.py class A: def _foo(): print('hello world') bar = {'foo': _foo} def run_bar(self): self.bar['foo']() def main(): A().run_bar() if __name__ == '__main__': raise…
1
vote
1 answer

How do I type annotate using any type as a key in a Python dictionary?

In Python, types are first class objects. As such, I can create a dictionary with types as the keys. For example: mydict = { int: "Hello!", str: 12, SomeClassName: "Hello again!"} Assuming that my dictionary will be populated with…
Eric Evans
  • 94
  • 10
1
vote
1 answer

Have a method in a subclass accept a subclass of an argument in its parents signature

I think it's easiest if I provide a PoC snippet instead of explaining it with words: from abc import ABCMeta, abstractmethod from typing import Any, Dict class A: a: int class B(A): b: str class Getter: @abstractmethod def…
TheEdgeOfRage
  • 565
  • 1
  • 8
  • 21
1
vote
1 answer

How can I pass a type hint that MyPy will accept as a parameter to a function which defines a function internally?

I'm trying to write unit tests for types we're using with beartype, our runtime type checker. The tests run as expected, however MyPy will not accept the code. The following code works, however the MyPy check fails. def typecheck(val: Any, t:…
Eric Yabs
  • 11
  • 3
1
vote
1 answer

pylintrc configuration in visual studio code

I am currently trying to configure pylint to ensure high coding standards in a particular project. My problem is that somehow I am not really able to configure pylint the way I would like. First I tried to use the normal settings in VSC to configure…
Jan
  • 23
  • 1
  • 6
1
vote
0 answers

mypy error: Cannot find implementation or library stub for module

I am trying to set MYPYPATH to find a library located in another directory. I am importing a function like this: from my_module import my_function The file structure: function |__lib1 | |__lib2 | |__my_module.py |__function1 |__src …
dflss
  • 43
  • 5
1
vote
0 answers

Delegating static type-checking to an attribute

In Python 3.9, I'm trying to unify type and instance checks with type hint annotation for static type checking in one class. Because get_origin and get_type_hints are still in flux and I don't want to build a parser for the different typing syntax,…
1
vote
1 answer

How to fix "mypy failed with error: Daemon has died . See Output panel for details." error in VS Code

I'm using the Mypy extension in VSCode, and it's giving me the error mypy failed with error: Daemon has died . See Output panel for details. When I open the Output panel, it doesn't give me much more info than just telling my that it tried to run…
Pro Q
  • 4,391
  • 4
  • 43
  • 92