python - 在不同的机器上用 mypy 扫描一个包会产生不同的结果

标签 python type-hinting mypy

我正在尝试键入检查 the latest revision of my code并在 vim 和 mypy 中使用 ALE 插件获得不一致的结果在命令行上。

Update: After the comments from @aaron below, I checked out the code on a different machine, and it works as expected: I get more errors there, than on my main development machine. So ALE inside vim gives me the same results as the process on another machine. And I don't understand why.



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

设置和环境来重现我遇到的行为(不需要 ALE):
  • 克隆上面链接的存储库并 checkout 修订版 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") 一样运行它。

    关于python - 在不同的机器上用 mypy 扫描一个包会产生不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59265293/

    相关文章:

    python - 你如何看待这个 Python 错误?

    python-3.x - 当函数返回特定类型的对象或 None 时,指定什么类型提示?

    python - 为什么不带字段的 TypedDict 类型和带 NotRequired 字段的类型不兼容?

    python - 用特定类型的值表示 Enum 的正确方法

    python - Polars - 选择不存在的列,没有错误

    python - 为什么 `conda update --all`没有全部更新?

    Python - 将 pgqueryobject 转换为字符串

    data-structures - 如何实现递归deftype

    python - 在 Python 中注释 "file type"的正确方法

    python - 如何输入传递给 numpy.asarray 的变量以生成 2D float 组?