Skip to content

Commit a70e95e

Browse files
committed
🎨 CodeStyle: add pre-commit
1 parent c92bed5 commit a70e95e

File tree

8 files changed

+103
-27
lines changed

8 files changed

+103
-27
lines changed

.github/workflows/release.yaml

Whitespace-only changes.

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ wheels/
99
# Virtual environments
1010
.venv
1111
src/yamlfmt/_version.py
12-
.python-version
12+
.python-version

.pre-commit-config.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
repos:
2+
# Common hooks
3+
- repo: https://github.com/pre-commit/pre-commit-hooks
4+
rev: v5.0.0
5+
hooks:
6+
- id: check-added-large-files
7+
- id: check-merge-conflict
8+
- id: check-symlinks
9+
- id: detect-private-key
10+
- id: end-of-file-fixer
11+
- id: trailing-whitespace
12+
- repo: https://github.com/astral-sh/ruff-pre-commit
13+
rev: v0.9.6
14+
hooks:
15+
- id: ruff
16+
args: [--fix, --exit-non-zero-on-fix, --no-cache]
17+
- id: ruff-format
18+
19+
# TODO: add self

hatch_build.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
from __future__ import annotations
2+
13
import os
24
import platform
5+
import shutil
36
import sys
4-
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
5-
from typing import Any
6-
from pathlib import Path
7+
import tarfile
78
import tempfile
9+
from pathlib import Path
10+
from typing import Any
811
from urllib import request
9-
import tarfile
10-
import shutil
12+
13+
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
1114

1215

1316
class SpecialBuildHook(BuildHookInterface):
@@ -29,9 +32,7 @@ def initialize(self, version: str, build_data: dict[str, Any]) -> None:
2932
if target_arch not in ["x86_64", "arm64", "aarch64", "i386"]:
3033
raise NotImplementedError(f"no support arch: {target_arch}")
3134

32-
if not any(
33-
os_name in target_os_info for os_name in ["linux", "darwin", "macos", "win"]
34-
):
35+
if not any(os_name in target_os_info for os_name in ["linux", "darwin", "macos", "win"]):
3536
raise NotImplementedError(f"no support os: {target_os_info}")
3637

3738
# 检查系统和架构的组合
@@ -59,9 +60,7 @@ def initialize(self, version: str, build_data: dict[str, Any]) -> None:
5960
# TODO: 加一个 sum 校验
6061
bin_path = self.temp_dir / self.BIN_NAME
6162
assert bin_path.is_file(), f"{self.BIN_NAME} not found"
62-
build_data["force_include"][f"{bin_path.resolve()}"] = (
63-
f"yamlfmt/{self.BIN_NAME}"
64-
)
63+
build_data["force_include"][f"{bin_path.resolve()}"] = f"yamlfmt/{self.BIN_NAME}"
6564

6665
def download_yamlfmt(self, target_os_info: str, target_arch: str) -> None:
6766
"""Download the yamlfmt binary for the specified OS and architecture."""
@@ -74,15 +73,11 @@ def download_yamlfmt(self, target_os_info: str, target_arch: str) -> None:
7473
"amd64": "x86_64",
7574
"aarch64": "arm64",
7675
}
77-
file_path = (
78-
self.temp_dir / f"{self.BIN_NAME}_{target_os_info}_{target_arch}.tar.gz"
79-
)
76+
file_path = self.temp_dir / f"{self.BIN_NAME}_{target_os_info}_{target_arch}.tar.gz"
8077
request.urlretrieve(
8178
self.YAMLFMT_REPO.format(
8279
version=self.metadata.version,
83-
target_os_info=target_os_info_to_go_os.get(
84-
target_os_info, target_os_info
85-
),
80+
target_os_info=target_os_info_to_go_os.get(target_os_info, target_os_info),
8681
target_arch=target_arch_to_go_arch.get(target_arch, target_arch),
8782
),
8883
file_path,

pyproject.toml

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ readme = "README.md"
66
requires-python = ">=3.9"
77
dependencies = []
88

9+
[dependency-groups]
10+
dev = [
11+
"hatchling>=1.27.0",
12+
]
13+
914
[project.scripts]
1015
yamlfmt = "yamlfmt.__main__:main"
1116

@@ -24,7 +29,56 @@ version.source = "vcs"
2429
requires = ["hatchling", "hatch-vcs"]
2530
build-backend = "hatchling.build"
2631

27-
[dependency-groups]
28-
dev = [
29-
"hatchling>=1.27.0",
32+
33+
[tool.ruff]
34+
line-length = 120
35+
target-version = "py39"
36+
37+
[tool.ruff.lint]
38+
select = [
39+
# Pyflakes
40+
"F",
41+
# Pycodestyle
42+
"E",
43+
"W",
44+
# Isort
45+
"I",
46+
# Comprehensions
47+
"C4",
48+
# Debugger
49+
"T100",
50+
# Pyupgrade
51+
"UP",
52+
# Flake8-pyi
53+
"PYI",
54+
# Bugbear
55+
"B",
56+
# Pylint
57+
"PLE",
58+
# Flake8-simplify
59+
"SIM101",
60+
# Flake8-use-pathlib
61+
"PTH",
62+
# Pygrep-hooks
63+
"PGH004",
64+
# Flake8-type-checking
65+
"TC",
66+
# Flake8-raise
67+
"RSE",
68+
# Refurb
69+
"FURB",
70+
# Flake8-future-annotations
71+
"FA",
72+
# Yesqa
73+
"RUF100",
74+
]
75+
ignore = [
76+
"E501", # line too long, duplicate with ruff fmt
77+
"F401", # imported but unused, duplicate with pyright
78+
"F841", # local variable is assigned to but never used, duplicate with pyright
79+
"UP038", # It will cause the performance regression on python3.10
3080
]
81+
82+
[tool.ruff.lint.isort]
83+
required-imports = ["from __future__ import annotations"]
84+
combine-as-imports = true

src/yamlfmt/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from ._version import version as __version__
24

35
__all__ = ["__version__"]

src/yamlfmt/__main__.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1+
from __future__ import annotations
2+
3+
import importlib.resources as pkg_resources
14
import os
25
import platform
3-
import sys
46
import subprocess
5-
import importlib.resources as pkg_resources
7+
import sys
8+
69
from yamlfmt import BIN_NAME
710

11+
812
def get_executable_path():
9-
with pkg_resources.as_file(pkg_resources.files('yamlfmt').joinpath(f"./{BIN_NAME}")) as p:
13+
with pkg_resources.as_file(pkg_resources.files("yamlfmt").joinpath(f"./{BIN_NAME}")) as p:
1014
executable_path = p
1115

1216
if platform.system() != "Windows":
1317
if not os.access(executable_path, os.X_OK):
14-
current_mode = os.stat(executable_path).st_mode
15-
os.chmod(executable_path, current_mode | 0o111)
18+
current_mode = executable_path.stat().st_mode
19+
executable_path.chmod(current_mode | 0o111)
1620

1721
return executable_path
1822

23+
1924
def main():
2025
executable_path = get_executable_path()
2126
result = subprocess.run([executable_path] + sys.argv[1:], check=False)
2227
sys.exit(result.returncode)
2328

29+
2430
if __name__ == "__main__":
2531
main()

src/yamlfmt/_version.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
version: str
2-
version_tuple: tuple[int, int, int, str, str] | tuple[int, int, int]
2+
version_tuple: tuple[int, int, int, str, str] | tuple[int, int, int]

0 commit comments

Comments
 (0)