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
22
votes
1 answer

How does mypy use typing.TYPE_CHECKING to resolve the circular import annotation problem?

I have the following structure for a package: /prog -- /ui ---- /menus ------ __init__.py ------ main_menu.py ------ file_menu.py -- __init__.py __init__.py prog.py These are my import/classes statements: prog.py: from prog.ui.menus import…
pstatix
  • 3,611
  • 4
  • 18
  • 40
21
votes
2 answers

Python: How to write typing.overload decorator for bool arguments by value

The example code of what I am trying to ask is below. None of the examples on the internet try to overload argument value as such. One of the argument is a bool value and I want to overload a method based on the bool value rather than the usual,…
dumbPy
  • 1,379
  • 1
  • 6
  • 19
21
votes
3 answers

Does mypy have a Subclass-Acceptable Return Type?

I'm wondering how (or if it is currently possible) to express that a function will return a subclass of a particular class that is acceptable to mypy? Here's a simple example where a base class Foo is inherited by Bar and Baz and there's a…
Sernst
  • 469
  • 1
  • 4
  • 8
21
votes
3 answers

MyPy - "Incompatible types in assignment (expression has type None, variable has type ...)"

I've got the following function, which given a string of the form 'a-02/b-03/foobarbaz_c-04', will extract the digits after a, b and c. The issue is that, for my use case, the input strings may not contain c, such that there will be no digits to…
Daniel
  • 11,332
  • 9
  • 44
  • 72
20
votes
2 answers

Workaround for lack of intersection types with Python generics?

I'm hitting an issue that would be easily solved by intersection types (currently under discussion but not yet implemented) and was wondering what the cleanest workaround is. Current setup and problem My current setup roughly corresponds to the…
Uri Granta
  • 1,814
  • 14
  • 25
20
votes
3 answers

Why is mypy checking files that I've excluded?

I'm trying to set up mypy type checking for a project. I want to exclude a bunch of files/directories to start so that we can at least enforce that type checking passes on new code, then we can burn down the exclude list over time. Unfortunately…
Eddie Aftandilian
  • 219
  • 1
  • 2
  • 4
20
votes
1 answer

Why is mypy finding "no type hints or library stubs" for any of my imports?

I'm working on a code base that has a lot of type hints written be a previous developer. At some point I noticed that these hints were not getting type-checked, and that I needed to add a step to the build if I wanted them checked. I'm comfortable…
Alex Altair
  • 3,246
  • 3
  • 21
  • 37
20
votes
2 answers

"Source file found twice" error with mypy>=0.780 in python for vscode

In my python project, after upgrading mypy from 0.770 to 0.782 an error is received in files where there were previously no type errors: my_pkg_name\__init__.py: error: Source file found twice under different module names: 'top_pkg.my_pkg_name' and…
stav
  • 1,497
  • 2
  • 15
  • 40
20
votes
2 answers

mypy: base class has no attribute x, how to type hint in base class

I recently discovered mypy and I want my code to be type checked with it. I have a Connector base class: class Connector(): ... some methods, but no __init__ ... And I have several subclasses, they are all connectors, but of different…
JPFrancoia
  • 4,866
  • 10
  • 43
  • 73
20
votes
1 answer

What does the asterisk in the output of `reveal_type` mean?

reveal_type(1) # Revealed type is 'builtins.int' bla = [1,2,3] reveal_type(bla[0]) # Revealed type is 'builtins.int*' reveal_type(bla[0] * 2) # Revealed type is 'builtins.int' What is the difference between int and int*?
Nova
  • 2,623
  • 4
  • 26
  • 45
19
votes
6 answers

Validate Python TypedDict at runtime

I'm working in a Python 3.8+ Django/Rest-Framework environment enforcing types in new code but built on a lot of untyped legacy code and data. We are using TypedDicts extensively for ensuring that data we are generating passes to our TypeScript…
19
votes
2 answers

How do you annotate the type of an abstract class with mypy?

I'm writing a library where I need a method that takes a (potentially) abstract type, and returns an instance of a concrete subtype of that type: # script.py from typing import Type from abc import ABC, abstractmethod class AbstractClass(ABC): …
19
votes
1 answer

MyPy: what is the type of a requests object?

I'm trying to use Python 3's type hinting syntax, along with the MyPy static type checker. I'm now writing a function that takes a requests response object, and I'm wondering how to indicate the type. That is to say, in the following piece of code,…
Newb
  • 2,810
  • 3
  • 21
  • 35
19
votes
2 answers

Using Mypy local stubs

I am trying the typing hint introduced by Python 3.5 and got a problem by using local stubs as the typing hint with mypy. The experiment I do is to create kk.py containing def type_check(a): pass Also, I put kk.pyi containing def type_check(a:…
Musen
  • 1,244
  • 11
  • 23
18
votes
1 answer

mypy: Untyped decorator makes function "my_method" untyped

When I try using a decorator that I defined in another package, mypy fails with the error message Untyped decorator makes function "my_method" untyped. How should I define my decorator to make sure this passes? from mypackage import…
1step1leap
  • 535
  • 1
  • 6
  • 11