Skip to content

Commit 6344ca1

Browse files
Skylion007pytorchmergebot
authored andcommitted
[BE][Ez]: Apply FURB188: use str remove(pre|suf)fix (pytorch#146997)
Since we are on 3.9, we can use this nice str builtin which is more readable and more efficient. Pull Request resolved: pytorch#146997 Approved by: https://github.com/XuehaiPan, https://github.com/cyyever, https://github.com/jansel
1 parent d473c21 commit 6344ca1

File tree

10 files changed

+12
-24
lines changed

10 files changed

+12
-24
lines changed

docs/source/scripts/onnx/build_onnx_torchscript_supported_aten_op_csv_table.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ def _get_op_lists():
2525
supported_result = set()
2626
not_supported_result = set()
2727
for opname in all_schemas:
28-
if opname.endswith("_"):
29-
opname = opname[:-1]
28+
opname = opname.removesuffix("_")
3029
if opname in symbolic_schemas:
3130
# Supported op
3231
opsets = symbolic_schemas[opname].opsets

tools/autograd/gen_autograd_functions.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,16 +1033,13 @@ def emit_derivative(
10331033
)
10341034
unpack_ivalues = []
10351035
for typ, name in zip(apply_functional_args_ref_types, apply_functional_args):
1036-
if typ.endswith("&"):
1037-
typ = typ[:-1]
1036+
typ = typ.removesuffix("&")
10381037
unpack_ivalues.append(f"auto {name} = packed_args.unpack<{typ}>();")
10391038

10401039
schema_args = [f"std::array<bool, {len(input_name_to_idx)}>"]
10411040
for typ in apply_functional_args_ref_types:
1042-
if typ.endswith("&"):
1043-
typ = typ[:-1]
1044-
if typ.startswith("const"):
1045-
typ = typ[5:]
1041+
typ = typ.removesuffix("&")
1042+
typ = typ.removeprefix("const")
10461043
schema_args.append(typ.strip())
10471044
compute_schema = ["std::vector<at::TypePtr> schema = {"]
10481045
for schema_arg in schema_args:

tools/testing/clickhouse.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ def get_clickhouse_client() -> Any:
1111
endpoint = os.environ["CLICKHOUSE_ENDPOINT"]
1212
# I cannot figure out why these values aren't being handled automatically
1313
# when it is fine in the lambda
14-
if endpoint.startswith("https://"):
15-
endpoint = endpoint[len("https://") :]
14+
endpoint = endpoint.removeprefix("https://")
1615
if endpoint.endswith(":8443"):
1716
endpoint = endpoint[: -len(":8443")]
1817
return clickhouse_connect.get_client(

tools/testing/target_determination/heuristics/filepath.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ def get_keywords(file: str) -> list[str]:
6767

6868

6969
def sanitize_name(folder_name: str) -> str:
70-
if folder_name.startswith("_"):
71-
folder_name = folder_name[1:]
70+
folder_name = folder_name.removeprefix("_")
7271

7372
for syn_rep, syns in keyword_synonyms.items():
7473
if folder_name in syns or folder_name == syn_rep:

torch/_dynamo/trace_rules.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3185,10 +3185,8 @@ def _as_posix_path(path):
31853185

31863186

31873187
def _strip_init_py(s):
3188-
# TODO: Once we require py3.9 use removesuffix instead.
31893188
suffix = "__init__.py"
3190-
if s.endswith(suffix):
3191-
s = s[: -len(suffix)]
3189+
s = s.removesuffix(suffix)
31923190
return _as_posix_path(s)
31933191

31943192

torch/cuda/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,7 @@ def _sleep(cycles):
232232
def _extract_arch_version(arch_string: str):
233233
"""Extracts the architecture string from a CUDA version"""
234234
base = arch_string.split("_")[1]
235-
if base.endswith("a"):
236-
base = base[:-1]
235+
base = base.removesuffix("a")
237236
return int(base)
238237

239238

torch/distributed/distributed_c10d.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5365,8 +5365,7 @@ def _find_or_create_pg_by_ranks_and_tag(
53655365
def _get_group_tag(pg: ProcessGroup) -> str:
53665366
"""Return the tag associated with ``pg``."""
53675367
tag = _world.pg_to_tag[pg]
5368-
if tag.startswith("user:"):
5369-
tag = tag[5:]
5368+
tag = tag.removeprefix("user:")
53705369
return tag
53715370

53725371

torch/distributed/fsdp/_state_dict_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,7 @@ def param_hook(
323323
# Strip prefix out of key if needed as buffer names and param names
324324
# do not have prefix considered as they are not computed in `state_dict`
325325
# call.
326-
if clean_key.startswith(clean_prefix):
327-
clean_key = clean_key[len(clean_prefix) :]
326+
clean_key = clean_key.removeprefix(clean_prefix)
328327

329328
# Clone parameters before exiting the `_unshard_fsdp_state_params()` context.
330329
if not getattr(state_dict[fqn], "_has_been_cloned", False):

torch/export/_trace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def _fixup_key(x):
188188
def _strip_root(x):
189189
if isinstance(x, str) and x.startswith("_export_root"):
190190
stripped = x[len("_export_root") :]
191-
return stripped[1:] if stripped.startswith(".") else stripped
191+
return stripped.removeprefix(".")
192192
return x
193193

194194

torch/onnx/_internal/fx/passes/modularization.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ def module_display_name(self) -> str:
6666
"""
6767
# E.g., from 'L__self___h_1_mlp_c_proj' to 'h_1_mlp_c_proj'.
6868
name = self.module_name
69-
if name.startswith("L__self___"):
70-
name = name[len("L__self___") :]
69+
name = name.removeprefix("L__self___")
7170
return name
7271

7372
@property

0 commit comments

Comments
 (0)