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

Using Typing and Mypy with Descriptors

I have looked at a few SO posts and github issues related to using Typing with descriptors but I have not been able to get my issue resolved. I have wrapper classes and I want to define properties as descriptos that can get and "cast" properties of…
gtalarico
  • 4,409
  • 1
  • 20
  • 42
9
votes
3 answers

Property annotation that checks assignment to a guard value initially set to None

I have a property that only gets computed once per instance and that uses None as a guard value. This is a pretty common pattern with properties. What's the best way to annotate it? edit/clarification: This question is about how to use mypy to…
JL Peyret
  • 10,917
  • 2
  • 54
  • 73
9
votes
1 answer

Infer generic lambda parameters from another generic lambda's parameters

Consider the following code: from typing import Callable, TypeVar T = TypeVar('T') def middle_man( producer: Callable[[], T], consumer: Callable[[T], None] ) -> None: consumer(producer()) middle_man( lambda: "HELLO", lambda s:…
P Daddy
  • 28,912
  • 9
  • 68
  • 92
9
votes
1 answer

Python Typing with Exception Handling

The following code is stored in a file called sample.py. import re from typing import Optional, Tuple   def func(path: str) -> Optional[Tuple[str, str]]:     regex = re.compile(r"/'([^/']+?)'/'([^/']+?)'")     try:         return…
scruffaluff
  • 345
  • 4
  • 20
9
votes
2 answers

Flask SQL Alchemy vs MyPy - error with Model type

I came across the following problem with the combination of flask_sqlalchemy and mypy. When I define a new ORM object like: class Foo(db.Model): pass where db is a database created using SQL Alchemy applied to flask app, mypy type check…
Marcin Możejko
  • 39,542
  • 10
  • 109
  • 120
9
votes
1 answer

Python type hints for generic *args (specifically zip or zipWith)

I am writing a function called zip_with with the following signature: _A = TypeVar("_A") _B = TypeVar("_B") _C = TypeVar("_C") def zip_with(zipper: Callable[[_A, _B], _C], a_vals: Iterable[_A], b_vals: Iterable[_B]) -> Generator[_C, None, None]:…
hoogamaphone
  • 1,092
  • 10
  • 20
9
votes
1 answer

Mypy throws and error 'Missing return statement', but i can't see where I'm missing it

I'm trying to implement a retry function in http_requests, but I'm running into problems with a 'needed' return statement, although I cant figure out where this should be def _http_request(method: _HttpMethod, url: str, **kwargs) ->…
Xoferif
  • 93
  • 1
  • 1
  • 6
9
votes
1 answer

Does VSCode support Python .pyi files for IntelliSense?

In VS Code, I tried importing a module called foo.py that has a type hinting stub file foo.pyi. I want to get code autocompletion based on the type hints in the .pyi file, as PyCharm does. However, the .pyi file does not seem to have any effect.…
RexE
  • 17,085
  • 16
  • 58
  • 81
9
votes
3 answers

mypy type checking on Callable thinks that member variable is a method

When I run mypy over the following code I see several errors: from typing import Callable, Type def class_creator(outside_reference: Callable[[str], None]) -> Type[object]: class SomeClass(): reference: Callable[[str], None] …
Dale Myers
  • 2,703
  • 3
  • 26
  • 48
9
votes
1 answer

Does PyCharm use Mypy?

Does PyCharm use Mypy or did JetBrains implement PEP 484 separately?
Nova
  • 2,623
  • 4
  • 26
  • 45
9
votes
1 answer

Typing __exit__ in 3.5 fails on runtime, but typechecks

What is the proper type signature for __exit__? I have the following: from types import TracebackType from typing import Optional, Type class Foo: def __enter__(self) -> 'Foo': return self def __exit__(self, exc_type:…
Bailey Parker
  • 15,599
  • 5
  • 53
  • 91
9
votes
1 answer

How to enable PyCharm Type Checker feature for cython(.pyx) file?

It seems python type checker in PyCharm automatically works for .py files.. but not for .pyx files. Is there any way to enable type checker for .pyx files in PyCharm? Also, is there any way to use mypy with cython files (.pyx files)?
sdf3w
  • 261
  • 2
  • 6
9
votes
1 answer

Comparable types with mypy

I'm trying to create a generic class to express that a value has lower and upper bounds, and to enforce those bounds. from typing import Any, Optional, TypeVar T = TypeVar("T") class Bounded(object): def __init__(self, minValue: T, maxValue:…
Hannes Karppila
  • 969
  • 2
  • 13
  • 31
9
votes
1 answer

Why does mypy say I have too many arguments

I've got a class which has a method that creates instances of the class. class Bar: @classmethod def create(cls, foo: int): return cls(foo) def __init__(self, foo: int) -> None: pass When I run mypy against it, it says…
McKay
  • 12,334
  • 7
  • 53
  • 76
9
votes
1 answer

How can I ensure that arguments have same type without listing the types explicitly?

Let us assume that we need a function that accepts two arguments of any type as long as both arguments have the same type. How would you check it statically with mypy? If we only need the function to accept some finite amount of already known types,…
zabolekar
  • 1,624
  • 1
  • 12
  • 17