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
1
vote
1 answer

How to do 'generic type hinting' of functions (i.e 'function templates') in Python?

I'm pretty sure, this has been asked before, but I don't know, what to search for.. I want to type hint a "generic" function (like a function template in C++) e.g. (this example makes no sense at all - it's just for demonstration) def foo(fn:…
frans
  • 8,868
  • 11
  • 58
  • 132
1
vote
0 answers

Setup vscode MyPy for project with both Python2 and Python3

I have a vscode linux based project that is mainly Python2.7. The project started well before Python3 had momentum. In vscode settings I have to following to specify the code is for python2. "python.linting.mypyArgs": [ "--py2" ], Now, I have to…
BrendanSimon
  • 665
  • 1
  • 9
  • 23
1
vote
1 answer

Lambda Function Cannot Be Assigned to Method

I have a python script from a Google API but when I run it, I get error: Cannot assign to a method on line that states: request.get_method = lambda: method Code block (Full code: https://cloud.google.com/identity/docs/how-to/create-devices): def…
elZuko
  • 13
  • 4
1
vote
2 answers

Override argument type in subtype with a subtype

I have a type and a subtype that define some binary operations in such a way that I'd like the operation to return the most-specific type possible. For example, in the following code example I expect the following behavior. Logic + Logic =>…
ktb
  • 1,498
  • 10
  • 27
1
vote
1 answer

How can I annotate a method that returns self so that the stub file shows correct return types?

Pretext: This code is useless, and the use case might be useless also, but I am just trying to figure out how to make it work so that the stub files are showing the correct signatures. The question seems confusing, so I am trying my best to explain,…
securisec
  • 3,435
  • 6
  • 36
  • 63
1
vote
1 answer

mypy reports error in Dict outside class but not inside

When I use a dict where members have different types, mypy accepts my type definitions within the class but not outside. The access of member 'first' in ExampleClass.__str__() does not produce an error from mypy when I iterate over it. But when I…
ahnkle
  • 467
  • 6
  • 17
1
vote
1 answer

how to define this line of code in python in order to pass mypy checking

I have such define in my python code: options = defaultdict(lambda: defaultdict(lambda: defaultdict(str))) options['modem1']['ByCost'] = ... options['modem1']['ByCarrier'] = ... options['modem1']['BySimSignal'] =…
user3595231
  • 711
  • 12
  • 29
1
vote
1 answer

How to fix mypy inferring loop variable as "object"?

I made this simple function which I want to check with mypy and pylint. It just parses a string and converts it to the appropriate type. import re from typing import Any, Callable def parse_constant(constant: str) -> Any: for reg, get_val…
qouify
  • 3,698
  • 2
  • 15
  • 26
1
vote
1 answer

Stronger type-annotation for a mixed-type mapping

Suppose I have a mapping that maps string -> int, and int -> string in one dictionary. Is there a way to express that with type annotations in a strict way? Currently I can do Dict[Union[str, int], Union[str, int]], but that allows for str -> str,…
Redoubts
  • 818
  • 9
  • 25
1
vote
1 answer

How can I type hint a variable whose Union type gets narrowed in Python?

I have a few helper functions that get passed a type converter and a value. Based on checks that happen later, I decide which helper function to call. How can I correctly annotate the types to narrow the foo variable's type below so that it can pass…
Jim Hunziker
  • 14,111
  • 8
  • 58
  • 64
1
vote
0 answers

mypyc: Implementing a proxy object

Is there a way to implement a proxy object with mypyc? For example, consider the following code which works fine with Python but not mypyc: import typing class Thing: def __getattr__(self, name: str) -> typing.Any: return 5 t =…
Sohail
  • 3,020
  • 1
  • 25
  • 23
1
vote
1 answer

Is there a way for mypyc to compile a proxy object?

Consider the code below. This fails to run properly with mypyc because Proxy does not have a __dict__ attribute at runtime. So the questions are: Is there any documentation regarding the subset of the language that mypyc supports? I can't seem to…
Sohail
  • 3,020
  • 1
  • 25
  • 23
1
vote
0 answers

mypy skips analyzing own imported module throwing an error

I cannot seem to figure out why mypy is throwing an error on own module import. $ python -m mypy ./reader/api/response/response.py reader/api/response/response.py:5: error: Skipping analyzing 'reader.api.response.answer': found module but no type…
mkserge
  • 93
  • 4
1
vote
0 answers

How to handle models when programmatically creating endpoints in FastAPI

The following examples both work just fine, the only issue is that mypy is complaing about create_operation. Specifically I'm getting these errors: Variable "model" is not valid as a type model? has no attribute "dict" Especially the second error…
charlemagne
  • 284
  • 2
  • 10
1
vote
0 answers

How to Handle Optional cases in MyPy Type Checking of Python Code

I am trying my hand at Python 3 type hinting in order to make my code cleaner. Whenever I try to pass my code via mypy, there are some repeated errors mentioning something about Optional data types, and I cannot figure out why. Here is a sample…
Della
  • 1,264
  • 2
  • 15
  • 32