Skip to content

Commit 1a1e741

Browse files
authored
Use ruff for formatting (#6434)
* Use `ruff` for formatting * Updat quality dependencies and lint setup.py * Update pre-commit-config * Small fix
1 parent c65315e commit 1a1e741

File tree

12 files changed

+48
-47
lines changed

12 files changed

+48
-47
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ jobs:
2828
pip install .[quality]
2929
- name: Check quality
3030
run: |
31-
black --check tests src benchmarks metrics
32-
ruff tests src benchmarks metrics
31+
ruff check tests src benchmarks metrics utils setup.py # linter
32+
ruff format --check tests src benchmarks metrics utils setup.py # formatter
3333
3434
test:
3535
needs: check_code_quality

.pre-commit-config.yaml

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
repos:
2-
- repo: https://github.com/psf/black
3-
rev: 23.1.0
2+
- repo: https://github.com/charliermarsh/ruff-pre-commit # https://github.com/charliermarsh/ruff#usage
3+
rev: 'v0.1.5'
44
hooks:
5-
- id: black
6-
language_version: python3
7-
types: [python]
8-
stages: [commit]
9-
args: ["--config", "pyproject.toml", "tests", "src", "benchmarks", "metrics"]
10-
- repo: https://github.com/charliermarsh/ruff-pre-commit
11-
rev: 'v0.0.255'
12-
hooks:
13-
- id: ruff
14-
stages: [commit]
15-
args: [ "--config", "pyproject.toml", "tests", "src", "benchmarks", "metrics", "--fix"]
5+
# Run the linter.
6+
- id: ruff
7+
args: [ --fix ]
8+
# Run the formatter.
9+
- id: ruff-format

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ check_dirs := tests src benchmarks metrics utils
55
# Check that source code meets quality standards
66

77
quality:
8-
black --check $(check_dirs) setup.py
9-
ruff $(check_dirs) setup.py
8+
ruff check $(check_dirs) setup.py # linter
9+
ruff format --check $(check_dirs) setup.py # formatter
1010

1111
# Format source code automatically
1212

1313
style:
14-
black tests src benchmarks metrics setup.py
15-
ruff $(check_dirs) setup.py --fix
14+
ruff check --fix $(check_dirs) setup.py # linter
15+
ruff format $(check_dirs) setup.py # formatter
1616

1717
# Run tests for the library
1818

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@
216216
TESTS_REQUIRE.extend(VISION_REQUIRE)
217217
TESTS_REQUIRE.extend(AUDIO_REQUIRE)
218218

219-
QUALITY_REQUIRE = ["black~=23.1", "ruff>=0.0.241", "pyyaml>=5.3.1"]
219+
QUALITY_REQUIRE = ["ruff>=0.1.5"]
220220

221221
DOCS_REQUIRE = [
222222
# Might need to add doc-builder and some specific deps in the future

src/datasets/arrow_dataset.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3101,7 +3101,8 @@ def load_processed_shard_from_cache(shard_kwargs):
31013101
else:
31023102

31033103
def format_cache_file_name(
3104-
cache_file_name: Optional[str], rank: Union[int, Literal["*"]] # noqa: F722
3104+
cache_file_name: Optional[str],
3105+
rank: Union[int, Literal["*"]], # noqa: F722
31053106
) -> Optional[str]:
31063107
if not cache_file_name:
31073108
return cache_file_name

src/datasets/iterable_dataset.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ def _convert_to_arrow(
127127
Drop the last batch if it is smaller than `batch_size`.
128128
"""
129129
if batch_size is None or batch_size <= 0:
130-
yield "all", pa.Table.from_pylist(
131-
cast_to_python_objects([example for _, example in iterable], only_1d_for_numpy=True)
130+
yield (
131+
"all",
132+
pa.Table.from_pylist(cast_to_python_objects([example for _, example in iterable], only_1d_for_numpy=True)),
132133
)
133134
return
134135
iterator = iter(iterable)
@@ -1112,8 +1113,9 @@ def __iter__(self):
11121113
# Then for each example, `TypedExamplesIterable` automatically fills missing columns with None.
11131114
# This is done with `_apply_feature_types_on_example`.
11141115
for key, example in self.ex_iterable:
1115-
yield key, _apply_feature_types_on_example(
1116-
example, self.features, token_per_repo_id=self.token_per_repo_id
1116+
yield (
1117+
key,
1118+
_apply_feature_types_on_example(example, self.features, token_per_repo_id=self.token_per_repo_id),
11171119
)
11181120

11191121
def _iter_arrow(self) -> Iterator[Tuple[Key, pa.Table]]:

src/datasets/load.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,9 +1493,7 @@ def dataset_module_factory(
14931493
download_config=download_config,
14941494
download_mode=download_mode,
14951495
).get_module()
1496-
except (
1497-
Exception
1498-
) as e1: # noqa all the attempts failed, before raising the error we should check if the module is already cached.
1496+
except Exception as e1: # noqa all the attempts failed, before raising the error we should check if the module is already cached.
14991497
try:
15001498
return CachedDatasetModuleFactory(path, dynamic_modules_path=dynamic_modules_path).get_module()
15011499
except Exception: # noqa if it's not in the cache, then it doesn't exist.
@@ -1598,9 +1596,7 @@ def metric_module_factory(
15981596
download_mode=download_mode,
15991597
dynamic_modules_path=dynamic_modules_path,
16001598
).get_module()
1601-
except (
1602-
Exception
1603-
) as e1: # noqa all the attempts failed, before raising the error we should check if the module is already cached.
1599+
except Exception as e1: # noqa all the attempts failed, before raising the error we should check if the module is already cached.
16041600
try:
16051601
return CachedMetricModuleFactory(path, dynamic_modules_path=dynamic_modules_path).get_module()
16061602
except Exception: # noqa if it's not in the cache, then it doesn't exist.

src/datasets/packaged_modules/folder_based_builder/folder_based_builder.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,15 @@ def _generate_examples(self, files, metadata_files, split_name, add_metadata, ad
323323
sample_label = {"label": os.path.basename(os.path.dirname(original_file))}
324324
else:
325325
sample_label = {}
326-
yield file_idx, {
327-
**sample_empty_metadata,
328-
self.BASE_COLUMN_NAME: downloaded_file_or_dir,
329-
**sample_metadata,
330-
**sample_label,
331-
}
326+
yield (
327+
file_idx,
328+
{
329+
**sample_empty_metadata,
330+
self.BASE_COLUMN_NAME: downloaded_file_or_dir,
331+
**sample_metadata,
332+
**sample_label,
333+
},
334+
)
332335
file_idx += 1
333336
else:
334337
for downloaded_dir_file in downloaded_file_or_dir:
@@ -391,10 +394,13 @@ def _generate_examples(self, files, metadata_files, split_name, add_metadata, ad
391394
sample_label = {"label": os.path.basename(os.path.dirname(downloaded_dir_file))}
392395
else:
393396
sample_label = {}
394-
yield file_idx, {
395-
**sample_empty_metadata,
396-
self.BASE_COLUMN_NAME: downloaded_dir_file,
397-
**sample_metadata,
398-
**sample_label,
399-
}
397+
yield (
398+
file_idx,
399+
{
400+
**sample_empty_metadata,
401+
self.BASE_COLUMN_NAME: downloaded_dir_file,
402+
**sample_metadata,
403+
**sample_label,
404+
},
405+
)
400406
file_idx += 1

src/datasets/splits.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class SplitBase(metaclass=abc.ABCMeta):
111111
to define which files to read and how to skip examples within file.
112112
113113
"""
114+
114115
# pylint: enable=line-too-long
115116

116117
@abc.abstractmethod
@@ -265,6 +266,7 @@ class PercentSlice(metaclass=PercentSliceMeta):
265266
[guide on splits](../loading#slice-splits)
266267
for more information.
267268
"""
269+
268270
# pylint: enable=line-too-long
269271
pass
270272

@@ -438,6 +440,7 @@ class Split:
438440
... )
439441
```
440442
"""
443+
441444
# pylint: enable=line-too-long
442445
TRAIN = NamedSplit("train")
443446
TEST = NamedSplit("test")

src/datasets/utils/patching.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def __enter__(self):
6363
# We don't check for the name of the global, but rather if its value *is* "os" or "os.path".
6464
# This allows to patch renamed modules like "from os import path as ospath".
6565
if obj_attr is submodule or (
66-
(isinstance(obj_attr, _PatchedModuleObj) and obj_attr._original_module is submodule)
66+
isinstance(obj_attr, _PatchedModuleObj) and obj_attr._original_module is submodule
6767
):
6868
self.original[attr] = obj_attr
6969
# patch at top level

0 commit comments

Comments
 (0)