我有一个 pydantic 基本模型,看起来像:
from pathlib import Path
from pydantic import BaseModel
class Model(BaseModel):
log_file: Path
我的 ruff 预提交钩子正在将其重新排序为:
from typing import TYPE_CHECKING
from pydantic import BaseModel
if TYPE_CHECKING:
from pathlib import Path
class Model(BaseModel):
log_file: Path
这导致了错误:
pydantic.errors.ConfigError: field "log_file" not yet prepared so type is still a ForwardRef, you might need to call Model.update_forward_refs()
我不想这样做。我怎样才能阻止 ruff 像这样重新订购进口?我的
.pre-commit-config.yaml
文件如下所示:
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.291"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- repo: https://github.com/psf/black
rev: 23.9.1
hooks:
- id: black
language_version: python3
和我的
pyproject.toml
文件:
[tool.black]
line-length = 120
include = '\.pyi?$'
exclude = '''
/(
\.eggs
| \.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
# The following are specific to Black, you probably don't want those.
| blib2to3
| tests/data
| profiling
)/
'''
[tool.ruff]
line-length = 120
ignore = ["F405", "B008"]
select = ["E", "F", "B", "C4", "DTZ", "PTH", "TCH", "I001"]
# unfixable = ["C4", "B"]
exclude = ["docs/conf.py", "Deployment/make_deployment_bundle.py"]
[tool.ruff.per-file-ignores]
"**/__init__.py" = ["F401", "F403"]
[tool.ruff.isort]
split-on-trailing-comma = true
known-first-party = ["influxabart"]
no-lines-before = ["local-folder"]
section-order = ["future","standard-library","third-party","first-party","this","local-folder"]
[tool.ruff.isort.sections]
"this" = ["InfluxTools"]
尚未重现此问题,但我认为您的
select
选项列表导致了此问题。我知道这不是 ruff 的默认行为。
select = ["E", "F", "B", "C4", "DTZ", "PTH", "TCH", "I001"]
尽管官方自述文件说
TC*
是flake8类型检查的代码,这个问题暗示TCH*
是。
来自自述文件:
TC001 Move application import into a type-checking block
TC002 Move third-party import into a type-checking block
TC003 Move built-in import into a type-checking block
所以
TC003
就是发生在你身上的事情。或者 TCH003
就是我猜 ruff 所说的。
所以我认为解决方案是从
"TCH"
中的 select
中删除 pyproject.toml
字段。