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
52
votes
4 answers

Python 3.10+: Optional[Type] or Type | None

Now that Python 3.10 has been released, is there any preference when indicating that a parameter or returned value might be optional, i.e., can be None. So what is preferred: Option 1: def f(parameter: Optional[int]) -> Optional[str]: Option 2: def…
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
52
votes
5 answers

How do I specify OrderedDict K,V types for Mypy type annotation?

I am using Python 3.5 together with Mypy to have some basic static checking for my script. Recently I refactored some methods to return OrderedDict, but ran into "'type' object is not subscriptable" error, when I tried to use return annotation with…
Xarn
  • 3,460
  • 1
  • 21
  • 43
50
votes
2 answers

Can I omit Optional if I set default to None?

For example: def foo(bar: int = None): pass When I check a type/annotation of bar pycharm tells me that it is Optional[int]. bar: int = None looks much cleaner rather then bar: Optional[int] = None, especially when you have 10+ parameters. So…
aiven
  • 3,775
  • 3
  • 27
  • 52
50
votes
2 answers

error: Skipping analyzing 'flask_mysqldb': found module but no type hints or library stubs

I am using Python 3.6 and flask. I used flask-mysqldb to connect to MySQL, but whenever I try to run mypy on my program I get this error: Skipping analyzing 'flask_mysqldb': found module but no type hints or library stubs. I tried running mypy…
rsev4292340
  • 599
  • 1
  • 4
  • 7
50
votes
4 answers

Why does mypy think library imports are missing?

When I run mypy it complains that modules cannot be found: sal@ahfang:~/workspace/ecs/cx-project-skeleton-repo/src/cx-example-function$ pipenv run python -m mypy . example_lambda.py:3: error: Cannot find module named 'aws_xray_sdk.core' But when…
Salim Fadhley
  • 6,975
  • 14
  • 46
  • 83
49
votes
3 answers

How can I get stub files for `matplotlib`, `numpy`, `scipy`, `pandas`, etc.?

I know that the stub files for built-in Python library for type checking and static analysis come with mypy or PyCharm installation. How can I get stub files for matplotlib, numpy, scipy, pandas, etc.?
Sunghee Yun
  • 722
  • 1
  • 6
  • 11
47
votes
1 answer

Python typing what does TypeVar(A, B, covariant=True) mean?

Today I took a deep dive into Liskov's Substitution Principle and covariance/contravariance. And I got stuck on the difference between: T = TypeVar("T", bound=Union[A, B]) T = TypeVar("T", A, B, covariant=True) My Understanding of #1 Difference…
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
46
votes
6 answers

What are the main differences of NamedTuple and TypedDict in Python / mypy

It seems to me that NamedTuple and TypedDict are fairly similar and the Python developers themselves recognized that. Concerning the PEP, I would rather add a common section about NamedTuple and TypedDict, they are quite similar and the latter…
Christoph
  • 26,519
  • 28
  • 95
  • 133
42
votes
1 answer

Using the class's own type inside class definition

The following code does not work as expected. Apparently, I cannot use the class's own type inside class definition: class Foo: def __init__(self, key :str) -> None: self.key = key def __eq__(self, other :Foo) -> bool: …
macjan
  • 531
  • 1
  • 4
  • 6
39
votes
2 answers

Is there a best practice to make a package PEP-561 compliant?

I'm writing a Python project which is published as a package to a pypi-like repository (using setuptools and twine). I use type hints in my code. The issue is, when importing the package from a different project and running mypy, I get the following…
Tsah Weiss
  • 563
  • 1
  • 6
  • 12
39
votes
1 answer

Set pyflake AND mypy ignore same line

I write a module for Salt. By the documentation it adds __salt__ object into builtins. So, pyflake warn me that __salt__ is undefined when I run prospector and mypy says the same, that __salt__ is undefined! I can ignore either for pyflake with #…
senior_pimiento
  • 702
  • 1
  • 5
  • 15
38
votes
1 answer

mypy Cannot find implementation or library stub for module

I have: foo/ ├── __init__.py ├── bar.py └── baz ├── __init__.py └── alice.py In bar.py, I import Alice, which is an empty class with nothing in it but the name attribute set to "Alice". from baz.alice import Alice a =…
Tom Huibregtse
  • 573
  • 1
  • 5
  • 13
37
votes
6 answers

Exclude folder from mypy checking

I would like to exclude a folder from the mypy checks. Looking at the documentation I tried the following configuration in my mypy.ini configuration file [mypy] python_version = 3.8 exclude '/venv/' with no luck. Yes I want to exclude my…
vianmixt
  • 703
  • 1
  • 6
  • 17
37
votes
1 answer

Mypy error - incompatible types in assignment

My function looks like this simplified code sample: def my_func() -> dict: result = {"success": False} if condition: result["success"] = True return result else: result["message"] = "error message" return…
Jan Rozycki
  • 1,603
  • 2
  • 15
  • 19
34
votes
2 answers

Define a jsonable type using mypy / PEP-526

Values that can be converted to a JSON string via json.dumps are: Scalars: Numbers and strings Containers: Mapping and Iterable Union[str, int, float, Mapping, Iterable] Do you have a better suggestion?
Terris
  • 887
  • 1
  • 10
  • 15