Pre-Commit集成MyPy无法禁用非错误消息的问题解决
解决pre-commit中MyPy的
annotation-unchecked提示无法禁用问题 问题背景
在给项目配置pre-commit钩子并使用MyPy工具时,执行后出现大量annotation-unchecked相关的非错误提示,希望禁用这些提示以简化命令行输出。已在pyproject.toml中添加disable_error_code = ["annotation-unchecked"]配置,但这些提示仍会出现。
当前配置
pyproject.toml中的MyPy配置
[tool.mypy] mypy_path = "src" disable_error_code = ["annotation-unchecked"] # 禁用MyPy抛出的非错误消息
pre-commit-config.yaml配置
# 类型检查 - repo: https://github.com/pre-commit/mirrors-mypy #rev: v0.910 rev: v1.9.0 hooks: - id: mypy files: '(src|tests)/.*\.py$' # 必须用单引号,因为正则里用了转义字符'\'(参考YAML 7.3流标量样式) additional_dependencies: [types-requests]
实际输出
执行pre-commit后,annotation-unchecked提示依然存在:
src/pkg/file1.py:14: error: Argument 2 to "from_bytes" of "int" has incompatible type "str"; expected "Literal['little', 'big']" [arg-type] src/pkg/file1.py:21: error: Argument 2 to "from_bytes" of "int" has incompatible type "str"; expected "Literal['little', 'big']" [arg-type] src/pkg/file1.py:23: error: Argument 2 to "from_bytes" of "int" has incompatible type "str"; expected "Literal['little', 'big']" [arg-type] src/pkg/file2.py:60: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked] src/pkg/file3.py:143: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked] src/pkg/file4.py:125: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked] src/pkg/file4.py:126: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked] src/pkg/file5.py:20: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked] src/pkg/file5.py:21: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked] src/pkg/file6.py:75: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked] src/pkg/file7.py:164: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked] src/pkg/file8.py:709: error: Value of type "Coroutine[Any, Any, None]" must be used [unused-coroutine] src/pkg/file9.py:709: note: Are you missing an await? src/pkg/file10.py:267: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked] Found 4 errors in 2 files (checked 72 source files)
问题原因
项目目录中存在被忽略的.mypy.ini文件,该文件优先级高于pyproject.toml,导致配置不生效,删除该文件即可解决问题。
MyPy配置文件优先级
当存在多个配置文件时,MyPy会按以下优先级加载:
./mypy.ini./.mypy.ini./pyproject.toml./setup.cfg$XDG_CONFIG_HOME/mypy/config~/.config/mypy/config~/.mypy.ini
替代解决方案
如果需要保留多个配置文件,可在pre-commit-config.yaml中通过参数指定使用的配置文件:
- repo: https://github.com/pre-commit/mirrors-mypy rev: v1.9.0 hooks: - id: mypy files: '(src|tests)/.*\.py$' # 必须用单引号,因为正则里用了转义字符'\'(参考YAML 7.3流标量样式) additional_dependencies: [types-requests] args: [--config-file=./pyproject.toml]
内容的提问来源于stack exchange,提问作者TheEponymousProgrammer
相关产品推荐
相关产品推荐

