Skip to content

Commit c9047c5

Browse files
committed
feat: switch code (re)formatting to ruff
1 parent cca73ba commit c9047c5

File tree

3 files changed

+32
-43
lines changed

3 files changed

+32
-43
lines changed

codegen/__init__.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@
2626
get_data_validator_instance,
2727
)
2828

29-
# Target Python version for code formatting with Black.
30-
# Must be one of the values listed in pyproject.toml.
31-
BLACK_TARGET_VERSIONS = "py38 py39 py310 py311 py312"
32-
3329

3430
# Import notes
3531
# ------------
@@ -89,6 +85,12 @@ def preprocess_schema(plotly_schema):
8985
items["colorscale"] = items.pop("concentrationscales")
9086

9187

88+
def reformat_code():
89+
"""Reformat Python code using settings in pyproject.toml."""
90+
91+
subprocess.call(["ruff", "format", "."])
92+
93+
9294
def perform_codegen(noformat=False):
9395
# Set root codegen output directory
9496
# ---------------------------------
@@ -344,12 +346,7 @@ def __getattr__(import_name):
344346
if noformat:
345347
print("skipping reformatting")
346348
else:
347-
target_version = [
348-
f"--target-version={v}" for v in BLACK_TARGET_VERSIONS.split()
349-
]
350-
subprocess.call(["black", *target_version, validators_pkgdir])
351-
subprocess.call(["black", *target_version, graph_objs_pkgdir])
352-
subprocess.call(["black", *target_version, graph_objects_path])
349+
reformat_code()
353350

354351

355352
if __name__ == "__main__":

commands.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Utility command runner."""
22

33
import argparse
4-
from distutils import log
4+
import logging
55
import json
66
import os
77
import platform
@@ -11,9 +11,10 @@
1111
import sys
1212
import time
1313

14-
from codegen import perform_codegen
14+
from codegen import perform_codegen, reformat_code
1515

1616

17+
LOGGER = logging.getLogger(__name__)
1718
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
1819
NODE_ROOT = os.path.join(PROJECT_ROOT, "js")
1920
NODE_MODULES = os.path.join(NODE_ROOT, "node_modules")
@@ -54,19 +55,19 @@ def install_js_deps(local):
5455

5556
skip_npm = os.environ.get("SKIP_NPM", False)
5657
if skip_npm:
57-
log.info("Skipping npm-installation")
58+
LOGGER.info("Skipping npm-installation")
5859
return
5960

6061
if not has_npm:
61-
log.error(
62+
LOGGER.error(
6263
"`npm` unavailable. If you're running this command using sudo, make sure `npm` is available to sudo"
6364
)
6465

6566
env = os.environ.copy()
6667
env["PATH"] = NPM_PATH
6768

6869
if has_npm:
69-
log.info("Installing build dependencies with npm. This may take a while...")
70+
LOGGER.info("Installing build dependencies with npm. This may take a while...")
7071
check_call(
7172
[npmName, "install"],
7273
cwd=NODE_ROOT,
@@ -130,9 +131,7 @@ def overwrite_plotlyjs_version_file(plotlyjs_version):
130131
# DO NOT EDIT
131132
# This file is generated by the updatebundle commands.py command
132133
__plotlyjs_version__ = "{plotlyjs_version}"
133-
""".format(
134-
plotlyjs_version=plotlyjs_version
135-
)
134+
""".format(plotlyjs_version=plotlyjs_version)
136135
)
137136

138137

@@ -223,7 +222,6 @@ def update_plotlyjs(plotly_js_version):
223222

224223
# Update the plotly.js schema and bundle from master
225224
def update_schema_bundle_from_master():
226-
227225
if "--devrepo" in sys.argv:
228226
devrepo = sys.argv[sys.argv.index("--devrepo") + 1]
229227
else:
@@ -295,9 +293,15 @@ def parse_args():
295293
subparsers = parser.add_subparsers(dest="cmd", help="Available subcommands")
296294

297295
p_codegen = subparsers.add_parser("codegen", help="generate code")
298-
p_codegen.add_argument("--noformat", action="store_true", help="prevent reformatting")
296+
p_codegen.add_argument(
297+
"--noformat", action="store_true", help="prevent reformatting"
298+
)
299299

300-
p_updateplotlyjsdev = subparsers.add_parser("updateplotlyjsdev", help="update plotly.js for development")
300+
p_format = subparsers.add_parser("format", help="reformat code")
301+
302+
p_updateplotlyjsdev = subparsers.add_parser(
303+
"updateplotlyjsdev", help="update plotly.js for development"
304+
)
301305

302306
p_updateplotlyjs = subparsers.add_parser("updateplotlyjs", help="update plotly.js")
303307

@@ -312,6 +316,9 @@ def main():
312316
if args.cmd == "codegen":
313317
perform_codegen(noformat=args.noformat)
314318

319+
elif args.cmd == "format":
320+
reformat_code()
321+
315322
elif args.cmd == "updateplotlyjsdev":
316323
update_plotlyjs_dev()
317324

pyproject.toml

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ dependencies = [
4747
[project.optional-dependencies]
4848
express = ["numpy"]
4949
kaleido = ["kaleido==1.0.0rc15"]
50-
dev = ["black==25.1.0"]
50+
dev = [
51+
"inflect=7.5.0",
52+
"requests==2.32.3",
53+
"ruff==0.11.12"
54+
]
5155

5256
[project.scripts]
5357
plotly_get_chrome = "plotly.io._kaleido:get_chrome"
@@ -65,27 +69,8 @@ plotly = [
6569
"package_data/datasets/*",
6670
]
6771

68-
[tool.black]
69-
line-length = 88
70-
target_version = ['py38', 'py39', 'py310', 'py311', 'py312']
71-
include = '\.pyi?$'
72-
exclude = '''
73-
/(
74-
\.eggs # exclude a few common directories in the
75-
| \.git # root of the project
76-
| \.hg
77-
| \.mypy_cache
78-
| \.tox
79-
| \.venv
80-
| _build
81-
| buck-out
82-
| build
83-
| dist
84-
| js
85-
| submodules
86-
| plotly/matplotlylib/mplexporter
87-
)/
88-
'''
72+
[tool.ruff]
73+
target-version = "py38"
8974

9075
[tool.jupyter-packaging.builder]
9176
factory = "jupyter_packaging.npm_builder"

0 commit comments

Comments
 (0)