Using a Mypy stub file does not work the same as a type hint in a function definition

I am trying to write a type hint explicitly in a function definition

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

ran python3.5 -m mypy test.pyand received the following message

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

It is right. But when I try to use a stub file

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

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

I have nothing. I saw this answer Using Mypy Local Stubs . So I try import.

from test import annotated

annotated(2, 3)

This is normal. I got

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

But the call annotated(2, 's')still got nothing.
So what should I do to check for illegal stub operation? Thanks

+4
source share

Source: https://habr.com/ru/post/1678085/


All Articles