Skip to content

Commit 28fd453

Browse files
committed
refactor: Format with nox
1 parent 15adb5c commit 28fd453

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# pylint: skip-file
2+
# type: ignore
3+
# -*- coding: utf-8 -*-
4+
#
5+
# Copyright 2025 Google LLC
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# https://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
import os
20+
import pathlib
21+
import subprocess
22+
23+
import nox
24+
25+
DEFAULT_PYTHON_VERSION = '3.13'
26+
27+
CURRENT_DIRECTORY = pathlib.Path(__file__).parent.absolute()
28+
29+
nox.options.sessions = [
30+
'format',
31+
]
32+
33+
# Error if a python version is missing
34+
nox.options.error_on_missing_interpreters = True
35+
36+
37+
@nox.session(python=DEFAULT_PYTHON_VERSION)
38+
def format(session):
39+
"""Format Python code using autoflake, pyupgrade, and ruff."""
40+
format_all = False
41+
42+
if format_all:
43+
lint_paths_py = ['.']
44+
else:
45+
target_branch = 'origin/main'
46+
47+
unstaged_files = subprocess.run(
48+
[
49+
'git',
50+
'diff',
51+
'--name-only',
52+
'--diff-filter=ACMRTUXB',
53+
target_branch,
54+
],
55+
stdout=subprocess.PIPE,
56+
text=True,
57+
check=False,
58+
).stdout.splitlines()
59+
60+
staged_files = subprocess.run(
61+
[
62+
'git',
63+
'diff',
64+
'--cached',
65+
'--name-only',
66+
'--diff-filter=ACMRTUXB',
67+
target_branch,
68+
],
69+
stdout=subprocess.PIPE,
70+
text=True,
71+
check=False,
72+
).stdout.splitlines()
73+
74+
committed_files = subprocess.run(
75+
[
76+
'git',
77+
'diff',
78+
'HEAD',
79+
target_branch,
80+
'--name-only',
81+
'--diff-filter=ACMRTUXB',
82+
],
83+
stdout=subprocess.PIPE,
84+
text=True,
85+
check=False,
86+
).stdout.splitlines()
87+
88+
changed_files = sorted(
89+
{
90+
file
91+
for file in (unstaged_files + staged_files + committed_files)
92+
if os.path.isfile(file)
93+
}
94+
)
95+
96+
lint_paths_py = [f for f in changed_files if f.endswith('.py')]
97+
98+
if not lint_paths_py:
99+
session.log('No changed Python files to lint.')
100+
return
101+
102+
session.install(
103+
'types-requests',
104+
'pyupgrade',
105+
'autoflake',
106+
'ruff',
107+
'no_implicit_optional',
108+
)
109+
110+
if lint_paths_py:
111+
session.run(
112+
'no_implicit_optional',
113+
'--use-union-or',
114+
*lint_paths_py,
115+
)
116+
if not format_all:
117+
session.run(
118+
'pyupgrade',
119+
'--exit-zero-even-if-changed',
120+
'--py313-plus',
121+
*lint_paths_py,
122+
)
123+
session.run(
124+
'autoflake',
125+
'-i',
126+
'-r',
127+
'--remove-all-unused-imports',
128+
*lint_paths_py,
129+
)
130+
session.run(
131+
'ruff',
132+
'check',
133+
'--fix-only',
134+
*lint_paths_py,
135+
)
136+
session.run(
137+
'ruff',
138+
'format',
139+
*lint_paths_py,
140+
)
141+

0 commit comments

Comments
 (0)