diff --git a/docs/source/scripts/onnx/build_onnx_torchscript_supported_aten_op_csv_table.py b/docs/source/scripts/onnx/build_onnx_torchscript_supported_aten_op_csv_table.py index 260202c1c17..6e512d59507 100644 --- a/docs/source/scripts/onnx/build_onnx_torchscript_supported_aten_op_csv_table.py +++ b/docs/source/scripts/onnx/build_onnx_torchscript_supported_aten_op_csv_table.py @@ -25,8 +25,7 @@ def _get_op_lists(): supported_result = set() not_supported_result = set() for opname in all_schemas: - if opname.endswith("_"): - opname = opname[:-1] + opname = opname.removesuffix("_") if opname in symbolic_schemas: # Supported op opsets = symbolic_schemas[opname].opsets diff --git a/tools/autograd/gen_autograd_functions.py b/tools/autograd/gen_autograd_functions.py index 176fa68c53b..6d5973aeed1 100644 --- a/tools/autograd/gen_autograd_functions.py +++ b/tools/autograd/gen_autograd_functions.py @@ -1033,16 +1033,13 @@ PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) { ) unpack_ivalues = [] for typ, name in zip(apply_functional_args_ref_types, apply_functional_args): - if typ.endswith("&"): - typ = typ[:-1] + typ = typ.removesuffix("&") unpack_ivalues.append(f"auto {name} = packed_args.unpack<{typ}>();") schema_args = [f"std::array"] for typ in apply_functional_args_ref_types: - if typ.endswith("&"): - typ = typ[:-1] - if typ.startswith("const"): - typ = typ[5:] + typ = typ.removesuffix("&") + typ = typ.removeprefix("const") schema_args.append(typ.strip()) compute_schema = ["std::vector schema = {"] for schema_arg in schema_args: diff --git a/tools/testing/clickhouse.py b/tools/testing/clickhouse.py index a7711aa56c9..9bc2e07d3ab 100644 --- a/tools/testing/clickhouse.py +++ b/tools/testing/clickhouse.py @@ -11,8 +11,7 @@ def get_clickhouse_client() -> Any: endpoint = os.environ["CLICKHOUSE_ENDPOINT"] # I cannot figure out why these values aren't being handled automatically # when it is fine in the lambda - if endpoint.startswith("https://"): - endpoint = endpoint[len("https://") :] + endpoint = endpoint.removeprefix("https://") if endpoint.endswith(":8443"): endpoint = endpoint[: -len(":8443")] return clickhouse_connect.get_client( diff --git a/tools/testing/target_determination/heuristics/filepath.py b/tools/testing/target_determination/heuristics/filepath.py index 0005ba5df5e..e9bdd920b4c 100644 --- a/tools/testing/target_determination/heuristics/filepath.py +++ b/tools/testing/target_determination/heuristics/filepath.py @@ -67,8 +67,7 @@ def get_keywords(file: str) -> list[str]: def sanitize_name(folder_name: str) -> str: - if folder_name.startswith("_"): - folder_name = folder_name[1:] + folder_name = folder_name.removeprefix("_") for syn_rep, syns in keyword_synonyms.items(): if folder_name in syns or folder_name == syn_rep: diff --git a/torch/_dynamo/trace_rules.py b/torch/_dynamo/trace_rules.py index 25191d7cb0d..b04c6347608 100644 --- a/torch/_dynamo/trace_rules.py +++ b/torch/_dynamo/trace_rules.py @@ -3185,10 +3185,8 @@ def _as_posix_path(path): def _strip_init_py(s): - # TODO: Once we require py3.9 use removesuffix instead. suffix = "__init__.py" - if s.endswith(suffix): - s = s[: -len(suffix)] + s = s.removesuffix(suffix) return _as_posix_path(s) diff --git a/torch/cuda/__init__.py b/torch/cuda/__init__.py index a4dd311b828..5f3632881cb 100644 --- a/torch/cuda/__init__.py +++ b/torch/cuda/__init__.py @@ -232,8 +232,7 @@ def _sleep(cycles): def _extract_arch_version(arch_string: str): """Extracts the architecture string from a CUDA version""" base = arch_string.split("_")[1] - if base.endswith("a"): - base = base[:-1] + base = base.removesuffix("a") return int(base) diff --git a/torch/distributed/distributed_c10d.py b/torch/distributed/distributed_c10d.py index 24c22897d9e..f20a606b85a 100644 --- a/torch/distributed/distributed_c10d.py +++ b/torch/distributed/distributed_c10d.py @@ -5365,8 +5365,7 @@ def _find_or_create_pg_by_ranks_and_tag( def _get_group_tag(pg: ProcessGroup) -> str: """Return the tag associated with ``pg``.""" tag = _world.pg_to_tag[pg] - if tag.startswith("user:"): - tag = tag[5:] + tag = tag.removeprefix("user:") return tag diff --git a/torch/distributed/fsdp/_state_dict_utils.py b/torch/distributed/fsdp/_state_dict_utils.py index 72b4b60b3f9..9d3c3b35259 100644 --- a/torch/distributed/fsdp/_state_dict_utils.py +++ b/torch/distributed/fsdp/_state_dict_utils.py @@ -323,8 +323,7 @@ def _full_post_state_dict_hook( # Strip prefix out of key if needed as buffer names and param names # do not have prefix considered as they are not computed in `state_dict` # call. - if clean_key.startswith(clean_prefix): - clean_key = clean_key[len(clean_prefix) :] + clean_key = clean_key.removeprefix(clean_prefix) # Clone parameters before exiting the `_unshard_fsdp_state_params()` context. if not getattr(state_dict[fqn], "_has_been_cloned", False): diff --git a/torch/export/_trace.py b/torch/export/_trace.py index ec13710e188..16053f5f33e 100644 --- a/torch/export/_trace.py +++ b/torch/export/_trace.py @@ -188,7 +188,7 @@ def _fixup_key(x): def _strip_root(x): if isinstance(x, str) and x.startswith("_export_root"): stripped = x[len("_export_root") :] - return stripped[1:] if stripped.startswith(".") else stripped + return stripped.removeprefix(".") return x diff --git a/torch/onnx/_internal/fx/passes/modularization.py b/torch/onnx/_internal/fx/passes/modularization.py index df3d54703ae..b045f497696 100644 --- a/torch/onnx/_internal/fx/passes/modularization.py +++ b/torch/onnx/_internal/fx/passes/modularization.py @@ -66,8 +66,7 @@ class _ModuleMeta: """ # E.g., from 'L__self___h_1_mlp_c_proj' to 'h_1_mlp_c_proj'. name = self.module_name - if name.startswith("L__self___"): - name = name[len("L__self___") :] + name = name.removeprefix("L__self___") return name @property