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

incompatible signature with supertype - abstractmethods vs Liskov for enforcing naming

I have an ABC class that I am deriving from to make some children. Each of the children have the same general structure, and the same methods, but those methods need to take different arguments (different variants of the same type of model). I also…
Jack
  • 271
  • 3
  • 8
1
vote
1 answer

No overload variant of "next" matches argument type List[Scene]

I get this mypy error No overload variant of "next" matches argument type List[Scene] when I am trying to check my following code. class Scene: def __init__(self): self.a = None def something(self): self.a = 2 class Game: …
Pritam
  • 333
  • 3
  • 13
1
vote
1 answer

Is there an equivalent of typedefs for mypy?

Sometimes when coding, I want "special sorts of strings" and "special sorts of integers" for documentation. For example you might have. def make_url(name:str) -> URL: where URL is really a string. In some languages like C you can use a typedef for…
Att Righ
  • 1,439
  • 1
  • 16
  • 29
1
vote
0 answers

How to make generic type variable?

Having generic pydantic model Localized[T], I want to make shortcut type for Union[T, Localized[T]]. I tried to make shortcut like this: MaybeLocalized = TypeVar("MaybeLocalized", bound=Union[T, Localized[T]]) but got an error: >>…
aiven
  • 3,775
  • 3
  • 27
  • 52
1
vote
1 answer

Typehint function which takes mixed type dictionary and returns mixed type dict with mypy

I'm wondering how to type hint the following: def f(d, n): return {(n, key): d[key] for key in d} dd = { 1.0: 10.2, 2.0: 11.8, "T1": 300, "T2": 300, } nn = "Name" f(d = dd, n = nn) # returns: # {('Name', 1.0): 10.2, # ('Name',…
baxx
  • 3,956
  • 6
  • 37
  • 75
1
vote
2 answers

How to type annotate dict comprehension?

Mypy considers this to be valid with strict = true: from typing import Dict, TypeVar KeyType = TypeVar("KeyType") ValueType = TypeVar("ValueType") class InvertibleDict(Dict[KeyType, ValueType]): def __inverse__(self) ->…
l0b0
  • 55,365
  • 30
  • 138
  • 223
1
vote
1 answer

Typehint list with strings and lists of strings in mypy

How would one type hint the following: def f(x : ) -> None: print(x) Where x is a list containing strings, and lists of strings. Eg: # example 1 ['a', 'sdf', ['sd', 'sdg'], 'oidsf d', 'as', ['asf','asr']] # example 2 [['as', 'asfd'], ['oei',…
baxx
  • 3,956
  • 6
  • 37
  • 75
1
vote
1 answer

Class whose instances are treated as Any by mypy with --strict-equality

I'm creating a library of matchers that are used by comparing against values with ==. Unfortunately, when comparing a compound type containing a matcher instance against a value with a non-Any type, like so: assert {"foo": SomeMatcher(arg)} ==…
jwodder
  • 54,758
  • 12
  • 108
  • 124
1
vote
1 answer

Typehint a list of tuples with nested tuples in python

I'm trying to type hint the following: from typing import * x = [('a',), (('b',), ('c', 'd'))] f( k : list[ tuple[str] | tuple[str, tuple[str]]]): print(k) and am unsure how to type-hint for this list.
baxx
  • 3,956
  • 6
  • 37
  • 75
1
vote
1 answer

Mypy- Report Generation for only specified files

I'm looking at the doc here: Is there a way to ignore certain files when making a report? I'm seeing some files I don't want a report of, and I'm wondering if I can make a runtime configuration or somthing to ignore them.
Isaac
  • 273
  • 3
  • 19
1
vote
2 answers

python type hints for none or something

consider the following snippet uri_match: Optional[Match[str]] = re.match(r"https//(.+?)/(.+)", "bla bla") re.match has type Match or None. res = uri_match.group(1) This line says that None has no member group. Which means that the type checker…
Sakibul Alam
  • 63
  • 1
  • 6
1
vote
1 answer

(MyPy) Missing type parameters for generic type "ModelAdmin"

When I run mypy, I get this error: emails/admin.py:43: error: Missing type parameters for generic type "ModelAdmin" And this is the corresponding code snippet: @admin.register(Message) class MessageAdmin(admin.ModelAdmin): list_display =…
Nick An
  • 27
  • 1
  • 5
1
vote
1 answer

Is it possible to statically type a generic C[T] as T in Python?

I am working with SQLite where type hints are still far from perfect, and I ended up with a MCVE of what I think I want to do, and it may be related to statically-typing ABCs. Example: T = TypeVar('T') class ColumnarValue(Generic[T]): …
ssice
  • 3,564
  • 1
  • 26
  • 44
1
vote
1 answer

Using mypy to type check and i cant figure out why this errors are happening

So, i using mypy to learn how to code in python using type check from the beginning. I'm using this code to train: def stars(*args: int, **kwargs: float) -> None: for arg in args: print(arg) for key, value in kwargs: …
1
vote
1 answer

getting started with mypy / ignoring external libs

I'm trying to run mypy type hints and just get a lot of errors for external libraries. I've checked this topic in the docs and have a mypy.ini like this: # Global options: [mypy] python_version = 3.8 ; warn_return_any = True ; warn_unused_configs =…
dcsan
  • 11,333
  • 15
  • 77
  • 118