该项目的测试结构如下:
├── app
| └── db
| └── postgres.py
| └── postgres.py
└── postgres.py
如果您不设置配置,调用poetry run mypy . | grep postgres将提供以下内容:
postgres.py:2: error: Unsupported operand types for + ("str" and
"float") [operator]
app/postgres.py:2: error: Unsupported operand types for + ("str"
and "float") [operator]
app/db/postgres.py:11: error: "Constraint" has no attribute
"columns" [attr-defined]
但是,我的目标是排除一个目录db或至少一个文件app/db/postgres.py,因此我pyproject.toml添加:
[tool.mypy]
exclude = ["postgres.py", "db", "app/db", "db/postgres.py", "app/db/postgres.py"]
我跟注poetry run mypy . | grep postgres。结论:
app/db/postgres.py:11: error: "Constraint" has no attribute "columns" [attr-defined]
由于某种原因,在第二层嵌套中,mypy它不想尊重exclude。看起来问题甚至不在这个参数上。有人可以帮忙吗?
exclude参数是一个带有正则表达式的字符串:
您可以在.toml中使用列表,但需要注意的是,它必须是正则表达式的列表。
文档中的示例:
相当于单行表达式:
此正则表达式排除:
仅使用文件或目录名称迟早会适得其反。
感谢您的回答。问题原来是这样的:https://mypy.readthedocs.io/en/stable/running_mypy.html#following-imports
Mypy默认情况下,它会检查导入中遇到的所有模块,即使它们与指定的匹配exclude。这种行为可以改变,但不建议这样做。mypy因此,从项目根目录调用时,app/db可以通过以下方式排除:exclude在正则表达式中设置并调用mypy而不检查遇到的导入(不推荐)poetry run mypy . --follow-imports silent禁用覆盖中导入模块的检查
mypy导入将被递归检查。