4

I try writing the type hint explicitly in the definition of the function

# test.py
def annotated(x: int, y: str) -> bool:
    return x < y

ran python3.5 -m mypy test.py and got following message

test.py:2: error: Unsupported operand types for > ("str" and "int")

It's correct. But when I try using stub file

# test.py
def annotated(x, y):
    return x < y

# test.pyi
def annotated(x: int, y: str) -> bool: ...

I got nothing. I saw this answer Using Mypy local stubs. So I try to import.

from test import annotated

annotated(2, 3)

It's ok. I got

error: Argument 2 to "annotated" has incompatible type "int"; expected "str"

But call annotated(2, 's') still got nothing.
So What should I do to check illegal operation with a stub file? Thanks

Hanaasagi
  • 219
  • 3
  • 7
  • You might want to try asking [mypy gitter channel](https://gitter.im/python/mypy) this question -- I suspect this behavior might be by design, but don't remember for certain, and it'll likely be quicker to just ask the core devs directly. – Michael0x2a May 31 '17 at 03:48
  • As I understand, stub files only provide a "type API" of a module for other modules that use it. In other words bodies of function implementations are not checked against stub files. If you want to change this, then you can open an issue on mypy tracker: https://github.com/python/mypy/issues – ivanl Jun 04 '17 at 09:50
  • Found the cause and solution here: https://github.com/python/mypy/issues/5520#issuecomment-426371485 – Pragy Agarwal Oct 11 '18 at 20:01

0 Answers0