[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()
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

View File

@ -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<bool, {len(input_name_to_idx)}>"]
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<at::TypePtr> schema = {"]
for schema_arg in schema_args:

View File

@ -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(

View File

@ -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:

View File

@ -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)

View File

@ -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)

View File

@ -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

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
# 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):

View File

@ -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

View File

@ -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