Skip to content

Commit 2c63468

Browse files
awaelchlilantiga
authored andcommitted
Enable RUF018 rule for walrus assignments in asserts (#18886)
(cherry picked from commit 018a308)
1 parent d3d7f56 commit 2c63468

File tree

15 files changed

+33
-29
lines changed

15 files changed

+33
-29
lines changed

.pre-commit-config.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ repos:
8383
- flake8-simplify
8484
- flake8-return
8585

86+
- repo: https://github.com/astral-sh/ruff-pre-commit
87+
rev: "v0.1.3"
88+
hooks:
89+
- id: ruff
90+
args: ["--fix", "--preview"]
91+
8692
- repo: https://github.com/psf/black
8793
rev: 23.9.1
8894
hooks:
@@ -120,9 +126,3 @@ repos:
120126
- id: prettier
121127
# https://prettier.io/docs/en/options.html#print-width
122128
args: ["--print-width=120"]
123-
124-
- repo: https://github.com/astral-sh/ruff-pre-commit
125-
rev: "v0.0.292"
126-
hooks:
127-
- id: ruff
128-
args: ["--fix"]

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ select = [
5353
"E", "W", # see: https://pypi.org/project/pycodestyle
5454
"F", # see: https://pypi.org/project/pyflakes
5555
"S", # see: https://pypi.org/project/flake8-bandit
56+
"RUF018", # see: https://docs.astral.sh/ruff/rules/assignment-in-assert
5657
]
5758
extend-select = [
5859
"I", # see: isort
@@ -64,6 +65,7 @@ extend-select = [
6465
ignore = [
6566
"E731", # Do not assign a lambda expression, use a def
6667
"S108",
68+
"E203", # conflicts with black
6769
]
6870
# Exclude a variety of commonly ignored directories.
6971
exclude = [

src/lightning/app/cli/connect/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def connect_app(app_name_or_id: str):
112112

113113
for command_name, metadata in retriever.api_commands.items():
114114
if "cls_path" in metadata:
115-
target_file = os.path.join(commands_folder, f"{command_name.replace(' ','_')}.py")
115+
target_file = os.path.join(commands_folder, f"{command_name.replace(' ', '_')}.py")
116116
_download_command(
117117
command_name,
118118
metadata["cls_path"],

src/lightning/app/source_code/tar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def _get_split_size(
9797
max_size = max_split_count * (1 << 31) # max size per part limited by Requests or urllib as shown in ref above
9898
if total_size > max_size:
9999
raise click.ClickException(
100-
f"The size of the datastore to be uploaded is bigger than our {max_size/(1 << 40):.2f} TBytes limit"
100+
f"The size of the datastore to be uploaded is bigger than our {max_size / (1 << 40):.2f} TBytes limit"
101101
)
102102

103103
split_size = minimum_split_size

src/lightning/pytorch/callbacks/lr_monitor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ def _add_prefix(
272272
def _add_suffix(self, name: str, param_groups: List[Dict], param_group_index: int, use_names: bool = True) -> str:
273273
if len(param_groups) > 1:
274274
if not use_names:
275-
return f"{name}/pg{param_group_index+1}"
276-
pg_name = param_groups[param_group_index].get("name", f"pg{param_group_index+1}")
275+
return f"{name}/pg{param_group_index + 1}"
276+
pg_name = param_groups[param_group_index].get("name", f"pg{param_group_index + 1}")
277277
return f"{name}/{pg_name}"
278278
if use_names:
279279
pg_name = param_groups[param_group_index].get("name")

src/lightning/pytorch/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,8 @@ def get_automatic(
627627
if len(optimizers) > 1 or len(lr_schedulers) > 1:
628628
raise MisconfigurationException(
629629
f"`{self.__class__.__name__}.add_configure_optimizers_method_to_model` expects at most one optimizer "
630-
f"and one lr_scheduler to be 'AUTOMATIC', but found {optimizers+lr_schedulers}. In this case the user "
631-
"is expected to link the argument groups and implement `configure_optimizers`, see "
630+
f"and one lr_scheduler to be 'AUTOMATIC', but found {optimizers + lr_schedulers}. In this case the "
631+
"user is expected to link the argument groups and implement `configure_optimizers`, see "
632632
"https://lightning.ai/docs/pytorch/stable/common/lightning_cli.html"
633633
"#optimizers-and-learning-rate-schedulers"
634634
)

tests/tests_app/core/test_lightning_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ def test_configure_api():
537537
asyncio.set_event_loop(loop)
538538
results = loop.run_until_complete(asyncio.gather(*coros))
539539
response_time = time() - t0
540-
print(f"RPS: {N/response_time}")
540+
print(f"RPS: {N / response_time}")
541541
assert response_time < 10
542542
assert len(results) == N
543543
assert all(r.get("detail", None) == ("HERE" if i % 5 == 0 else None) for i, r in enumerate(results))

tests/tests_app/storage/test_copier.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_copier_handles_exception(stat_mock, dir_mock, monkeypatch):
6363
copy_request_queue.put(request)
6464
copier.run_once()
6565
response = copy_response_queue.get()
66-
assert type(response.exception) == OSError
66+
assert type(response.exception) is OSError
6767
assert response.exception.args[0] == "Something went wrong"
6868

6969

tests/tests_app/utilities/test_cli_helpers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ def test_arrow_time_callback():
5151
assert _arrow_time_callback(Mock(), Mock(), "2022-08-23 12:34:00.000") == arrow.Arrow(2022, 8, 23, 12, 34)
5252

5353
# Just check humanized format is parsed
54-
assert type(_arrow_time_callback(Mock(), Mock(), "48 hours ago")) == arrow.Arrow
54+
assert type(_arrow_time_callback(Mock(), Mock(), "48 hours ago")) is arrow.Arrow
5555

56-
assert type(_arrow_time_callback(Mock(), Mock(), "60 minutes ago")) == arrow.Arrow
56+
assert type(_arrow_time_callback(Mock(), Mock(), "60 minutes ago")) is arrow.Arrow
5757

58-
assert type(_arrow_time_callback(Mock(), Mock(), "120 seconds ago")) == arrow.Arrow
58+
assert type(_arrow_time_callback(Mock(), Mock(), "120 seconds ago")) is arrow.Arrow
5959

6060
# Check raising errors
6161
with pytest.raises(Exception, match="cannot parse time Mon"):

tests/tests_app/utilities/test_exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_4xx_exceptions_caught_in_subcommands(self, mock_api_handled_group, mock
5959

6060
mock_subcommand.invoke.assert_called
6161
assert result.exit_code == 1
62-
assert type(result.exception) == ClickException
62+
assert type(result.exception) is ClickException
6363
assert api_error_msg == str(result.exception)
6464

6565
def test_original_thrown_if_cannot_decode_body(self, mock_api_handled_group, mock_subcommand):
@@ -81,4 +81,4 @@ def test_original_thrown_if_cannot_decode_body(self, mock_api_handled_group, moc
8181

8282
mock_subcommand.invoke.assert_called
8383
assert result.exit_code == 1
84-
assert type(result.exception) == ApiException
84+
assert type(result.exception) is ApiException

0 commit comments

Comments
 (0)