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

django, mypy : queryset, choices. error: Incompatible types

We have: In models.py class Language(models.IntegerChoices): en = 1 ru = 2 class Translation(models.Model): language_id = models.PositiveIntegerField(choices=Language.choices) mnemonic = models.CharField(max_length=255) …
Yury Dr
  • 21
  • 4
1
vote
1 answer

How to define a generic type that is not a class?

I'd like to define a generic type. Something like: from typing import TypeVar, Sequence, Union, Generic T = TypeVar('T') RecurSeqOf = Sequence[Union[Generic[T], Sequence[T]]] # mypy error: Variable "typing.Generic" is not valid as a type Is there…
Min-Soo Pipefeet
  • 2,208
  • 4
  • 12
  • 31
1
vote
1 answer

Adding additional arguments to overridden functions without pylint complaining

I would like to define a function f in a superclass that has a set of required positional arguments, and permit subclasses to provide their own versions of f that share the required arguments, but also have additional named arguments. Further, I…
Ray
  • 1,706
  • 22
  • 30
1
vote
0 answers

Change PYTHONPATH so that mypy can find local module

My project is structured as follows: $ tree . -I venv . ├── mydir │   └── __init__.py ├── myotherdir │   └── t.py └── t.py 2 directories, 3 files Both t.py and myotherdir/t.py have the same content: $ cat t.py import mydir $ cat myotherdir/t.py…
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
1
vote
1 answer

Mypy: importing standard libary module from module with the same name

For a new application, I want to set up logging. The app is going to contain several modules, that will require the same kind of configuration. So I create a module my_company/logging.py: import logging # the standard Python logging def…
xtofl
  • 40,723
  • 12
  • 105
  • 192
1
vote
2 answers

Can I simplify deriving from a string type in Python?

I derive from str to get strongly typed string references. They look like below, which are redundant, and I'd like to avoid the redundancy: class VarRef(str): def __new__(cls, *args, **kw): return str.__new__(cls, *args, **kw) class…
edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267
1
vote
1 answer

Mypy and the walrus operator

I am using Python 3.8.5 and mypy 0.790. Running mypy on this code block will raise an error: # mypy_and_the_walrus.py z = "asd" if (p := z): print('hello') # error: invalid syntax Running the program is fine: ❯ python -i…
Algebra8
  • 1,115
  • 1
  • 10
  • 23
1
vote
1 answer

Callback protocols - when do we need the double underscore prefix?

The mypy docs read Callback protocols and :py:data:~typing.Callable types can be used interchangeably. Keyword argument names in :py:meth:__call__ methods must be identical, unless a double underscore prefix is used. For…
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
1
vote
1 answer

Python mypy type error with Union of callable and list of callable converted to list of callable

The argument hook can either be a function or a list of function. If it's a function, I convert it to a list so I can assume it's a list later. HookType = Union[Callable[[str], str], List[Callable[[str], str]]] ... def __init__( ... hook:…
cacharle
  • 13
  • 2
1
vote
1 answer

Mypy errors on enum auto() value

I have this class for auto-name enums. class AutoNameEnum(Enum): def _generate_next_value_(name, start, count, last_values): return name This is taken straight from the Python documentation on enums. Usage is like this class…
Kris Harper
  • 5,672
  • 8
  • 51
  • 96
1
vote
0 answers

Method Resolution Order (MRO) with mypy type check?

While working through a custom django mixin, I came across code with the following structure: class A: def method(self, i): # this is a method provided by django, so can't easily change this signature print(f"A: {i}") class B: def…
EricC
  • 1,355
  • 1
  • 20
  • 33
1
vote
0 answers

Null file object for optional file argument

Does the Python Standard Library have a standard tool to create a null file object? By this I mean a file object which would not be related to an existing file. It should also behave as a closed file (reading or writing would fail). Reasoning for…
1
vote
2 answers

Pattern matching over nested `Union` types in Python

Building a Python library, I am using type hints to guarantee consistency over certain data representation. In particular, I am making use of Union (sum types) in a nested fashion to represent the different "flavor" a datum can take. What I end up…
1
vote
0 answers

MyPy not considering dataclass attribute mechanics

I am developing a Python3.8 project with usage of typing and dataclasses, and automatic tests include mypy. This brings me in a strange behavior that I do not really understand... In short: Mypy seems not to understand dataclass attributes mechanics…
Joël
  • 2,723
  • 18
  • 36
1
vote
1 answer

Mypy errors out on Calling function with return type TypeVar of List of TypedDict

Im attempting to type hint functions in a class but I'm getting the following error in mypy The get_items function calls the scrapyRT API running in the background and outputs depending on the spider name and url the spider espn-matches returns a…