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

Python mypy Annotation Decorators __call__

I'm trying to annotate a decorator implemented as a class, but mypy seems to either lose the annotation or lose the type and think it's an Any. What I am trying to annotate: class my_decorator: def __init__(self, func): self.func = func …
1
vote
1 answer

Refactor to meet mypy requirements

I have a codebase which I would like to validate using mypy. In the current design, it is very common that a class may have a non-primitive member that can be set later after __init__. So, in __init__ the member is initialized to None and its type…
Elad Weiss
  • 3,662
  • 3
  • 22
  • 50
1
vote
0 answers

Type hinting and ctypes foreign functions

How should I type-hint a foreign function defined with Python's ctypes? Do I need to repeat the information provided in the restype and argtypes attributes? Or is there a method to enable mypy to somehow comprehend the attributes? For example,…
Matthew Walker
  • 2,527
  • 3
  • 24
  • 30
1
vote
2 answers

How can I use MyPy to overload the __init__ method to adjust a getter's return value?

Let's say I have a class like this (pseudo-code, please ignore the odd db structure): class Blog(): title = StringProperty() comments = StringProperty(repeated=True) I want to type check StringProperty such that Blog().title returns a str…
YPCrumble
  • 26,610
  • 23
  • 107
  • 172
1
vote
1 answer

List of Tox-supported tools

How can I know if tox offer support for a specific tool? To be specific, I'd like to know why this tox.ini section works for flake8: [flake8] max-line-length = 120 # works like a charm [testenv:flake8] deps = flake8 commands = flake8…
wecx
  • 302
  • 2
  • 10
1
vote
0 answers

Ensuring that mypy gets the hint that compared values are always of same types when comparing Unions of multiple types

I get the following traceback that mypy throws: dust/core/validator/_validators.py:504: error: Unsupported operand types for < ("str" and "ValidateProperty") dust/core/validator/_validators.py:504: error: Unsupported operand types for < ("float" and…
qw4r9cal
  • 11
  • 2
1
vote
0 answers

How to annotate a variable of an existing object? (working with mypy)

def get_source(import_name: str) -> Optional[str]: spec = get_spec(import_name) # Now, here. The spec.loader might be one of several values. # But I *know* that it is gonna be importlib.machinery.SourceFileloader # I need to typecast…
Işık Kaplan
  • 2,815
  • 2
  • 13
  • 28
1
vote
0 answers

python abstract method with more specific arguments

I am getting a type warning when implementing an abstractmethod in a more explicit way, why is that wrong? I have the following interface from typing import Any from abc import abstractmethod, ABCMeta class SaveDataInterface(metaclass=ABCMeta): …
moshevi
  • 4,999
  • 5
  • 33
  • 50
1
vote
1 answer

Function with generic return type causing typing issues in calling functions with specific type

I have a generic lookup function, that mostly returns TypeA, but sometimes can return TypeB: Types = Union[TypeA,TypeB] def get_hashed_value( key:str, table: Dict[str,Types] ) -> Types: return table.get(key) and I use it in two less-generic…
1
vote
1 answer

mypy updating return value from child method using child's name instead of parent's method's generic signature

I have a Generic base class that returns itself in one method (get_self). I have type hinted it as such. I then have a child class of that base class that passes in a type arg for the Generic. In that child class, I call get_self. I would like to…
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
1
vote
1 answer

python3 raising attribute-error on type annotation

Some background information: I am using mypy_protobuf package. For the type checking purpose it generates .pyi files, and for every enum wrapper Xxx in module mmm it will generate type mmm.XxxValue. So I have a function. def aaa(aaa: mmm.XxxValue)…
uuu777
  • 765
  • 4
  • 21
1
vote
1 answer

Mypy: Type of dict value doesn't change with assignment while type of variable does

In the following example, why does the type of the variable foo change when it's value is set to an integer but the same doesn't occur for the type of bar["foo"]? import typing as tp foo: tp.Union[float, int] D = tp.TypedDict("D", {"foo":…
SuperShoot
  • 9,880
  • 2
  • 38
  • 55
1
vote
0 answers

mypy fails to detect errors when Callable return type is bool/numeric

The following file, test.py, has multiple type errors due to return types of Callable's not matching. from typing import Callable def func() -> bool: return True f1: Callable[[], bool] = func # okay f2: Callable[[], float] = func # should…
Dave Doty
  • 285
  • 1
  • 9
1
vote
1 answer

Getting Too many arguments or Too few arguments on nested dict

I'm trying to add type hints in a dict type which has multiple fields that bind functions to them. e.g. from typing import Dict, Callable, Any, Union def fn(): print("Hello World") def fn2(name): print("goodbye world", name) d = { "hello"…
shriek
  • 5,605
  • 8
  • 46
  • 75
1
vote
1 answer

Incompatible types in assignment when creating a dict out of two lists

When joining two lists into a dict, mypy complains about incompatible types in the assignment. Like this: from typing import Dict d = Dict[str, int] ln = ['a', 'b', 'c'] lc = [3, 5, 7] d = dict(zip(ln, lc)) print(ln) print(lc) print(d) The output…
Windowlicker
  • 498
  • 11
  • 18
1 2 3
99
100