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

Dealing with lack of non-null assertion operator in Python

I would like to allow Mypy' strict_optional flag. However, consider this: emails = [get_user(uuid).email for uuid in user_uuids] where get_user could return None in theory, but in this use case, I know it can't (and am fine with getting an…
Garrett
  • 4,007
  • 2
  • 41
  • 59
10
votes
1 answer

mypy declares IO[bytes] incompatible with BinaryIO

Consider the following code: from io import TextIOWrapper from typing import List from zipfile import ZipFile def read_zip_lines(zippath: str, filename: str) -> List[str]: with ZipFile(zippath) as zf: with zf.open(filename) as…
jwodder
  • 54,758
  • 12
  • 108
  • 124
10
votes
3 answers

Type annotation for overloads that exclude types (something vs everything else)

I'm trying to use @overload to communicate the different ways of calling a function, but what is easily communicated in the code with a simple else statement is not possible in the type annotations. Without the "else" MyPy (correctly) complains…
Iftah
  • 9,512
  • 2
  • 33
  • 45
10
votes
2 answers

How can I type-hint a nested object in Python?

I'm currently doing a integration with WSDL, and such decided to go with Python using the Zeep library. I'm trying to model the response with mypy, so that it works with VSCode's Intellisense, as well as some giving me hints when I'm doing careless…
Tan Jia Ming
  • 435
  • 1
  • 6
  • 22
10
votes
1 answer

What is the platform-independent mypy type annotation for asyncio event loop?

I want to write mypy-typed code that uses asyncio and works on multiple platforms. Specifically, I often have classes and methods that explicitly bind to an event loop. I want to provide a type annotation for the event loop. When I check the type of…
Nick Settje
  • 3,022
  • 1
  • 11
  • 18
10
votes
2 answers

How can I make a recursive Python type defined over several aliases?

I want this logical type structure: from typing import List, Dict, Union ObjectType = Dict[str, 'EntryType'] ListType = List['EntryType'] EntryType = Union[str, 'ListType', 'ObjectType'] mypy reports these errors: mdl/structure.py:7: error: Cannot…
edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267
10
votes
1 answer

How to annotate attribute that can be implemented as property?

I am trying to make mypy happy with my type annotations. Here is minimal example: class FooInterface: x: int class FooWithAttribute(FooInterface): x: int = 0 class FooWithProperty(FooInterface): @property def x(self) -> int: …
sanyassh
  • 8,100
  • 13
  • 36
  • 70
10
votes
4 answers

Extending stub file for a third-party library/module

I am using the yarl library's URL object. It has a quasi-private attribute, ._val, which is a urllib.parse.SplitResult object but has no type annotation in yarl/__init__.pyi. (Understandably so, if the developer does not want to formally make it…
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
10
votes
2 answers

How to make type-annotation-only type assertions?

I have two functions: def get_foo(params) -> Optional[str] def bar(foo: str) And a function which chains these functions together: def f(params): # other stuff up here foo = get_foo(params) return bar(foo) I know based on the other…
Daniel Kats
  • 5,141
  • 15
  • 65
  • 102
10
votes
2 answers

Python generics and subclasses

I'm trying to add type annotations to an existing package, and clearly I'm missing something important. I have an abstract superclass, and subclasses. The superclass should be generic, whereas the subclasses should be for a specific type. Here's a…
brunns
  • 2,689
  • 1
  • 13
  • 24
10
votes
1 answer

mypy error: Callable has no attribute "__get__"

I have something like the following: from typing import TypeVar, Callable, Generic, Type, Union, Optional T = TypeVar("T") V = TypeVar("V") class DescClass(Generic[T, V]): """A descriptor.""" def __init__(self, func: Callable[[T], V]) ->…
Rick
  • 43,029
  • 15
  • 76
  • 119
10
votes
2 answers

Mypy: annotating a variable with a class type

I am having some trouble assigning the variables in a Python 3.6 class to a particular type--a Pathlib path. Following an example from link, I tried to create a TypeVar, but mypy is still throwing errors. I want to make sure that the class variables…
krishnab
  • 9,270
  • 12
  • 66
  • 123
10
votes
1 answer

super().__init__(....) in mixins fails with `Too many arguments for "__init__" of "object" `

When I create a mixin class that extends the logic of __init__, the regular thing to do is: class ExtraValuemixin: def __init__(self, *args, **kwargs) -> None: super().__init__(*args, **kwargs) # some extra initialization …
kurtgn
  • 8,140
  • 13
  • 55
  • 91
10
votes
2 answers

mypy trouble with inheritance of objects in lists

Python 3.6.5 and mypy 0.600 I wrote the code: from typing import List class Animal(): pass class Dog(Animal): def __init__(self) -> None: super() def bark(self) -> None: pass class Cat(Animal): def…
user3517175
  • 363
  • 2
  • 14
10
votes
2 answers

mypy: Signature of "__getitem__" incompatible with supertype "Sequence"

I have a class that inherits from MutableSequence like this: class QqTag(MutableSequence): def __init__(self): self._children = [] def __getitem__(self, idx: int) -> 'QqTag': return self._children[idx] mypy complains that…
Ilya V. Schurov
  • 7,687
  • 2
  • 40
  • 78