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
10
votes
2 answers

Bytes-like object or Buffer type annotaion

How do I annotate a bytes-like object or a Buffer? There is no interface for the buffer protocol but I wish to accept all buffers in a function of mine. I don't mind if it's only mypy-specific.
Bharel
  • 23,672
  • 5
  • 40
  • 80
9
votes
1 answer

Suppress Mypy notes

I'm getting a ton of "notes" from Mypy output of the form: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked] Even explicitly setting check_untyped_defs = false in my…
Garrett
  • 4,007
  • 2
  • 41
  • 59
9
votes
1 answer

Why use `from module import A as A` instead of just `from module import A`

When reading source code of fastapi, this line make me fuzzy: from starlette.testclient import TestClient as TestClient Why not just: from starlette.testclient import TestClient?
Waket Zheng
  • 5,065
  • 2
  • 17
  • 30
9
votes
2 answers

Python: type-hinting a classmethod that returns an instance of the class, for a class that is inherited

Consider the following: from __future__ import annotations class A: def __init__(self): print("A") self.hello = "hello" # how do I type this so that the return type is A for A.bobo() # and B for B.bobo()? …
Tristan Duquesne
  • 512
  • 1
  • 6
  • 16
9
votes
1 answer

Union of generic types that is also generic

Say I have two types (one of them generic) like this from typing import Generic, TypeVar T = TypeVar('T') class A(Generic[T]): pass class B: pass And a union of A and B like this C = A|B Or, in pre-Python-3.10/PEP 604-syntax: C = Union[A,B] How…
Sören
  • 1,803
  • 2
  • 16
  • 23
9
votes
1 answer

Catch-all overload for in Python type annotations

The below code fails mypy with error: Overloaded function signatures 1 and 2 overlap with incompatible return types. @overload def test_overload(x: str) -> str: ... @overload def test_overload(x: object) -> int: ... def test_overload(x) ->…
Sean Mackesey
  • 10,701
  • 11
  • 40
  • 66
9
votes
3 answers

Type-hinting parameters with a sentinel value as the default

I currently use this strategy when I cannot assign default arguments in a function's signature and/or None already has meaning. from typing import Optional DEFAULT = object() # `None` already has meaning. def spam(ham: Optional[list[str]] =…
9
votes
1 answer

Type a function with a callable

I have a lot of functions that have the same signature, let's say (int, int) -> int. Is there a way to type these functions with a Callable (or something else) to avoid specifying the types of the parameters and the return type for each of these…
qouify
  • 3,698
  • 2
  • 15
  • 26
9
votes
1 answer

Python type hints with Protocol and TypeVar to specify arbitrary dataclasses

I'm writing a class that can store arbitrary dataclasses in memory. There I'm trying to specify that instances to be stored must be a dataclass and have a id field. Also it should be possible to get instances by specifying the classes and the id of…
lmr2391
  • 571
  • 1
  • 7
  • 14
9
votes
1 answer

Does mypy only type check a function if it declares a return type?

The following file: from typing import List class A: def __init__(self, myStr): self.chars: List[int] = list(myStr) def toString(self): return "".join(self.chars) typechecks (note chars should be List[str] not List[int]): ➜ Python…
Edward Garemo
  • 434
  • 3
  • 13
9
votes
1 answer

PyCharm raising Unresolved reference + expression expected for mypy ignore based on error code

I am trying to silence mypy errors based on error codes. This is done using something like this: from foolib import foo # type: ignore[attr-defined] I believe PyCharm is interpreting my type: ignore[code] comments as a type comment, and reporting…
Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
9
votes
2 answers

How do you get mypy to recognize a newer version of python?

I just updated my project to Python 3.7 and I'm seeing this error when I run mypy on the project: error: "Type[datetime]" has no attribute "fromisoformat" datetime does have a function fromisoformat in Python 3.7, but not in previous versions of…
Charlie A
  • 416
  • 3
  • 11
9
votes
2 answers

Python typehint for os.getenv causes downstream incompatible type errors

When using os.getenv to retrieve environment variables, the default behavior returns a type of Optional[str]. This is problematic as any downstream methods/functions that utilize these variables will likely be defined to accept a str type…
Grr
  • 15,553
  • 7
  • 65
  • 85
9
votes
1 answer

How to find code that is missing type annotations?

I have a project that is fully annotated. Or at least I hope so, because it is entirely possible that there is a function or two somewhere in there that is missing type annotations. How can I find such functions (or any other blocks of code)?
frnhr
  • 12,354
  • 9
  • 63
  • 90
9
votes
1 answer

How can I use @property setters and make mypy happy?

I have the following example.py file: class Location(object): def __init__(self, latitude, longitude): self.latitude = latitude self.longitude = longitude @property def latitude(self): return self._latitude …
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958