从 VS Code 调用时,Mypy 不尊重 mypy.ini 中的设置以排除文件夹检查

via*_*ixt 40 python virtualenv visual-studio-code vscode-extensions mypy

我想从 mypy 检查中排除一个文件夹。查看文档我在 mypy.ini 配置文件中尝试了以下配置

[mypy]  
python_version = 3.8  
exclude '/venv/'
Run Code Online (Sandbox Code Playgroud)

没有运气。

我想从 mypy 检查中排除我的虚拟环境。我只有一个来检查我写的代码。

这是 mypy 的错误吗?

我正在使用 mypy 0.901 和 mypy-extensions 0.4.3。我还使用 mypy vs-code 扩展 0.2.0。

The*_*ool 33

问题是 VS Code 正在逐个mypy文件调用。并且mypy在单个文件上调用时不使用排除选项。解决方法是使用python.linting.ignorePatterns在 settings.json 中使用。

\n
 "python.linting.ignorePatterns": [ "venv/**/*.py" ]\n
Run Code Online (Sandbox Code Playgroud)\n

有关排除行为的更多信息

\n
\n

请注意,此标志仅影响递归发现,即当 mypy 发现要检查的目录树或包的子模块中的文件时。如果您显式传递文件或模块,仍然会检查它。例如,mypy --exclude \'/setup.py$\' but_still_check/setup.py。

\n
\n

为了完整性,我仍然会在配置文件中排除它\xe2\x80\x99,以防你运行mypy手动运行。

\n
[mypy]\nexclude = venv\n
Run Code Online (Sandbox Code Playgroud)\n


Ale*_*aev 21

如果我想忽略我的venv文件夹,那么我将以下行写入该文件mypy.ini

[mypy]
exclude = venv
Run Code Online (Sandbox Code Playgroud)


小智 17

忽略多个目录。

pyproject.toml

[tool.mypy]
exclude = ['venv', '.venv']
Run Code Online (Sandbox Code Playgroud)

mypy --config-file pyproject.toml ./

  • 它会自动读取 pyproject.toml 并且您可以跳过手动指定它。 (2认同)

小智 9

根据官方文档,如果使用mypy.inior setup.cfg,配置文件中字段的内容exclude似乎必须是正则表达式,例如:

[mypy]
exclude = (?x)(
    ^one\.py$    # files named "one.py"
    | two\.pyi$  # or files ending with "two.pyi"
    | ^three\.   # or files starting with "three."
  )
Run Code Online (Sandbox Code Playgroud)

另一方面,如果用作pyproject.toml配置文件,则该exclude字段的内容可以有两种形式:(1)单个正则表达式(如上);(2)字符串数组如下:

[tool.mypy]
exclude = [
    "^one\\.py$",  # TOML's double-quoted strings require escaping backslashes
    'two\.pyi$',  # but TOML's single-quoted strings do not
    '^three\.',
]
Run Code Online (Sandbox Code Playgroud)

因此,MyPy 似乎不允许使用文件/目录名数组(不是正则表达式),这与flake8(仅使用文件/目录名数组就可以)不同。

  • 似乎不起作用。 (2认同)

mdr*_*den 7

就我而言,问题是我的测试文件夹中的 vscode linting。上面列出的解决方案 ( "python.linting.ignorePatterns": [ "test" ]) 可能会起作用,但我不想完全禁用那里的 linting。对我有用的是将其添加到我的 mypy.ini 中:

[mypy]
# global config here

[mypy-test.*] # the folder I wanted ignored was named "test" and is in the root of my workspace.
ignore_errors = True
Run Code Online (Sandbox Code Playgroud)


mau*_*777 5

经过一番尝试和错误后,这对我有用:

[mypy]
python_version = 3.9
disallow_untyped_defs = True
ignore_missing_imports = True
exclude = ['env/']
Run Code Online (Sandbox Code Playgroud)