[BE][Ez]: Apply FURB188: use str remove(pre|suf)fix (#146997)

Since we are on 3.9, we can use this nice str builtin which is more readable and more efficient.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/146997
Approved by: https://github.com/XuehaiPan, https://github.com/cyyever, https://github.com/jansel
This commit is contained in:
Aaron Gokaslan 2025-02-14 03:38:07 +00:00 committed by PyTorch MergeBot
parent d473c212fd
commit 6344ca1dd4
10 changed files with 12 additions and 24 deletions

View File

@ -25,8 +25,7 @@ def _get_op_lists():
supported_result = set() supported_result = set()
not_supported_result = set() not_supported_result = set()
for opname in all_schemas: for opname in all_schemas:
if opname.endswith("_"): opname = opname.removesuffix("_")
opname = opname[:-1]
if opname in symbolic_schemas: if opname in symbolic_schemas:
# Supported op # Supported op
opsets = symbolic_schemas[opname].opsets opsets = symbolic_schemas[opname].opsets

View File

@ -1033,16 +1033,13 @@ PyObject* THP${op}_${name}_getter(THPCppFunction *self, void *_unused) {
) )
unpack_ivalues = [] unpack_ivalues = []
for typ, name in zip(apply_functional_args_ref_types, apply_functional_args): for typ, name in zip(apply_functional_args_ref_types, apply_functional_args):
if typ.endswith("&"): typ = typ.removesuffix("&")
typ = typ[:-1]
unpack_ivalues.append(f"auto {name} = packed_args.unpack<{typ}>();") unpack_ivalues.append(f"auto {name} = packed_args.unpack<{typ}>();")
schema_args = [f"std::array<bool, {len(input_name_to_idx)}>"] schema_args = [f"std::array<bool, {len(input_name_to_idx)}>"]
for typ in apply_functional_args_ref_types: for typ in apply_functional_args_ref_types:
if typ.endswith("&"): typ = typ.removesuffix("&")
typ = typ[:-1] typ = typ.removeprefix("const")
if typ.startswith("const"):
typ = typ[5:]
schema_args.append(typ.strip()) schema_args.append(typ.strip())
compute_schema = ["std::vector<at::TypePtr> schema = {"] compute_schema = ["std::vector<at::TypePtr> schema = {"]
for schema_arg in schema_args: for schema_arg in schema_args:

View File

@ -11,8 +11,7 @@ def get_clickhouse_client() -> Any:
endpoint = os.environ["CLICKHOUSE_ENDPOINT"] endpoint = os.environ["CLICKHOUSE_ENDPOINT"]
# I cannot figure out why these values aren't being handled automatically # I cannot figure out why these values aren't being handled automatically
# when it is fine in the lambda # when it is fine in the lambda
if endpoint.startswith("https://"): endpoint = endpoint.removeprefix("https://")
endpoint = endpoint[len("https://") :]
if endpoint.endswith(":8443"): if endpoint.endswith(":8443"):
endpoint = endpoint[: -len(":8443")] endpoint = endpoint[: -len(":8443")]
return clickhouse_connect.get_client( return clickhouse_connect.get_client(

View File

@ -67,8 +67,7 @@ def get_keywords(file: str) -> list[str]:
def sanitize_name(folder_name: str) -> str: def sanitize_name(folder_name: str) -> str:
if folder_name.startswith("_"): folder_name = folder_name.removeprefix("_")
folder_name = folder_name[1:]
for syn_rep, syns in keyword_synonyms.items(): for syn_rep, syns in keyword_synonyms.items():
if folder_name in syns or folder_name == syn_rep: if folder_name in syns or folder_name == syn_rep:

View File

@ -3185,10 +3185,8 @@ def _as_posix_path(path):
def _strip_init_py(s): def _strip_init_py(s):
# TODO: Once we require py3.9 use removesuffix instead.
suffix = "__init__.py" suffix = "__init__.py"
if s.endswith(suffix): s = s.removesuffix(suffix)
s = s[: -len(suffix)]
return _as_posix_path(s) return _as_posix_path(s)

View File

@ -232,8 +232,7 @@ def _sleep(cycles):
def _extract_arch_version(arch_string: str): def _extract_arch_version(arch_string: str):
"""Extracts the architecture string from a CUDA version""" """Extracts the architecture string from a CUDA version"""
base = arch_string.split("_")[1] base = arch_string.split("_")[1]
if base.endswith("a"): base = base.removesuffix("a")
base = base[:-1]
return int(base) return int(base)

View File

@ -5365,8 +5365,7 @@ def _find_or_create_pg_by_ranks_and_tag(
def _get_group_tag(pg: ProcessGroup) -> str: def _get_group_tag(pg: ProcessGroup) -> str:
"""Return the tag associated with ``pg``.""" """Return the tag associated with ``pg``."""
tag = _world.pg_to_tag[pg] tag = _world.pg_to_tag[pg]
if tag.startswith("user:"): tag = tag.removeprefix("user:")
tag = tag[5:]
return tag return tag

View File

@ -323,8 +323,7 @@ def _full_post_state_dict_hook(
# Strip prefix out of key if needed as buffer names and param names # 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` # do not have prefix considered as they are not computed in `state_dict`
# call. # call.
if clean_key.startswith(clean_prefix): clean_key = clean_key.removeprefix(clean_prefix)
clean_key = clean_key[len(clean_prefix) :]
# Clone parameters before exiting the `_unshard_fsdp_state_params()` context. # Clone parameters before exiting the `_unshard_fsdp_state_params()` context.
if not getattr(state_dict[fqn], "_has_been_cloned", False): if not getattr(state_dict[fqn], "_has_been_cloned", False):

View File

@ -188,7 +188,7 @@ def _fixup_key(x):
def _strip_root(x): def _strip_root(x):
if isinstance(x, str) and x.startswith("_export_root"): if isinstance(x, str) and x.startswith("_export_root"):
stripped = x[len("_export_root") :] stripped = x[len("_export_root") :]
return stripped[1:] if stripped.startswith(".") else stripped return stripped.removeprefix(".")
return x return x

View File

@ -66,8 +66,7 @@ class _ModuleMeta:
""" """
# E.g., from 'L__self___h_1_mlp_c_proj' to 'h_1_mlp_c_proj'. # E.g., from 'L__self___h_1_mlp_c_proj' to 'h_1_mlp_c_proj'.
name = self.module_name name = self.module_name
if name.startswith("L__self___"): name = name.removeprefix("L__self___")
name = name[len("L__self___") :]
return name return name
@property @property