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



两者都应该给我相同的东西,但是我注意到ALE给我的结果比命令行更多。而且我想知道CLIt调用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/

    10-13 06:48