Skip to content

Commit 6330d62

Browse files
authored
[ci] enable ruff-format on some files, add pre-commit config (#6308)
1 parent cc733f8 commit 6330d62

File tree

10 files changed

+207
-171
lines changed

10 files changed

+207
-171
lines changed

.ci/get_workflow_status.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,26 @@ def get_runs(trigger_phrase):
3232
"""
3333
pr_runs = []
3434
if environ.get("GITHUB_EVENT_NAME", "") == "pull_request":
35-
pr_number = int(environ.get("GITHUB_REF").split('/')[-2])
35+
pr_number = int(environ.get("GITHUB_REF").split("/")[-2])
3636
page = 1
3737
while True:
3838
req = request.Request(
3939
url="{}/repos/microsoft/LightGBM/issues/{}/comments?page={}&per_page=100".format(
40-
environ.get("GITHUB_API_URL"),
41-
pr_number,
42-
page
40+
environ.get("GITHUB_API_URL"), pr_number, page
4341
),
44-
headers={"Accept": "application/vnd.github.v3+json"}
42+
headers={"Accept": "application/vnd.github.v3+json"},
4543
)
4644
url = request.urlopen(req)
47-
data = json.loads(url.read().decode('utf-8'))
45+
data = json.loads(url.read().decode("utf-8"))
4846
url.close()
4947
if not data:
5048
break
51-
runs_on_page = [i for i in data
52-
if i['author_association'].lower() in {'owner', 'member', 'collaborator'}
53-
and i['body'].startswith('/gha run {}'.format(trigger_phrase))]
49+
runs_on_page = [
50+
i
51+
for i in data
52+
if i["author_association"].lower() in {"owner", "member", "collaborator"}
53+
and i["body"].startswith("/gha run {}".format(trigger_phrase))
54+
]
5455
pr_runs.extend(runs_on_page)
5556
page += 1
5657
return pr_runs[::-1]
@@ -70,20 +71,20 @@ def get_status(runs):
7071
The most recent status of workflow.
7172
Can be 'success', 'failure' or 'in-progress'.
7273
"""
73-
status = 'success'
74+
status = "success"
7475
for run in runs:
75-
body = run['body']
76+
body = run["body"]
7677
if "Status: " in body:
7778
if "Status: skipped" in body:
7879
continue
7980
if "Status: failure" in body:
80-
status = 'failure'
81+
status = "failure"
8182
break
8283
if "Status: success" in body:
83-
status = 'success'
84+
status = "success"
8485
break
8586
else:
86-
status = 'in-progress'
87+
status = "in-progress"
8788
break
8889
return status
8990

@@ -92,8 +93,8 @@ def get_status(runs):
9293
trigger_phrase = argv[1]
9394
while True:
9495
status = get_status(get_runs(trigger_phrase))
95-
if status != 'in-progress':
96+
if status != "in-progress":
9697
break
9798
sleep(60)
98-
if status == 'failure':
99+
if status == "failure":
99100
exit(1)

.ci/lint-python.sh

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
#!/bin/sh
22

3-
echo "running ruff"
4-
ruff check \
5-
--config=./python-package/pyproject.toml \
6-
. \
7-
|| exit 1
8-
echo "done running ruff"
9-
10-
echo "running isort"
11-
isort \
12-
--check-only \
13-
--settings-path=./python-package/pyproject.toml \
14-
. \
15-
|| exit 1
16-
echo "done running isort"
3+
echo "running pre-commit checks"
4+
pre-commit run --all-files || exit 1
5+
echo "done running pre-commit checks"
176

187
echo "running mypy"
198
mypy \

.ci/test.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ if [[ $TASK == "lint" ]]; then
7474
${CONDA_PYTHON_REQUIREMENT} \
7575
cmakelint \
7676
cpplint \
77-
isort \
7877
mypy \
79-
'r-lintr>=3.1' \
80-
ruff
78+
'pre-commit>=3.6.0' \
79+
'r-lintr>=3.1'
8180
source activate $CONDA_ENV
8281
echo "Linting Python code"
8382
sh ${BUILD_DIRECTORY}/.ci/lint-python.sh || exit 1

.nuget/create_nuget.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
copyfile(source / "lib_lightgbm.dylib", osx_folder_path / "lib_lightgbm.dylib")
2121
copyfile(source / "lib_lightgbm.dll", windows_folder_path / "lib_lightgbm.dll")
2222
copyfile(source / "lightgbm.exe", windows_folder_path / "lightgbm.exe")
23-
version = (current_dir.parent / 'VERSION.txt').read_text(encoding='utf-8').strip().replace('rc', '-rc')
23+
version = (current_dir.parent / "VERSION.txt").read_text(encoding="utf-8").strip().replace("rc", "-rc")
2424
nuget_str = rf"""<?xml version="1.0"?>
2525
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
2626
<metadata>
@@ -75,6 +75,6 @@
7575
</Target>
7676
</Project>
7777
"""
78-
(current_dir / "LightGBM.nuspec").write_text(nuget_str, encoding='utf-8')
79-
(current_dir / "build" / "LightGBM.props").write_text(prop_str, encoding='utf-8')
80-
(current_dir / "build" / "LightGBM.targets").write_text(target_str, encoding='utf-8')
78+
(current_dir / "LightGBM.nuspec").write_text(nuget_str, encoding="utf-8")
79+
(current_dir / "build" / "LightGBM.props").write_text(prop_str, encoding="utf-8")
80+
(current_dir / "build" / "LightGBM.targets").write_text(target_str, encoding="utf-8")

.pre-commit-config.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
exclude: |
2+
(?x)^(
3+
build|
4+
external_libs|
5+
lightgbm-python|
6+
lightgbm_r|
7+
)$
8+
9+
repos:
10+
- repo: https://github.com/astral-sh/ruff-pre-commit
11+
# Ruff version.
12+
rev: v0.2.1
13+
hooks:
14+
# Run the linter.
15+
- id: ruff
16+
args: ["--config", "python-package/pyproject.toml"]
17+
# Run the formatter.
18+
- id: ruff-format
19+
args: ["--config", "python-package/pyproject.toml"]
20+
- repo: https://github.com/pycqa/isort
21+
rev: 5.13.2
22+
hooks:
23+
- id: isort
24+
name: isort (python)
25+
args: ["--settings-path", "python-package/pyproject.toml"]

CONTRIBUTING.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
1-
LightGBM has been developed and used by many active community members. Your help is very valuable to make it better for everyone.
1+
# contributing
2+
3+
LightGBM has been developed and used by many active community members.
4+
5+
Your help is very valuable to make it better for everyone.
6+
7+
## How to Contribute
28

39
- Check for the [Roadmap](https://github.com/microsoft/LightGBM/projects/1) and the [Feature Requests Hub](https://github.com/microsoft/LightGBM/issues/2302), and submit pull requests to address chosen issue. If you need development guideline, you can check the [Development Guide](https://github.com/microsoft/LightGBM/blob/master/docs/Development-Guide.rst) or directly ask us in Issues/Pull Requests.
410
- Contribute to the [tests](https://github.com/microsoft/LightGBM/tree/master/tests) to make it more reliable.
511
- Contribute to the [documentation](https://github.com/microsoft/LightGBM/tree/master/docs) to make it clearer for everyone.
612
- Contribute to the [examples](https://github.com/microsoft/LightGBM/tree/master/examples) to share your experience with other users.
713
- Add your stories and experience to [Awesome LightGBM](https://github.com/microsoft/LightGBM/blob/master/examples/README.md). If LightGBM helped you in a machine learning competition or some research application, we want to hear about it!
814
- [Open an issue](https://github.com/microsoft/LightGBM/issues) to report problems or recommend new features.
15+
16+
## Development Guide
17+
18+
### Linting
19+
20+
Every commit in the repository is tested with multiple static analyzers.
21+
22+
When developing locally, run some of them using `pre-commit` ([pre-commit docs](https://pre-commit.com/)).
23+
24+
```shell
25+
pre-commit run --all-files
26+
```
27+
28+
That command will check for some issues and automatically reformat the code.

0 commit comments

Comments
 (0)