Skip to content

Commit

Permalink
Use str.splitlines() to split lines (#33592)
Browse files Browse the repository at this point in the history
  • Loading branch information
eumiro authored Aug 24, 2023
1 parent ae56cae commit 0e00564
Show file tree
Hide file tree
Showing 15 changed files with 41 additions and 53 deletions.
2 changes: 1 addition & 1 deletion airflow/cli/commands/standalone_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def print_output(self, name: str, output):
"standalone": "white",
}.get(name, "white")
colorised_name = colored("%10s" % name, color)
for line in output.split("\n"):
for line in output.splitlines():
print(f"{colorised_name} | {line.strip()}")

def print_error(self, name: str, output):
Expand Down
28 changes: 13 additions & 15 deletions airflow/utils/code_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import functools
import inspect
import os
from pathlib import Path
from typing import Any


Expand Down Expand Up @@ -61,21 +62,18 @@ def prepare_code_snippet(file_path: str, line_no: int, context_lines_count: int
:param context_lines_count: The number of lines that will be cut before and after.
:return: str
"""
with open(file_path) as text_file:
# Highlight code
code = text_file.read()
code_lines = code.split("\n")
# Prepend line number
code_lines = [
f">{lno:3} | {line}" if line_no == lno else f"{lno:4} | {line}"
for lno, line in enumerate(code_lines, 1)
]
# # Cut out the snippet
start_line_no = max(0, line_no - context_lines_count - 1)
end_line_no = line_no + context_lines_count
code_lines = code_lines[start_line_no:end_line_no]
# Join lines
code = "\n".join(code_lines)
code_lines = Path(file_path).read_text().splitlines()
# Prepend line number
code_lines = [
f">{lno:3} | {line}" if line_no == lno else f"{lno:4} | {line}"
for lno, line in enumerate(code_lines, 1)
]
# # Cut out the snippet
start_line_no = max(0, line_no - context_lines_count - 1)
end_line_no = line_no + context_lines_count
code_lines = code_lines[start_line_no:end_line_no]
# Join lines
code = "\n".join(code_lines)
return code


Expand Down
2 changes: 1 addition & 1 deletion dev/assign_cherry_picked_prs_with_milestone.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def get_changes(verbose: bool, previous_release: str, current_release: str) -> l
cwd=SOURCE_DIR_PATH,
text=True,
)
return [get_change_from_line(line) for line in change_strings.split("\n")]
return [get_change_from_line(line) for line in change_strings.splitlines()]


def update_milestone(r: Repository, pr: PullRequest, m: Milestone):
Expand Down
2 changes: 1 addition & 1 deletion dev/check_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def get_packages() -> list[tuple[str, str]]:
# e.g. https://pypi.org/project/apache-airflow-providers-airbyte/3.1.0rc1/

packages = []
for line in content.split("\n"):
for line in content.splitlines():
if not line:
continue
name, version = line.rstrip("/").split("/")[-2:]
Expand Down
4 changes: 2 additions & 2 deletions dev/prepare_release_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def get_changes(
cwd=SOURCE_DIR_PATH,
text=True,
)
return [get_change_from_line(line) for line in change_strings.split("\n")]
return [get_change_from_line(line) for line in change_strings.splitlines()]


def render_template(
Expand Down Expand Up @@ -300,7 +300,7 @@ def generate_issue_content(
# GitHub does not have linked issues in PR - but we quite rigorously add Fixes/Closes
# Relate so we can find those from the body
if pr.body:
body = pr.body.replace("\n", " ").replace("\r", " ")
body = " ".join(pr.body.splitlines())
linked_issue_numbers = {
int(issue_match.group(1)) for issue_match in ISSUE_MATCH_IN_BODY.finditer(body)
}
Expand Down
2 changes: 1 addition & 1 deletion dev/provider_packages/prepare_provider_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ def convert_git_changes_to_table(
"""
from tabulate import tabulate

lines = changes.split("\n")
lines = changes.splitlines()
headers = ["Commit", "Committed", "Subject"]
table_data = []
changes_list: list[Change] = []
Expand Down
2 changes: 1 addition & 1 deletion docs/exts/docs_build/code_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def guess_lexer_for_filename(filename):
code=code, formatter=TerminalFormatter(), lexer=guess_lexer_for_filename(file_path)
)

code_lines = code.split("\n")
code_lines = code.splitlines()
# Prepend line number
code_lines = [f"{line_no:4} | {line}" for line_no, line in enumerate(code_lines, 1)]
# # Cut out the snippet
Expand Down
2 changes: 1 addition & 1 deletion docs/exts/docs_build/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def parse_sphinx_warnings(warning_text: str, docs_dir: str) -> list[DocBuildErro
:return: list of DocBuildErrors.
"""
sphinx_build_errors = []
for sphinx_warning in warning_text.split("\n"):
for sphinx_warning in warning_text.splitlines():
if not sphinx_warning:
continue
warning_parts = sphinx_warning.split(":", 2)
Expand Down
2 changes: 1 addition & 1 deletion docs/exts/docs_build/spelling_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def parse_spelling_warnings(warning_text: str, docs_dir: str) -> list[SpellingEr
:return: list of SpellingError.
"""
sphinx_spelling_errors = []
for sphinx_warning in warning_text.split("\n"):
for sphinx_warning in warning_text.splitlines():
if not sphinx_warning:
continue
warning_parts = None
Expand Down
14 changes: 2 additions & 12 deletions helm_tests/airflow_aux/test_configmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,8 @@ def test_default_flower_url_prefix(self):
show_only=["templates/configmaps/configmap.yaml"],
)
expected = "flower_url_prefix = "
found = False
cfg = jmespath.search('data."airflow.cfg"', docs[0])
for item in cfg.split("\n"):
if item == expected:
found = True

assert found is True
assert expected in cfg.splitlines()

def test_overriden_flower_url_prefix(self):
docs = render_chart(
Expand All @@ -139,11 +134,6 @@ def test_overriden_flower_url_prefix(self):
)

expected = "flower_url_prefix = /overriden-path"
found = False

cfg = jmespath.search('data."airflow.cfg"', docs[0])
for item in cfg.split("\n"):
if item == expected:
found = True

assert found is True
assert expected in cfg.splitlines()
4 changes: 2 additions & 2 deletions kubernetes_tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ def _describe_resources(self, namespace: str):
@staticmethod
def _num_pods_in_namespace(namespace):
air_pod = check_output(["kubectl", "get", "pods", "-n", namespace]).decode()
air_pod = air_pod.split("\n")
air_pod = air_pod.splitlines()
names = [re2.compile(r"\s+").split(x)[0] for x in air_pod if "airflow" in x]
return len(names)

@staticmethod
def _delete_airflow_pod(name=""):
suffix = "-" + name if name else ""
air_pod = check_output(["kubectl", "get", "pods"]).decode()
air_pod = air_pod.split("\n")
air_pod = air_pod.splitlines()
names = [re2.compile(r"\s+").split(x)[0] for x in air_pod if "airflow" + suffix in x]
if names:
check_call(["kubectl", "delete", "pod", names[0]])
Expand Down
24 changes: 12 additions & 12 deletions tests/cli/commands/test_config_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_cli_show_config_should_only_show_comments_when_no_defaults(self):
with contextlib.redirect_stdout(io.StringIO()) as temp_stdout:
config_command.show_config(self.parser.parse_args(["config", "list", "--color", "off"]))
output = temp_stdout.getvalue()
lines = output.split("\n")
lines = output.splitlines()
assert all(not line.startswith("#") or line.endswith("= ") for line in lines if line)

def test_cli_show_config_shows_descriptions(self):
Expand All @@ -87,7 +87,7 @@ def test_cli_show_config_shows_descriptions(self):
self.parser.parse_args(["config", "list", "--color", "off", "--include-descriptions"])
)
output = temp_stdout.getvalue()
lines = output.split("\n")
lines = output.splitlines()
# comes from metrics description
assert all(not line.startswith("# Source: ") for line in lines if line)
assert any(line.startswith("# StatsD") for line in lines if line)
Expand All @@ -100,7 +100,7 @@ def test_cli_show_config_shows_examples(self):
self.parser.parse_args(["config", "list", "--color", "off", "--include-examples"])
)
output = temp_stdout.getvalue()
lines = output.split("\n")
lines = output.splitlines()
assert all(not line.startswith("# Source: ") for line in lines if line)
assert all(not line.startswith("# StatsD") for line in lines if line)
assert any(line.startswith("# Example:") for line in lines if line)
Expand All @@ -112,7 +112,7 @@ def test_cli_show_config_shows_variables(self):
self.parser.parse_args(["config", "list", "--color", "off", "--include-env-vars"])
)
output = temp_stdout.getvalue()
lines = output.split("\n")
lines = output.splitlines()
assert all(not line.startswith("# Source: ") for line in lines if line)
assert all(not line.startswith("# StatsD") for line in lines if line)
assert all(not line.startswith("# Example:") for line in lines if line)
Expand All @@ -124,7 +124,7 @@ def test_cli_show_config_shows_sources(self):
self.parser.parse_args(["config", "list", "--color", "off", "--include-sources"])
)
output = temp_stdout.getvalue()
lines = output.split("\n")
lines = output.splitlines()
assert any(line.startswith("# Source: ") for line in lines if line)
assert all(not line.startswith("# StatsD") for line in lines if line)
assert all(not line.startswith("# Example:") for line in lines if line)
Expand All @@ -136,7 +136,7 @@ def test_cli_show_config_defaults(self):
self.parser.parse_args(["config", "list", "--color", "off", "--defaults"])
)
output = temp_stdout.getvalue()
lines = output.split("\n")
lines = output.splitlines()
assert all(not line.startswith("# Source: ") for line in lines if line)
assert any(line.startswith("# StatsD") for line in lines if line)
assert any(not line.startswith("# Example:") for line in lines if line)
Expand All @@ -151,7 +151,7 @@ def test_cli_show_config_defaults_not_show_conf_changes(self):
self.parser.parse_args(["config", "list", "--color", "off", "--defaults"])
)
output = temp_stdout.getvalue()
lines = output.split("\n")
lines = output.splitlines()
assert any(line.startswith("# task_runner = StandardTaskRunner") for line in lines if line)

@mock.patch("os.environ", {"AIRFLOW__CORE__TASK_RUNNER": "test-env-runner"})
Expand All @@ -161,30 +161,30 @@ def test_cli_show_config_defaults_do_not_show_env_changes(self):
self.parser.parse_args(["config", "list", "--color", "off", "--defaults"])
)
output = temp_stdout.getvalue()
lines = output.split("\n")
lines = output.splitlines()
assert any(line.startswith("# task_runner = StandardTaskRunner") for line in lines if line)

@conf_vars({("core", "task_runner"): "test-runner"})
def test_cli_show_changed_defaults_when_overridden_in_conf(self):
with contextlib.redirect_stdout(io.StringIO()) as temp_stdout:
config_command.show_config(self.parser.parse_args(["config", "list", "--color", "off"]))
output = temp_stdout.getvalue()
lines = output.split("\n")
lines = output.splitlines()
assert any(line.startswith("task_runner = test-runner") for line in lines if line)

@mock.patch("os.environ", {"AIRFLOW__CORE__TASK_RUNNER": "test-env-runner"})
def test_cli_show_changed_defaults_when_overridden_in_env(self):
with contextlib.redirect_stdout(io.StringIO()) as temp_stdout:
config_command.show_config(self.parser.parse_args(["config", "list", "--color", "off"]))
output = temp_stdout.getvalue()
lines = output.split("\n")
lines = output.splitlines()
assert any(line.startswith("task_runner = test-env-runner") for line in lines if line)

def test_cli_has_providers(self):
with contextlib.redirect_stdout(io.StringIO()) as temp_stdout:
config_command.show_config(self.parser.parse_args(["config", "list", "--color", "off"]))
output = temp_stdout.getvalue()
lines = output.split("\n")
lines = output.splitlines()
assert any(line.startswith("celery_config_options") for line in lines if line)

def test_cli_comment_out_everything(self):
Expand All @@ -193,7 +193,7 @@ def test_cli_comment_out_everything(self):
self.parser.parse_args(["config", "list", "--color", "off", "--comment-out-everything"])
)
output = temp_stdout.getvalue()
lines = output.split("\n")
lines = output.splitlines()
assert all(not line.strip() or line.startswith(("#", "[")) for line in lines if line)


Expand Down
2 changes: 1 addition & 1 deletion tests/cli/commands/test_dag_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ def test_trigger_dag_output_as_json(self):
with contextlib.redirect_stdout(io.StringIO()) as temp_stdout:
dag_command.dag_trigger(args)
# get the last line from the logs ignoring all logging lines
out = temp_stdout.getvalue().strip().split("\n")[-1]
out = temp_stdout.getvalue().strip().splitlines()[-1]
parsed_out = json.loads(out)

assert 1 == len(parsed_out)
Expand Down
2 changes: 1 addition & 1 deletion tests/models/test_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ def test_tree_view(self):
dag.tree_view()
stdout = stdout.getvalue()

stdout_lines = stdout.split("\n")
stdout_lines = stdout.splitlines()
assert "t1" in stdout_lines[0]
assert "t2" in stdout_lines[1]
assert "t3" in stdout_lines[2]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setup_method(self) -> None:
self.execute_cmd(["terraform", "apply", "-input=false", "-auto-approve", self.TERRAFORM_DIR])

def get_tf_output(self, name):
return self.check_output(["terraform", "output", name]).decode("utf-8").replace("\r\n", "")
return "".join(self.check_output(["terraform", "output", name]).decode("utf-8").splitlines())

def teardown_method(self) -> None:
self.execute_cmd(["terraform", "plan", "-destroy", "-input=false", self.TERRAFORM_DIR])
Expand Down

0 comments on commit 0e00564

Please sign in to comment.