Skip to content

Commit

Permalink
Refactor str.startswith with tuples (#33292)
Browse files Browse the repository at this point in the history
  • Loading branch information
eumiro authored Aug 16, 2023
1 parent 86f9f95 commit d747a79
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 14 deletions.
6 changes: 3 additions & 3 deletions airflow/template/templater.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def resolve_template_files(self) -> None:
content = getattr(self, field, None)
if content is None:
continue
elif isinstance(content, str) and any(content.endswith(ext) for ext in self.template_ext):
elif isinstance(content, str) and content.endswith(tuple(self.template_ext)):
env = self.get_template_env()
try:
setattr(self, field, env.loader.get_source(env, content)[0]) # type: ignore
Expand All @@ -79,7 +79,7 @@ def resolve_template_files(self) -> None:
elif isinstance(content, list):
env = self.get_template_env()
for i, item in enumerate(content):
if isinstance(item, str) and any(item.endswith(ext) for ext in self.template_ext):
if isinstance(item, str) and item.endswith(tuple(self.template_ext)):
try:
content[i] = env.loader.get_source(env, item)[0] # type: ignore
except Exception:
Expand Down Expand Up @@ -150,7 +150,7 @@ def render_template(
jinja_env = self.get_template_env()

if isinstance(value, str):
if any(value.endswith(ext) for ext in self.template_ext): # A filepath.
if value.endswith(tuple(self.template_ext)): # A filepath.
template = jinja_env.get_template(value)
else:
template = jinja_env.from_string(value)
Expand Down
2 changes: 1 addition & 1 deletion dev/chart/build_changelog_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def print_entry(section: str, description: str, pr_number: int | None):
break
in_first_release = True
continue
if line.startswith('"""') or line.startswith("----") or line.startswith("^^^^"):
if line.startswith(('"""', "----", "^^^^")):
continue

# Make sure we get past "significant features" before we actually start keeping track
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def get_provider_id_from_import(import_name: str, file_path: Path) -> str | None
provider_id = get_provider_id_from_relative_import_or_file(relative_provider_import)
if provider_id is None:
relative_path_from_import = relative_provider_import.replace(".", os.sep)
if any(relative_path_from_import.startswith(suspended_path) for suspended_path in suspended_paths):
if relative_path_from_import.startswith(tuple(suspended_paths)):
return None
warnings.append(f"We could not determine provider id from import {import_name} in {file_path}")
return provider_id
Expand Down Expand Up @@ -154,7 +154,7 @@ def get_provider_id_from_file_name(file_path: Path) -> str | None:
return None
provider_id = get_provider_id_from_relative_import_or_file(str(relative_path))
if provider_id is None and file_path.name not in ["__init__.py", "get_provider_info.py"]:
if any(relative_path.as_posix().startswith(suspended_path) for suspended_path in suspended_paths):
if relative_path.as_posix().startswith(tuple(suspended_paths)):
return None
else:
warnings.append(f"We had a problem to classify the file {file_path} to a provider")
Expand Down
7 changes: 3 additions & 4 deletions scripts/in_container/run_provider_yaml_files_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,19 +419,18 @@ def check_doc_files(yaml_files: dict[str, dict]):
for f in expected_doc_files
if f.name != "index.rst"
and "_partials" not in f.parts
and not any(f.relative_to(DOCS_DIR).as_posix().startswith(s) for s in suspended_providers)
and not f.relative_to(DOCS_DIR).as_posix().startswith(tuple(suspended_providers))
} | {
f"/docs/{f.relative_to(DOCS_DIR).as_posix()}"
for f in DOCS_DIR.glob("apache-airflow-providers-*/operators.rst")
if not any(f.relative_to(DOCS_DIR).as_posix().startswith(s) for s in suspended_providers)
if not f.relative_to(DOCS_DIR).as_posix().startswith(tuple(suspended_providers))
}
console.print("[yellow]Suspended logos:[/]")
console.print(suspended_logos)
expected_logo_urls = {
f"/{f.relative_to(DOCS_DIR).as_posix()}"
for f in DOCS_DIR.glob("integration-logos/**/*")
if f.is_file()
and not any(f"/{f.relative_to(DOCS_DIR).as_posix()}".startswith(s) for s in suspended_logos)
if f.is_file() and not f"/{f.relative_to(DOCS_DIR).as_posix()}".startswith(tuple(suspended_logos))
}

try:
Expand Down
6 changes: 3 additions & 3 deletions scripts/in_container/verify_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ def mk_prefix(provider_id):
return f"{prefix}{provider_id}"

if provider_ids:
provider_prefixes = [mk_prefix(provider_id) for provider_id in provider_ids]
provider_prefixes = tuple(mk_prefix(provider_id) for provider_id in provider_ids)
else:
provider_prefixes = [prefix]
provider_prefixes = (prefix,)

def onerror(_):
nonlocal tracebacks
Expand All @@ -187,7 +187,7 @@ def onerror(_):

for path, prefix in walkable_paths_and_prefixes.items():
for modinfo in pkgutil.walk_packages(path=[path], prefix=prefix, onerror=onerror):
if not modinfo.name.startswith(tuple(provider_prefixes)):
if not modinfo.name.startswith(provider_prefixes):
if print_skips:
console.print(f"Skipping module: {modinfo.name}")
continue
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ def is_package_excluded(package: str, exclusion_list: list[str]) -> bool:
:param exclusion_list: list of excluded packages
:return: true if package should be excluded
"""
return any(package.startswith(excluded_package) for excluded_package in exclusion_list)
return package.startswith(tuple(exclusion_list))


def remove_provider_limits(package: str) -> str:
Expand Down

0 comments on commit d747a79

Please sign in to comment.