使用 mypy 扫描包在不同的计算机上会产生不同的结果



我正在尝试键入检查代码的最新版本,并在命令行上使用 vim 和mypy中的 ALE 插件获得不一致的结果。

更新:在下面@aaron的评论之后,我在另一台机器上检查了代码,它按预期工作:我在那里得到的错误比在我的主开发机器上更多。因此,vim 中的 ALE 给我的结果与另一台机器上的流程相同。我不明白为什么。

两者都应该给我相同的结果,但我注意到 ALE 给我的结果比命令行多。我想知道我在 CLI 调用mypy时做错了什么.

要设置和环境以重现我正在经历的行为(不需要 ALE(,请执行以下操作:

  • 克隆上面链接的存储库并签出修订4fb26c4e5b
  • 运行python3 -m venv env
  • 运行./env/bin/pip install -e .
  • 运行./env/bin/pip install mypy

现在,使用此环境,对目录进行类型检查puresnmp会产生以下结果:

$ ./env/bin/mypy puresnmp
puresnmp/aio/api/raw.py:505: error: Type signature has too few arguments
puresnmp/api/pythonic.py:239: error: Type signature has too few arguments
puresnmp/api/raw.py:490: error: Type signature has too few arguments
puresnmp/test/asyncmock.py:18: error: 'yield' in async function
Found 4 errors in 4 files (checked 38 source files)

使用./env/bin/mypy puresnmp/**/*.py./env/bin/mypy -p puresnmp生成与上述相同的报告。

但是,在显式指定文件时,我收到更多错误(恰好对应于ALE错误(:

$ ./env/bin/mypy puresnmp/api/pythonic.py
puresnmp/x690/types.pyi:58: error: Return type "Union[Null, UnknownType]" of "from_bytes" incompatible with return type "Type[bytes]" in supertype "Type"
puresnmp/transport.py:83: error: Argument 1 to "Timeout" has incompatible type "str"; expected "int"
puresnmp/pdu.py:178: error: Argument 2 to "Sequence" has incompatible type "Union[str, bytes, int, datetime, timedelta, None]"; expected "Type[Any]"
puresnmp/pdu.py:217: error: On Python 3 '%s' % b'abc' produces "b'abc'"; use %r if this is a desired behavior
puresnmp/pdu.py:253: error: Argument 2 to "VarBind" has incompatible type "Null"; expected "Union[str, bytes, int, datetime, timedelta, None]"
puresnmp/pdu.py:316: error: Argument 2 to "VarBind" has incompatible type "Null"; expected "Union[str, bytes, int, datetime, timedelta, None]"
puresnmp/pdu.py:320: error: Argument 2 to "Sequence" has incompatible type "Union[str, bytes, int, datetime, timedelta, None]"; expected "Type[Any]"
puresnmp/pdu.py:365: error: On Python 3 '%s' % b'abc' produces "b'abc'"; use %r if this is a desired behavior
puresnmp/api/raw.py:104: error: Incompatible return value type (got "List[Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]]", expected "List[Type[Union[str, bytes, int, datetime, timedelta, None]]]")
puresnmp/api/raw.py:160: error: Argument 1 to "VarBind" has incompatible type "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]"; expected "Union[ObjectIdentifier, str]"
puresnmp/api/raw.py:160: error: Argument 2 to "VarBind" has incompatible type "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]"; expected "Union[str, bytes, int, datetime, timedelta, None]"
puresnmp/api/raw.py:164: error: Unsupported left operand type for < ("ObjectIdentifier")
puresnmp/api/raw.py:317: error: Argument 2 to "VarBind" has incompatible type "Type[Union[str, bytes, int, datetime, timedelta, None]]"; expected "Union[str, bytes, int, datetime, timedelta, None]"
puresnmp/api/raw.py:336: error: Incompatible return value type (got "Dict[str, Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]]", expected "Dict[str, Type[Union[str, bytes, int, datetime, timedelta, None]]]")
puresnmp/api/raw.py:460: error: Incompatible types in assignment (expression has type "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]", target has type "Type[Union[str, bytes, int, datetime, timedelta, None]]")
puresnmp/api/raw.py:490: error: Type signature has too few arguments
puresnmp/api/raw.py:566: error: Argument 1 to "len" has incompatible type "ObjectIdentifier"; expected "Sized"
puresnmp/api/raw.py:570: error: Argument 1 to "tablify" has incompatible type "List[VarBind]"; expected "Iterable[Tuple[Any, Any]]"
puresnmp/api/raw.py:587: error: Argument 1 to "len" has incompatible type "ObjectIdentifier"; expected "Sized"
puresnmp/api/pythonic.py:104: error: Item "str" of "Union[ObjectIdentifier, str, bytes, int, datetime, timedelta, None, Any]" has no attribute "pythonize"
puresnmp/api/pythonic.py:104: error: Item "bytes" of "Union[ObjectIdentifier, str, bytes, int, datetime, timedelta, None, Any]" has no attribute "pythonize"
puresnmp/api/pythonic.py:104: error: Item "int" of "Union[ObjectIdentifier, str, bytes, int, datetime, timedelta, None, Any]" has no attribute "pythonize"
puresnmp/api/pythonic.py:104: error: Item "datetime" of "Union[ObjectIdentifier, str, bytes, int, datetime, timedelta, None, Any]" has no attribute "pythonize"
puresnmp/api/pythonic.py:104: error: Item "timedelta" of "Union[ObjectIdentifier, str, bytes, int, datetime, timedelta, None, Any]" has no attribute "pythonize"
puresnmp/api/pythonic.py:104: error: Item "None" of "Union[ObjectIdentifier, str, bytes, int, datetime, timedelta, None, Any]" has no attribute "pythonize"
puresnmp/api/pythonic.py:154: error: Argument 1 to "VarBind" has incompatible type "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]"; expected "Union[ObjectIdentifier, str]"
puresnmp/api/pythonic.py:154: error: Item "str" of "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]" has no attribute "pythonize"
puresnmp/api/pythonic.py:154: error: Item "bytes" of "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]" has no attribute "pythonize"
puresnmp/api/pythonic.py:154: error: Item "int" of "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]" has no attribute "pythonize"
puresnmp/api/pythonic.py:154: error: Item "datetime" of "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]" has no attribute "pythonize"
puresnmp/api/pythonic.py:154: error: Item "timedelta" of "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]" has no attribute "pythonize"
puresnmp/api/pythonic.py:154: error: Item "object" of "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]" has no attribute "pythonize"
puresnmp/api/pythonic.py:169: error: Argument 1 to "VarBind" has incompatible type "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]"; expected "Union[ObjectIdentifier, str]"
puresnmp/api/pythonic.py:169: error: Item "str" of "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]" has no attribute "pythonize"
puresnmp/api/pythonic.py:169: error: Item "bytes" of "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]" has no attribute "pythonize"
puresnmp/api/pythonic.py:169: error: Item "int" of "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]" has no attribute "pythonize"
puresnmp/api/pythonic.py:169: error: Item "datetime" of "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]" has no attribute "pythonize"
puresnmp/api/pythonic.py:169: error: Item "timedelta" of "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]" has no attribute "pythonize"
puresnmp/api/pythonic.py:169: error: Item "object" of "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]" has no attribute "pythonize"
puresnmp/api/pythonic.py:185: error: Argument 1 to "VarBind" has incompatible type "Union[ObjectIdentifier, Union[str, bytes, int, datetime, timedelta, None]]"; expected "Union[ObjectIdentifier, str]"
puresnmp/api/pythonic.py:239: error: Type signature has too few arguments
puresnmp/api/pythonic.py:258: error: The return type of a generator function should be "Generator" or one of its supertypes
puresnmp/api/pythonic.py:272: error: Argument 1 to "len" has incompatible type "ObjectIdentifier"; expected "Sized"
puresnmp/api/pythonic.py:282: error: The return type of a generator function should be "Generator" or one of its supertypes
puresnmp/api/pythonic.py:296: error: Argument 1 to "len" has incompatible type "ObjectIdentifier"; expected "Sized"
Found 45 errors in 5 files (checked 1 source file)

如何解释这种差异?

更重要的是:如何在不手动指定每个文件的情况下获取所有错误?

我相信默认情况下,当您传递文件夹时,对于mypy来说,这意味着scan this package.所以它遵循进口。(本案./env/bin/mypy puresnmp(

当您将 python 文件列表作为 args 传递时 - 它会扫描文件列表。

在文档网站上查看更多详细信息 https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-follow-imports

https://mypy.readthedocs.io/en/stable/running_mypy.html#following-imports

对所有文件运行mypy

我认为您应该能够在文件夹中的所有python文件上运行该实用程序,您可以像./env/bin/mypy $(find puresnmp -name "*.py")一样运行它

问题是通过penv安装了多个 Python 版本。

在我为 ALE/vim 创建虚拟环境时,我将我的 Python 版本设置为不同的版本,然后在我手动运行mypy时。

这导致了不同的结果,因为键入在这些版本之间进行了更新。

清理所有内容并确保 Python 版本相同解决了这个问题。

最新更新