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
0 answers

mypy - [mypy-foo.*] does not work in config file

My mypy.ini, located in .../viv/mypy.ini looks like this: [mypy] mypy_path = $MYPY_CONFIG_FILE_DIR/stubs ignore_missing_imports = true allow_untyped_globals = true I want those two ignore settings only to apply to a certain subfolder…
Greedo
  • 4,967
  • 2
  • 30
  • 78
1
vote
1 answer

Should a Python function that doesn't return a value have `-> None` in its signature?

When writing a typed function in Python that does not return a value (like "void" in other languages), is it best and/or conventional to mark it as follows? def say_hello(name: str) -> None print(f"Hello, {name}.") Alternatively, should one…
Noldorin
  • 144,213
  • 56
  • 264
  • 302
1
vote
0 answers

Typehint addition of lists in python mypy where the lists have different structures

Example: # check.py x = [("a", "b"), ("c", "d")] y = [("x", [("1", "2"), ("3", "4")])] z = x + y where mypy check.py returns: check.py:5: error: Unsupported operand types for + ("List[Tuple[str, str]]" and "List[Tuple[str, List[Tuple[str, str]]]]")…
baxx
  • 3,956
  • 6
  • 37
  • 75
1
vote
0 answers

Strongly typed enums for python (mypy)

I am using mypy for typing but it doesn't work well for Enum Most of the enums I define are only integer mappings For eg. class Role(Enum): ENG = 0 PRODUCT = 1 BUSINESS = 2 FOUNDER = 3 When I make an enum and try to get its value…
1
vote
1 answer

Django and Mypy : unable to read config options

I am attempting to use mypy with django, following the tutorial here: django-mypy-check-runs I wasn't able to use the init.py import as per the article, instead I used the same code in the ready function of the apps.py in my top level app. mypy runs…
miller the gorilla
  • 860
  • 1
  • 7
  • 19
1
vote
1 answer

How to specify an instance with a subclass without typing hint errors?

I'm using instance __class__ attribute to specify an instance created from the super class. But MyPy rejects it with Incompatible return value type. Is there a most pythonic option to do that without ignoring it? Here is my example code: class A: …
1
vote
1 answer

Where can I find the type annotations for methods of built-in types like set.symmetric_difference?

I know that built-ins for CPython are implemented in C. But I'm interested in the type-hints, or annotations, for the methods of the built-in types. For example, I'd like to know how set.symmetric_difference_update()is annotated. But I can't find…
Rich Holton
  • 662
  • 5
  • 12
1
vote
0 answers

How do I make a generic reference type in mypy that also accepts own subclasses?

I am using Pydantic on top of MongoDB, and things are working pretty well including documentation for the IDs I'm describing here. However, I want to be able to type references between different documents as well. I was able to make it work halfway…
1
vote
1 answer

mypy: Missing positional argument

I am using mypy as linter and a bit confused how default arguments are treated. I use following type annotation: def func(dict_1: Dict[str, Any], dict_2: Optional[Dict[str, str]]) and real function is signature is: def func(dict_1,…
Beliaev Maksim
  • 968
  • 12
  • 21
1
vote
1 answer

How to annotate mixin in Python

I have a free-standing function, that uses super(), and using it like this: import typing from abc import ABC class Foo(typing.Protocol): def foo(): raise NotImplementedError def free_foo(child): super(child.__class__, child).foo()…
Stormwalker
  • 351
  • 1
  • 12
1
vote
2 answers

How to correctly type an asyncio class instance variables

Consider the following example class containing attributes that require running a coroutine for initialization: class Example: def __init__(self) -> None: self._connection: Optional[Connection] = None async def connect() -> None: …
Lefty
  • 407
  • 5
  • 12
1
vote
1 answer

Make MyPy properly detect type of the descriptor used within Enum class

Problem description I've been trying to add MyPy support for my implementation of a slightly modified Enum class, where it is possible to define a certain containers for enum values. My enum and descriptor classes definitions (enums.py file): from…
Waszker
  • 233
  • 3
  • 10
1
vote
1 answer

Mypy type checker and "static instances"

For a class A I wrote, there are some instances foo and bar that I want to be accessible through A.foo and A.bar as class variables. However, foo and bar are both instances of A, and I'm not sure how to let the typechecker mypy handle this…
Knaapje
  • 165
  • 1
  • 1
  • 10
1
vote
1 answer

Python Typing: Type literal

I am writing a function that takes an argument dtype of type type as shown below. def my_function(dtype: type): pass Is there a way to restrict the value of dtype only to str and int? The type union str | int will not do the job because it will…
Troll
  • 1,895
  • 3
  • 15
  • 34
1
vote
1 answer

Compare two type annotations (possibly generics) to see if they fit

I'm looking for a function that can say if a type annotation is a subset of another. It could be in the standard library or 3rd-party. Since type-checkers such as mypy and pytype have solved this problem, I assume there is some function that can do…
Uri
  • 25,622
  • 10
  • 45
  • 72