Cla*_*ltz 9 python regex django pre-commit.com pyproject.toml
我刚刚为我的 Django 存储库设置了Black和Pre-Commit。
我在我遵循的教程中使用了 Black 的默认配置,它运行良好,但我无法从中排除我的迁移文件。
这是我一直在使用的默认配置:
pyproject.toml
[tool.black]
line-length = 79
include = '\.pyi?$'
exclude = '''
/(
\.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
)/
'''
Run Code Online (Sandbox Code Playgroud)
我使用Regex101.com来确保^.*\b(migrations)\b.*$
匹配apps/examples/migrations/test.py
.
[tool.black]
line-length = 79
include = '\.pyi?$'
exclude = '''
/(
\.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
| ^.*\b(migrations)\b.*$
)/
'''
Run Code Online (Sandbox Code Playgroud)
当我将该正则表达式行添加到我的配置文件并运行时pre-commit run --all-files
,它会忽略该.git
文件夹,但仍会格式化迁移文件。
这就是问题的解决方案:pyproject.toml
[tool.black]
exclude = '''
/(
| migrations
)/
'''
Run Code Online (Sandbox Code Playgroud)
将迁移排除添加到您的.pre-commit-config.yaml
文件中
- id: black
exclude: ^.*\b(migrations)\b.*$
Run Code Online (Sandbox Code Playgroud)