Skip to content

Commit

Permalink
Introduce ruff linter to lint all Python packages & remove duplicated…
Browse files Browse the repository at this point in the history
… ruff configs (#140)
  • Loading branch information
piglei authored Dec 13, 2023
1 parent bb13100 commit 4c73fe6
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 380 deletions.
47 changes: 12 additions & 35 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,25 @@ repos:
language: system
exclude: "^(.*/poetry.lock|.*/requirements.txt|.*/requirements_dev.txt)|sdks/apigw-manager/build.yml"
entry: bash -c "if [[ -d pre_commit_hooks ]]; then pre_commit_hooks/ip.sh $@; fi"

# See ./pyproject.toml for below tool's version and settings
- id: isort
name: isort
# Set language to disable pre-commit's virtual-env
# 总是使用 --force-exclude 来让 ruff 配置文件中的 exclude 生效
- id: ruff-check-fix
name: ruff-check-fix
language: system
types: [python]
exclude: "^(sdks/apigw-manager/.*|sdks/bkapi-client-core/.*)$"
entry: poetry run isort --settings-path=pyproject.toml
- id: black
name: black
# 修复包导入顺序问题,类似 isort 工具
entry: poetry run ruff check --force-exclude --select I --fix
- id: ruff-format
name: ruff-format
language: system
types: [python]
entry: poetry run black --config=pyproject.toml
- id: flake8
name: flak8
entry: poetry run ruff format --force-exclude
- id: ruff-check
name: ruff-check
language: system
types: [python]
entry: poetry run pflake8 --config=pyproject.toml
entry: poetry run ruff check --force-exclude

# mypy hooks for each Python project, the hooks must be separated because different
# project might use their own configurations
Expand All @@ -50,18 +51,6 @@ repos:
# sdk bkapi-client-core
- repo: local
hooks:
- id: format
name: run ruff-formatter for "bkapi-client-core"
language: python
types: [python]
entry: bash -c 'cd sdks/bkapi-client-core/ && ruff format --config=pyproject.toml --force-exclude .'
files: sdks/bkapi-client-core/
- id: ruff
name: run ruff for "bkapi-client-core"
language: python
types: [python]
entry: bash -c 'cd sdks/bkapi-client-core/ && ruff --config=pyproject.toml --force-exclude --fix .'
files: sdks/bkapi-client-core/
- id: mypy
name: run mypy for "bkapi-client-core"
language: python
Expand All @@ -71,18 +60,6 @@ repos:
# sdk apigw-manager
- repo: local
hooks:
- id: format
name: run ruff-formatter for "apigw-manager"
language: python
types: [python]
entry: bash -c 'cd sdks/apigw-manager/ && ruff format --config=pyproject.toml --force-exclude .'
files: sdks/apigw-manager/
- id: ruff
name: run ruff for "apigw-manager"
language: python
types: [python]
entry: bash -c 'cd sdks/apigw-manager/ && ruff --config=pyproject.toml --force-exclude --fix .'
files: sdks/apigw-manager/
- id: mypy
name: run mypy for "apigw-manager"
language: python
Expand Down
144 changes: 110 additions & 34 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,122 @@ description = "BlueKing PaaS Python SDK"
authors = ["blueking <[email protected]>"]

[tool.poetry.dependencies]
python = ">=3.6.2,<4.0"

[tool.poetry.dev-dependencies]
# black
black = "^21.7b0"
# isort
isort = "^5.9.2"
# flake8
pyproject-flake8 = "^0.0.1-alpha.2"
flake8-comprehensions = "^3.5.0"
# pytest
pytest = "^6.2.4"
pytest-django = "^3.9.0"
pytest-cov = "^2.8.1"

[tool.black]
python = ">=3.8.1,<3.9"

[tool.poetry.group.dev.dependencies]
ruff = "^0.1.7"

[tool.ruff]
line-length = 119
skip-string-normalization = 'true'

[tool.isort]
multi_line_output = 3
include_trailing_comma = 'true'
force_grid_wrap = 0
use_parentheses = 'true'
line_length = 119
skip_glob = ["*/migrations/**", "*/node_modules/**"]
known_local_folder = [

# 每个规则集的具体内容请参考:https://docs.astral.sh/ruff/rules/
select = [
"E",
"F",
"W",
"C90",
"B",
"PIE",
"C4",
"PL",
"RET",
"N",
"PERF",
"G",
"TRY",
"SIM",
"PT",
]

# 理论上来说,ignore 规则列表中的条目越少越好。但在实际项目中,选择性地忽略某些规则常常是必须的。
# 下面收集了常见的待忽略规则集,并将其分为两类:“推荐忽略”和“谨慎忽略”。“推荐忽略”类中所包含的规
# 则,所有项目建议默认启用。“谨慎忽略”类中的规则,项目可酌情添加。
#
# 除此处列出的规则以外,不建议随意扩充忽略规则列表。
ignore = [
# === 推荐忽略 ===
# pyflakes: 忽略后,允许使用 import *,仅在个别情况下搭配 __all__ 使用
"F403",
"F405",
# pycodestyle: 忽略后,不检查行长,最大的每行长度直接由 ruff formatter 来保证
"E501",
# pycodestyle: 忽略后,不再对多行字符串的版权头信息报错, 假如将其批量调整成普通 # 号注释后可解
"E402",
# flake8-comprehensions: 忽略后,允许使用 dict() 函数来构建字典对象
"C408",
# flake8-bugbear: 忽略后,允许对字面量属性名使用 getattr/setattr 函数
"B009",
"B010",
# flake8-bugbear: 忽略后,允许在 except 语句块中使用裸的 raise 语句,不要求必须使用 raise ... from
"B904",
# pylint:忽略后,允许定义超过 5 个参数的函数
"PLR0913",
# pylint:忽略后,允许直接和 magic value 字面量做比较,因为并非所有情况都适合抽象常量
"PLR2004",
# flake8-return: 忽略后,允许在 return 前定义一个临时变量,这有时对可读性有帮助
"RET504",
# flake8-return: 忽略后,允许使用非必须的分支包裹 return/raise 语句,这有时对可读性有帮助
"RET505",
"RET506",
# pep8-naming: 忽略后,不强制 class 内的变量使用蛇形命名,因为部分情况下驼峰命名更契合上下文
"N815",
# pep8-naming: 忽略后,允许使用短名字作为导入时的别名
"N817",
# pep8-naming: 忽略后,不强制要求异常类的名字必须以 Error 结尾
"N818",
# perflint: 忽略后,不强制要求一定用推导式替代 for 循环,因为有时循环的可读性更好
"PERF401",
# perflint: 忽略后,允许在循环中使用 try except 代码块,因为这对性能影响微乎其微,有时可读性更好
"PERF203",
# tryceratops: 忽略后,允许抛出异常时,使用较长的字符串作为异常信息,可能会降低异常的可使用性
"TRY003",
# tryceratops: 忽略后,允许在 except 语句块中使用裸的 raise 语句,不要求必须使用 raise ... from
"TRY200",
# flake8-simplify: 忽略后,允许使用 exception: pass 语句,不要求必须用 contextlib.suppress 替换
"SIM105",
# flake8-simplify: 忽略后,不强制要求用三元表达式替换普通的短 if-else 语句
"SIM108",
# flake8-pytest-style:忽略后,允许 fixture 显式指定 scope="function"
"PT003",

# === 谨慎忽略 ===
# pep8-naming: 忽略后,不强制 class 必须使用驼峰命名,部分单元测试代码中用下划线起名可能更方便
"N801",
# flake8-logging-format: 忽略后,允许在打印日志时使用 .format() 方法,可能降低性能
"G001",
# flake8-logging-format: 忽略后,允许在打印日志时使用 f-string 表达式,更可读但可能降低性能
"G004",
# pylint: 忽略后,允许使用 global 关键字修改全局变量
"PLW0603",
]

# 添加需要额外忽略的目录,比如 Django 自动生成的 migrations 目录
extend-exclude = ["*/migrations"]

[tool.ruff.format]
quote-style = "double"

[tool.ruff.isort]
# 总是显式制定 import section 的顺序
section-order = [
"future",
"standard-library",
"third-party",
"first-party",
"local-folder",
]
relative-imports-order = "closest-to-furthest"
# 添加那些不能被默认识别的 first party 的模块名
known-first-party = [
"tests",
# Knonwn Python packages
"blue_krill",
"bkpaas_auth",
"bkapi_client_core",
"bkapi_component",
"bk_storages",
"apigw_manager"
"apigw_manager",
]

[tool.flake8]
ignore = "F405,C901,E203,W503"
max-line-length = 119
max-complexity = 8
format = "pylint"
exclude = "*migrations*,*.pyc,.git,__pycache__,*/node_modules/*,*/bin/*"
[tool.ruff.lint.mccabe]
# 调大所允许的最大圈复杂度
max-complexity = 12
30 changes: 2 additions & 28 deletions sdks/apigw-manager/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4c73fe6

Please sign in to comment.