使用 Mypy 本地存根 [英] Using Mypy local stubs

查看:65
本文介绍了使用 Mypy 本地存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 Python 3.5 引入的输入提示,但在使用本地存根作为 mypy 的输入提示时遇到了问题.

I am trying the typing hint introduced by Python 3.5 and got a problem by using local stubs as the typing hint with mypy.

我做的实验是创建包含

def type_check(a):
    pass

另外,我把kk.pyi包含

Also, I put kk.pyi containing

def type_check(a: int):...

在同一目录中.通过这种方式,我试图通过向kk.py中的type_check传递一个字符串来触发ncompatible types in assignment"的错误.但是,当我运行 mypy kk.py 并没有出现错误时.

in the same directory. In this way, I tried to trigger the error of "ncompatible types in assignment" by passing a string to type_check in kk.py. However, when I ran mypy kk.py and get no error.

因此我尝试了 mypy doc 建议的另一种方法,即将环境变量 MYPYPATH 设置为 ~/some/path/stub 并将 kk.pyi 放在目录中.但是,我遇到了同样的错误.

Thus I tried another way that mypy doc suggests, which is to set environment variable MYPYPATH to ~/some/path/stub and put kk.pyi in the directory. I got the same error, however.

有人可以帮我解决这个问题吗?

Anyone can help me on this?

这是关于如何的mypy wiki使用本地存根.

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

推荐答案

我不知道为什么有人在没有回答或评论他/她不喜欢它的情况下投票否决了这个问题,但这是我想出的答案:

I do not know why someone have voted down this question without answering it or commenting about why he/she disliked it, but here is the answer I figured out:

mypy 的存根文件仅在导入模块时有效.因此,如果你有

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

def try_check(a):
    pass

在 kk.py 和

def try_check(a: int):...

在kk.py与kk.py同目录下或MYPYPATH指定的目录下,如果导入kk,mypy会打字检查python文件.是的,如果你有

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

import .kk
kk.try_check('str')

在test.py中运行mypy test.py,mypy会报类型冲突.但是,如果您有

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

try_check('str')

在 kk.py 中.

您可以在包含函数定义的程序中键入检查函数,如果您在函数定义中明确写入键入提示.例如,你可以写

You can type check functions in the program that contains the function definition If you write the typing hint explicitly in the definition of the function. For instance, you can write

def try_check(a: int):
    pass

try_check('str')

在 kk.py 中,然后是 mypy kk.py.Mypy 会报类型冲突.

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

这篇关于使用 Mypy 本地存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆