Mypy- 仅指定文件的报告生成

Mypy- Report Generation for only specified files

我正在查看文档 here:

有没有办法在举报时忽略某些文件?我看到一些我不想报告的文件,我想知道我是否可以进行运行时配置或其他操作来忽略它们。

是的。

你可以忽略整个包(cf this and the relevant Github issue)。

在此 GitHub issue which recommended other ways to achieve the same result (@no_type_check decorator for functions, checking the Python version, custom mypy call script, ...) and was closed as a duplicate of this other issue which happens to have been closed 13 days ago (2021-06-10) by Guido because "it didn't seem all that important", although the PEP 484 状态中讨论了仅忽略一个文件:

A # type: ignore comment on a line by itself at the top of a file, before any docstrings, imports, or other executable code, silences all errors in the file.

在另一个相关的GitHub issue, someone mentions there is a command option for exactly that since version 0.810 (2021-02-10) : --exclude (cf commit)

A regular expression that matches file names, directory names and paths which mypy should ignore while recursively discovering files to check. Use forward slashes on all platforms.

这是我的设置:

# file: ok.py
a: int = 4
# file: bad.py
a: int = "definitively not an integer"
  • 与 MyPy v0.800 :

    $ mypy . --txt-report report0800; cat report0800/index.txt
    bad.py:2: error: Incompatible types in assignment (expression has type "str", variable has type "int")
    Generated TXT report (via XSLT): /home/stack_overflow/PyCharmProjects/68071098/report0800/index.txt
    Found 1 error in 1 file (checked 2 source files)
    Mypy Type Check Coverage Summary
    ================================
    
    Script: index
    
    +-----+-------------------+-------+
    | Module | Imprecision       | Lines |
    +-----+-------------------+-------+
    | bad |   0.00% imprecise | 2 LOC |
    | ok  |   0.00% imprecise | 2 LOC |
    +-----+-------------------+-------+
    | Total |   0.00% imprecise | 4 LOC |
    +-----+-------------------+-------+
    
  • 与 MyPy v0.812 :

    $ mypy . --txt-report report0812 --exclude 'bad.py'; cat report0812/index.txt
    Generated TXT report (via XSLT): /home/stack_overflow/PyCharmProjects/68071098/report0812/index.txt
    Success: no issues found in 1 source file
    Mypy Type Check Coverage Summary
    ================================
    
    Script: index
    
    +----+-------------------+-------+
    | Module | Imprecision       | Lines |
    +----+-------------------+-------+
    | ok |   0.00% imprecise | 2 LOC |
    +----+-------------------+-------+
    | Total |   0.00% imprecise | 2 LOC |
    +----+-------------------+-------+
    

我在一个真实的项目上尝试过,正则表达式的行为似乎很挑剔(或者我对文档的理解不太好),但它仍然有效。