Using Mypy Local Stubs

I am trying to enter a tooltip introduced by Python 3.5, and there was a problem using local stubs as input tooltip using mypy.

The experiment I'm doing is creat kk.py containing

def type_check(a): pass 

In addition, I put kk.pyi containing

 def type_check(a: int):... 

in the same directory. So I tried to throw the error "ncompatible types in assign" by passing the string type_check to kk.py. However, when I launched mypy kk.py and did not receive errors.

So I tried the other way mypy doc suggests, which should set the MYPYPATH environment variable to ~ / some / path / stub and put kk.pyi in the directory. However, I have the same error.

Can anybody help me?

Here is the mypy wiki link on how to use a local stub.

+5
source share
1 answer

I don’t know why someone voted for this question without answering it or commenting on why he / she does not like him, but here is the answer I found out:

The mypy stub file only works when importing a module. So, if you have

 def try_check(a): pass 

in kk.py and

 def try_check(a: int):... 

in kk.pyi in the same directory with kk.py or in the directory that sets MYPYPATH, mypy will type the python file check if you import kk. This is if you

 import .kk kk.try_check('str') 

in test.py and run mypy test.py, mypy will report a type conflict. However, it will not report a conflict if you have

 try_check('str') 

at kk.py.

You can enter validation functions in a program that contains a function definition. If you write an input hint explicitly in the function definition. For example, you can write

 def try_check(a: int): pass try_check('str') 

in kk.py and then mypy kk.py. Mypy will report a type conflict.

+6
source

All Articles