Skip to content

Commit 4ada3aa

Browse files
authored
Add option to use ruff as formatter (#537)
1 parent 79c842a commit 4ada3aa

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ It uses token analysis and
1212
- [isort][isort]
1313
- [autoflake][autoflake]
1414
- [black][black]
15+
- [ruff][ruff]
1516
- git
1617

1718
to try and update the typing syntax the best it can.
@@ -48,7 +49,8 @@ In particular, these need to be updated manually:
4849
unused. If not, revert changes with `git restore`.
4950
4. Remove unused imports with [autoflake][autoflake].
5051
5. Run [isort][isort] to try to restore the previous formatting.
51-
6. Optional: Run [black][black]. (Requires `black` to be added as `additional_dependency`)
52+
6. Optional: Run [black][black]. (Requires `black` to be added as `additional_dependency`)
53+
OR: Run [ruff][ruff]. (Requires `ruff` to be added as `additional_dependency`)
5254
7. Check `git diff` for modified comments.
5355
If one is detected, revert changes and print file name.
5456
Can be overwritten with `--force`.
@@ -101,6 +103,15 @@ To use it, add `black` as `additional_dependency` in your `.pre-commit-config.ya
101103
- black==<insert current version here!>
102104
```
103105
106+
**`--ruff`**
107+
Run `ruff check --fix` and `ruff format` after updates.
108+
To use it, add `ruff` as `additional_dependency` in your `.pre-commit-config.yaml`.
109+
110+
```yaml
111+
additional_dependencies:
112+
- ruff==<insert current version here!>
113+
```
114+
104115
**`--disable-committed-check`**
105116
Don't abort with uncommitted changes. **Don't use it in production!**
106117
Risk of losing uncommitted changes.
@@ -154,6 +165,7 @@ See [LICENSE][LICENSE_FILE] for the full license text.
154165
[isort]: https://github.com/PyCQA/isort
155166
[autoflake]: https://github.com/PyCQA/autoflake
156167
[black]: https://github.com/psf/black
168+
[ruff]: https://github.com/astral-sh/ruff
157169
[PEP585]: https://www.python.org/dev/peps/pep-0585/
158170
[PEP604]: https://www.python.org/dev/peps/pep-0604/
159171

pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ dependencies = [
3636
black = [
3737
"black==24.10.0",
3838
]
39+
ruff = [
40+
"ruff==0.6.9",
41+
]
3942

4043
[project.scripts]
4144
python-typing-update = "python_typing_update.__main__:main"

python_typing_update/__main__.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ async def async_main(argv: list[str] | None = None) -> int:
2929
description="Tool to update Python typing syntax.",
3030
formatter_class=CustomHelpFormatter,
3131
)
32+
formatter_options = parser.add_argument_group("select optional formatter")
3233
mode_options = parser.add_argument_group("select different mode")
3334
py_version_options = parser.add_argument_group("python version options")
3435

@@ -60,14 +61,21 @@ async def async_main(argv: list[str] | None = None) -> int:
6061
help="Keep updates even if no import was removed",
6162
)
6263
parser.add_argument(
64+
'--disable-committed-check',
65+
action='store_true',
66+
help="Don't abort with uncommitted changes. Don't use it in production!",
67+
)
68+
69+
group_formatter = formatter_options.add_mutually_exclusive_group()
70+
group_formatter.add_argument(
6371
'--black',
6472
action='store_true',
65-
help="Run black formatting after update",
73+
help="Run 'black' after update",
6674
)
67-
parser.add_argument(
68-
'--disable-committed-check',
75+
group_formatter.add_argument(
76+
'--ruff',
6977
action='store_true',
70-
help="Don't abort with uncommitted changes. Don't use it in production!",
78+
help="Run 'ruff check --fix' and 'ruff format' after update",
7179
)
7280

7381
group_mode = mode_options.add_mutually_exclusive_group()
@@ -95,11 +103,11 @@ async def async_main(argv: list[str] | None = None) -> int:
95103
group_py_version.add_argument(
96104
'--py38-plus',
97105
action='store_const', dest='min_version', const=(3, 8),
98-
help="Default"
99106
)
100107
group_py_version.add_argument(
101108
'--py39-plus',
102109
action='store_const', dest='min_version', const=(3, 9),
110+
help="Default"
103111
)
104112
group_py_version.add_argument(
105113
'--py310-plus',
@@ -127,10 +135,17 @@ async def async_main(argv: list[str] | None = None) -> int:
127135

128136
if args.black:
129137
try:
130-
# pylint: disable=unused-import,import-outside-toplevel
138+
# pylint: disable-next=unused-import,import-outside-toplevel
131139
import black # noqa: F401
132140
except ImportError:
133-
print("Error! Black is not installed")
141+
print("Error! Black isn't installed")
142+
return 2
143+
elif args.ruff:
144+
try:
145+
# pylint: disable-next=unused-import,import-outside-toplevel
146+
import ruff # noqa: F401
147+
except ImportError:
148+
print("Error! Ruff isn't installed")
134149
return 2
135150

136151
return await async_run(args)

python_typing_update/main.py

+8
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ async def typing_update(
107107
stderr=asyncio.subprocess.DEVNULL,
108108
)
109109
await process.communicate()
110+
elif args.ruff:
111+
process = await asyncio.create_subprocess_shell(
112+
f"ruff check --force-exclude --fix {filename} && "
113+
f"ruff format --force-exclude {filename}",
114+
stdout=asyncio.subprocess.DEVNULL,
115+
stderr=asyncio.subprocess.DEVNULL,
116+
)
117+
await process.communicate()
110118

111119
return 0, filename
112120

requirements_ruff.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ruff==0.6.9

0 commit comments

Comments
 (0)