简体   繁体   English

mypy“无效类型”错误

[英]mypy “invalid type” error

I'm trying to implement type annotations in a current project, and am receiving errors from mypy that I don't understand. 我正在尝试在当前项目中实现类型注释,并且从mypy收到我不理解的错误。

I'm using Python 2.7.11, and newly installed mypy in my base virtualenv. 我正在使用Python 2.7.11,并在我的基础virtualenv中新安装了mypy。 The following program runs fine: 以下程序运行正常:

from __future__ import print_function
from types import StringTypes
from typing import List, Union, Callable

def f(value):     # type: (StringTypes) -> StringTypes
    return value

if __name__ == '__main__':
    print("{}".format(f('some text')))
    print("{}".format(f(u'some unicode text')))

But running mypy --py2 -s mypy_issue.py returns the following: 但是运行mypy --py2 -s mypy_issue.py返回以下内容:

mypy_issue.py: note: In function "f":
mypy_issue.py:8: error: Invalid type "types.StringTypes"

The above types appear to be in Typeshed ... the mypy documentation says "Mypy incorporates the typeshed project, which contains library stubs for the Python builtins and the standard library. "... Not sure what "incorporates" means - do I need to do something to "activate", or provide a path to, Typeshed? 上面的类型似乎是在Typeshed中 ... mypy 文档说“Mypy包含了类型化项目,其中包含Python内置函数和标准库的库存根。”......不确定“合并”意味着什么 - 我需要什么做一些事情来“激活”,或提供一个路径,类型? Do I need to download and install(?) Typeshed locally? 我是否需要在本地下载并安装(?)Typeshed?

The problem is that types.StringTypes is defined to be a sequence of types -- the formal type signature on Typeshed is: 问题是types.StringTypes被定义为一系列类型 - Typeshed上的正式类型签名是:

StringTypes = (StringType, UnicodeType)

This corresponds to the official documentation , which states that the StringTypes constant is "a sequence containing StringType and UnicodeType "... 这对应于官方文档 ,其中声明StringTypes常量是“包含StringTypeUnicodeType的序列”......

So then, this explains the error you're getting -- StringTypes isn't an actual class (it's probably a tuple) and so mypy doesn't recognize it as a valid type. 那么,这就解释了你得到的错误 - StringTypes不是一个实际的类(它可能是一个元组),因此mypy不会将它识别为有效类型。

There are several possible fixes for this. 对此有几种可能的解决方法。

The first way would probably be to use typing.AnyStr which is defined as AnyStr = TypeVar('AnyStr', bytes, unicode) . 第一种方法可能是使用typing.AnyStr ,它被定义为AnyStr = TypeVar('AnyStr', bytes, unicode) Although AnyStr is included within the typing module, it is, unfortunately, somewhat poorly documented as of now -- you can find more detailed information about what it does within the mypy docs . 虽然AnyStr包含在typing模块中,但遗憾的是,它现在的文档记录很少 - 您可以在mypy文档中找到更详细的信息。

A slightly less cleaner way of expression this would be to do: 这将是一种稍微不那么清晰的表达方式:

from types import StringType, UnicodeType
from typing import Union

MyStringTypes = Union[StringType, UnicodeType]

def f(value):     
    # type: (MyStringTypes) -> MyStringTypes
    return value

This also works, but is less desirable because the return type is no longer obligated to be the same thing as the input type which is usually not what you want when working with different kinds of strings. 这也有效,但是不太理想,因为返回类型不再与输入类型相同,在使用不同类型的字符串时通常不是你想要的。

And as for typeshed -- it's bundled by default when you install mypy. 至于打字 - 在安装mypy时默认捆绑它。 In an ideal world, you shouldn't need to worry about typeshed at all, but since mypy is in beta and so typeshed is being frequently updated to account for missing modules or incorrect type annotations, it might be worth installing mypy directly from the Github repo and installing typeshed locally if you find yourself frequently running into bugs with typeshed. 在一个理想的世界中,您根本不需要担心类型化,但由于mypy处于测试阶段,因此经常更新类型以解决缺少的模块或不正确的类型注释,因此可能值得直接从Github安装mypy 回购 ,如果你发现自己经常跑与typeshed错误本地安装已typeshed。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM