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 Async decorator preserve typing

For the following file: from abc import ABC, abstractmethod from typing import Any, Awaitable, Callable, TypeVar, cast T = TypeVar('T') def dec(args: Any) -> Callable[..., Awaitable[T]]: def dec2(f: Callable[..., Awaitable[T]]) ->…
1
vote
1 answer

List[Optional[int]] type checking when assigning

mypy seems smart enough to detect that after checking an optional value for None the Optional part of the type is disregarded. Meaning: declaring value: Optional[int] would result in value behaving like an int after if value is not None. I think I…
Ashesh
  • 13
  • 6
1
vote
2 answers

How do I tell MyPy that errors about os.path are not errors?

In VS Code, the Problems tab is showing errors when using os.path.join, but the code actually runs. How do I tell MyPy that these are not errors? I'm working on the Salome platform, and the existing code that runs shows the following as an error:…
1
vote
1 answer

mypy: Signature of "__add__" incompatible with supertype "tuple" - but everything fine with __sub__

I am making a simple vector class, and I'm struggling to understand how mypy is working on my __add__ and __sub__ methods (in particular the difference in the mypy output in Code 1 vs Code 3 below). Code 1: from typing import NamedTuple class…
Frank
  • 223
  • 3
  • 11
1
vote
0 answers

Complex annotations concerning types as arguments in python

As a toy example, consider a function that takes two types A, B and returns on object A(B). The standard example for this would be A = defaultdict and B e.g. int. How can I annotate something like this? The closest I got is from collections import…
CallMeStag
  • 5,467
  • 1
  • 7
  • 22
1
vote
2 answers

Unsupported target for indexed assignment

Function runs perfectly, but I am getting error with mypy: Unsupported target for indexed assignment Here is my code: def function(image: List[str], start: Tuple[int]): if image[start[0]][start[1]] == "*": return else: …
MV912HB
  • 51
  • 1
  • 6
1
vote
3 answers

Expressing a dictionary that returns string and list of values in my py

I am new to python and very new to using types. I have some function that returns a dictionary where the key is a string and value is a list of strings. I want to know what is the correct way to express this using types. Sample output {"lebron":…
Sluna
  • 169
  • 10
1
vote
1 answer

Mypy: Generic container with some methods only valid if extra protocols apply

In mypy, how would you specify that a type Generic over T has methods that are only valid if T meets certain conditions? For example, if we made a custom collection class with a min method, returning the smallest element in the collection: from…
Oliver Rice
  • 768
  • 8
  • 20
1
vote
1 answer

mypy factory method with bound TypeVar

I have defined and used a factory function roughly with the following code: import typing as t from pydantic import BaseModel M = t.TypeVar("M", bound=t.Union[t.Dict, BaseModel]) def foo(factory: t.Type[M]) -> M: ... return…
hangc
  • 4,730
  • 10
  • 33
  • 66
1
vote
0 answers

In mypy why does boolean pass float type annotation

The example below returns a boolean instead of a float. This passes mypy and shows no errors in the PyCharm editor. def add(a: float, b: float) -> float: return False You can test this using the online mypy checker…
Neil
  • 8,925
  • 10
  • 44
  • 49
1
vote
0 answers

Mypy import * in python

Suppose I have a package with an __init__.py like this: # ifpy init script import ifpy.world as world import ifpy.grammar as grammar import ifpy.util as util import ifpy.cli as cli __all__ = [*world.__all__, *grammar.__all__, *util.__all__,…
Th31AndOnly
  • 45
  • 1
  • 6
1
vote
1 answer

Proper typing for class function decorator

What would be the proper way to type the print_before function decorator, so that the wrapped function has the proper type but I can't use the decorator on a class that would not work? thank you def print_before(func): def func_wrapper(self,…
Archaeron
  • 767
  • 1
  • 7
  • 15
1
vote
2 answers

How to use default values in __init__ in a Generic class?

When a Generic class has a default value in an init parameter, MyPy complains about the possibility of a type mismatch even though the init parameter defines the type. Consider the following minimal example class…
Josiah
  • 1,072
  • 6
  • 15
1
vote
0 answers

VS code not underlining errors

Problem: VS Code is not underlining errors (with wavy red underlining) as it used to. Problem exists for me and my friend (macOS and Windows) Linter: mypy settings.json: { "python.pythonPath":…
smiffy
  • 75
  • 1
  • 8
1
vote
2 answers

How do I make a type annotation for a list of subclass instances, e.g to concatenate two lists?

I want to iterate over List[A] and List[Subclass of A] and do the same loop. The best way I can see to do that is to concatenate the two lists. However, mypy is not happy about it. How can I concatenate the two and keep mypy happy? Currently, I do #…
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
1 2 3
99
100