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
16
votes
3 answers

How to specify return type in an async Python function?

In TypeScript, you would do something like async function getString(word: string): Promise { return word; } How can I do the same in Python? I tried the following: async def get_string(word: str) -> Coroutine[str]: return word And…
Ron Serruya
  • 3,988
  • 1
  • 16
  • 26
16
votes
1 answer

How can I add type-annotations to dynamically created classes?

In one application I have code which generates dynamic classes which reduces the amount of duplicated code considerably. But adding type-hints for mypy checking resulted in an error. Consider the following example code (simplified to focus on the…
exhuma
  • 20,071
  • 12
  • 90
  • 123
15
votes
3 answers

MyPy configuration - exclude multiple directories

We're currently using Mypy (v 0.910) in our project with pyproject.toml for configuration. I have the following file structure: src --app --generated --service --data --ingest pyproject.toml: ... [tool.mypy] python_version =…
James B
  • 447
  • 3
  • 15
15
votes
1 answer

Proper type hint for functools.partial

What's the proper type hint for functools.partial? I have a function that returns a partial and I want to type hint it so mypy doesn't throw any error: def my_func() -> ?: return partial(foo, bar="baz") More specific than typing.Callable
A23149577
  • 2,045
  • 2
  • 40
  • 74
15
votes
1 answer

Using mypy with with lazy initialization of instance attributes

UPDATE: trying to check/fill values in another function I'm trying to use mypy in my projects, but many of the instance attributes I use are only initialized after __init__, and not inside it. However, I do want to keep the good practice of…
Beka
  • 725
  • 6
  • 22
15
votes
2 answers

"Unsupported target for indexed assignment" with mypy, depending on type hinting moment with respect to assignment

I'm trying to do some typing on my python code, and I got the following mypy error: "Unsupported target for indexed assignment" On a simplified example, it amounts to the following code: from pathlib import Path from typing import (Literal,…
bli
  • 7,549
  • 7
  • 48
  • 94
15
votes
5 answers

How do I annotate a Python function to hint that it takes the same arguments as another function?

Is there any Python type-hinting syntax to state that a function takes the same parameters (and parameter types) as another function? In particular this is useful for wrapping, e.g., async def do_stuff( param1: str, param2: int, …
lxop
  • 7,596
  • 3
  • 27
  • 42
15
votes
1 answer

Variable "foo_class" is not valid as type, but why?

I have something similar to this: from typing import Type class Foo: pass def make_a_foobar_class(foo_class: Type[Foo]) -> Type[Foo]: class FooBar(foo_class): # this.py:10: error: Variable "foo_class" is not valid as a type …
frnhr
  • 12,354
  • 9
  • 63
  • 90
15
votes
4 answers

Type-check Jupyter Notebooks with mypy

I have a project containing a bunch of Python modules (.py files) and a bunch of Jupyter Notebooks (.ipynb files) which import things from the Python modules. I can (assuming I've got __init__.py files in all subfolders) type-check all the .py files…
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
15
votes
2 answers

Typing Decorator with Parameters in MyPy with TypeVar yields expected uninhabited type

MyPy has some issues with Callable *args and **kwargs, particularly concerning decorators as detailed in: https://github.com/python/mypy/issues/1927 Specifically, for a decorator with no parameters which only wraps a function (and does not change…
Bailey Parker
  • 15,599
  • 5
  • 53
  • 91
14
votes
2 answers

Type hint for return value in subclass

I am writing a CustomEnum class in which I want to add some helper methods, that would then be available by the classes subclassing my CustomEnum. One of the methods is to return a random enum value, and this is where I am stuck. The function works…
HitLuca
  • 1,040
  • 9
  • 33
14
votes
1 answer

Type-checked conversion between Dataclasses and TypedDicts

I have a bunch of @dataclasses and a bunch of corresponding TypedDicts, and I want to facilitate smooth and type-checked conversion between them. For example, consider from dataclasses import dataclass from typing_extensions import…
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
14
votes
3 answers

Type hints for lxml?

New to Python and come from a statically typed language background. I want type hints for https://lxml.de just for ease of development (mypy flagging issues and suggesting methods would be nice!) To my knowledge, this is a python 2.0 module and…
Ian
  • 301
  • 3
  • 6
14
votes
2 answers

How to subclass a dictionary so it supports generic type hints?

How can a dictionary be subclassed such that the subclass supports generic type hinting? It needs to behave like a dictionary in every way and support type hints of the keys and values. The subclass will add functions that access and manipulate the…
Tom Jordan
  • 141
  • 1
  • 7
14
votes
1 answer

mypy gives an error while importing submodule : Module has no attribute

When I am checking my modules through mypy it gives me this error: Module 'django.contrib.gis' has no attribute 'forms' and I am importing forms like this: from django.contrib.gis import forms I know it is correct but mypy shows this error…
Liya
  • 141
  • 1
  • 1
  • 4