Skip to content

Commit

Permalink
Refactor shorter defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
eumiro authored and potiuk committed Oct 21, 2023
1 parent a562174 commit 789e22b
Show file tree
Hide file tree
Showing 29 changed files with 40 additions and 50 deletions.
2 changes: 1 addition & 1 deletion airflow/api/common/trigger_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _trigger_dag(
if dag is None or dag_id not in dag_bag.dags:
raise DagNotFound(f"Dag id {dag_id} not found")

execution_date = execution_date if execution_date else timezone.utcnow()
execution_date = execution_date or timezone.utcnow()

if not timezone.is_localized(execution_date):
raise ValueError("The execution_date should be localized")
Expand Down
4 changes: 1 addition & 3 deletions airflow/api_connexion/endpoints/extra_link_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,5 @@ def get_extra_links(
all_extra_link_pairs = (
(link_name, task.get_extra_links(ti, link_name)) for link_name in task.extra_links
)
all_extra_links = {
link_name: link_url if link_url else None for link_name, link_url in sorted(all_extra_link_pairs)
}
all_extra_links = {link_name: link_url or None for link_name, link_url in sorted(all_extra_link_pairs)}
return all_extra_links
2 changes: 1 addition & 1 deletion airflow/cli/commands/info_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class Architecture(Enum):
def get_current() -> Architecture:
"""Get architecture."""
current_architecture = _MACHINE_TO_ARCHITECTURE.get(platform.machine().lower())
return current_architecture if current_architecture else Architecture.UNKNOWN
return current_architecture or Architecture.UNKNOWN


_MACHINE_TO_ARCHITECTURE: dict[str, Architecture] = {
Expand Down
2 changes: 1 addition & 1 deletion airflow/jobs/scheduler_job_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ def _run_scheduler_loop(self) -> None:
# If the scheduler is doing things, don't sleep. This means when there is work to do, the
# scheduler will run "as quick as possible", but when it's stopped, it can sleep, dropping CPU
# usage when "idle"
time.sleep(min(self._scheduler_idle_sleep_time, next_event if next_event else 0))
time.sleep(min(self._scheduler_idle_sleep_time, next_event or 0))

if loop_count >= self.num_runs > 0:
self.log.info(
Expand Down
2 changes: 1 addition & 1 deletion airflow/models/dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ def __init__(
if tags and any(len(tag) > TAG_MAX_LEN for tag in tags):
raise AirflowException(f"tag cannot be longer than {TAG_MAX_LEN} characters")

self.owner_links = owner_links if owner_links else {}
self.owner_links = owner_links or {}
self.user_defined_macros = user_defined_macros
self.user_defined_filters = user_defined_filters
if default_args and not isinstance(default_args, dict):
Expand Down
2 changes: 1 addition & 1 deletion airflow/models/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def resolve(self, value: Any = NOTSET, suppress_exception: bool = False) -> Any:

if value is not NOTSET:
self._warn_if_not_json(value)
final_val = value if value is not NOTSET else self.value
final_val = self.value if value is NOTSET else value
if isinstance(final_val, ArgNotSet):
if suppress_exception:
return None
Expand Down
2 changes: 1 addition & 1 deletion airflow/triggers/external_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(
self.execution_dates = execution_dates
self.poll_interval = poll_interval
self.trigger_start_time = trigger_start_time
self.states = states if states else [TaskInstanceState.SUCCESS.value]
self.states = states or [TaskInstanceState.SUCCESS.value]
self._timeout_sec = 60

def serialize(self) -> tuple[str, dict[str, typing.Any]]:
Expand Down
2 changes: 1 addition & 1 deletion airflow/utils/deprecation_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ def add_deprecated_classes(
imports,
full_module_name,
override_deprecated_classes_for_module,
extra_message if extra_message else "",
extra_message or "",
)
sys.modules.setdefault(full_module_name, module_type)
16 changes: 8 additions & 8 deletions airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ def _iter_parsed_moved_data_table_names():
dashboard_alerts=dashboard_alerts,
migration_moved_data_alerts=sorted(set(_iter_parsed_moved_data_table_names())),
current_page=current_page,
search_query=arg_search_query if arg_search_query else "",
search_query=arg_search_query or "",
page_title=Markup(page_title) if page_title_has_markup else page_title,
page_size=dags_per_page,
num_of_pages=num_of_pages,
Expand All @@ -1017,10 +1017,10 @@ def _iter_parsed_moved_data_table_names():
current_page,
num_of_pages,
search=escape(arg_search_query) if arg_search_query else None,
status=arg_status_filter if arg_status_filter else None,
tags=arg_tags_filter if arg_tags_filter else None,
sorting_key=arg_sorting_key if arg_sorting_key else None,
sorting_direction=arg_sorting_direction if arg_sorting_direction else None,
status=arg_status_filter or None,
tags=arg_tags_filter or None,
sorting_key=arg_sorting_key or None,
sorting_direction=arg_sorting_direction or None,
),
num_runs=num_runs,
tags=tags,
Expand Down Expand Up @@ -2044,7 +2044,7 @@ def trigger(self, dag_id: str, session: Session = NEW_SESSION):
dag=dag,
dag_id=dag_id,
origin=origin,
conf=request_conf if request_conf else {},
conf=request_conf or {},
form=form,
is_dag_run_conf_overrides_params=is_dag_run_conf_overrides_params,
recent_confs=recent_confs,
Expand Down Expand Up @@ -3810,8 +3810,8 @@ def audit_log(self, dag_id: str, session: Session = NEW_SESSION):
paging=wwwutils.generate_pages(
current_page,
num_of_pages,
sorting_key=arg_sorting_key if arg_sorting_key else None,
sorting_direction=arg_sorting_direction if arg_sorting_direction else None,
sorting_key=arg_sorting_key or None,
sorting_direction=arg_sorting_direction or None,
),
sorting_key=arg_sorting_key,
sorting_direction=arg_sorting_direction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ def update_expected_environment_variables(env: dict[str, str]) -> None:
set_value_to_default_if_not_set(env, "AIRFLOW_CONSTRAINTS_REFERENCE", "constraints-source-providers")
set_value_to_default_if_not_set(env, "AIRFLOW_EXTRAS", "")
set_value_to_default_if_not_set(env, "AIRFLOW_ENABLE_AIP_44", "true")
set_value_to_default_if_not_set(env, "ANSWER", answer if answer is not None else "")
set_value_to_default_if_not_set(env, "ANSWER", answer or "")
set_value_to_default_if_not_set(env, "BASE_BRANCH", "main")
set_value_to_default_if_not_set(env, "BREEZE", "true")
set_value_to_default_if_not_set(env, "BREEZE_INIT_COMMAND", "")
Expand Down
2 changes: 1 addition & 1 deletion helm_tests/airflow_core/test_dag_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ def test_revision_history_limit(self, revision_history_limit, global_revision_hi
values=values,
show_only=["templates/dag-processor/dag-processor-deployment.yaml"],
)
expected_result = revision_history_limit if revision_history_limit else global_revision_history_limit
expected_result = revision_history_limit or global_revision_history_limit
assert jmespath.search("spec.revisionHistoryLimit", docs[0]) == expected_result

@pytest.mark.parametrize("command", [None, ["custom", "command"]])
Expand Down
2 changes: 1 addition & 1 deletion helm_tests/airflow_core/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def test_revision_history_limit(self, revision_history_limit, global_revision_hi
values=values,
show_only=["templates/scheduler/scheduler-deployment.yaml"],
)
expected_result = revision_history_limit if revision_history_limit else global_revision_history_limit
expected_result = revision_history_limit or global_revision_history_limit
assert jmespath.search("spec.revisionHistoryLimit", docs[0]) == expected_result

def test_should_create_valid_affinity_tolerations_and_node_selector(self):
Expand Down
2 changes: 1 addition & 1 deletion helm_tests/airflow_core/test_triggerer.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_revision_history_limit(self, revision_history_limit, global_revision_hi
values=values,
show_only=["templates/triggerer/triggerer-deployment.yaml"],
)
expected_result = revision_history_limit if revision_history_limit else global_revision_history_limit
expected_result = revision_history_limit or global_revision_history_limit
assert jmespath.search("spec.revisionHistoryLimit", docs[0]) == expected_result

def test_disable_wait_for_migration(self):
Expand Down
2 changes: 1 addition & 1 deletion helm_tests/airflow_core/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_revision_history_limit(self, revision_history_limit, global_revision_hi
values=values,
show_only=["templates/workers/worker-deployment.yaml"],
)
expected_result = revision_history_limit if revision_history_limit else global_revision_history_limit
expected_result = revision_history_limit or global_revision_history_limit
assert jmespath.search("spec.revisionHistoryLimit", docs[0]) == expected_result

def test_should_add_extra_containers(self):
Expand Down
2 changes: 1 addition & 1 deletion helm_tests/other/test_flower.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_revision_history_limit(self, revision_history_limit, global_revision_hi
values=values,
show_only=["templates/flower/flower-deployment.yaml"],
)
expected_result = revision_history_limit if revision_history_limit else global_revision_history_limit
expected_result = revision_history_limit or global_revision_history_limit
assert jmespath.search("spec.revisionHistoryLimit", docs[0]) == expected_result

@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion helm_tests/other/test_pgbouncer.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_revision_history_limit(self, revision_history_limit, global_revision_hi
values=values,
show_only=["templates/pgbouncer/pgbouncer-deployment.yaml"],
)
expected_result = revision_history_limit if revision_history_limit else global_revision_history_limit
expected_result = revision_history_limit or global_revision_history_limit
assert jmespath.search("spec.revisionHistoryLimit", docs[0]) == expected_result

def test_scheduler_name(self):
Expand Down
2 changes: 1 addition & 1 deletion helm_tests/other/test_statsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_revision_history_limit(self, revision_history_limit, global_revision_hi
values=values,
show_only=["templates/statsd/statsd-deployment.yaml"],
)
expected_result = revision_history_limit if revision_history_limit else global_revision_history_limit
expected_result = revision_history_limit or global_revision_history_limit
assert jmespath.search("spec.revisionHistoryLimit", docs[0]) == expected_result

def test_scheduler_name(self):
Expand Down
2 changes: 1 addition & 1 deletion kubernetes_tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _num_pods_in_namespace(namespace):

@staticmethod
def _delete_airflow_pod(name=""):
suffix = "-" + name if name else ""
suffix = f"-{name}" if name else ""
air_pod = check_output(["kubectl", "get", "pods"]).decode()
air_pod = air_pod.splitlines()
names = [re2.compile(r"\s+").split(x)[0] for x in air_pod if "airflow" + suffix in x]
Expand Down
2 changes: 1 addition & 1 deletion kubernetes_tests/test_kubernetes_pod_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def create_context(task) -> Context:
@pytest.fixture(scope="session")
def kubeconfig_path():
kubeconfig_path = os.environ.get("KUBECONFIG")
return kubeconfig_path if kubeconfig_path else os.path.expanduser("~/.kube/config")
return kubeconfig_path or os.path.expanduser("~/.kube/config")


@pytest.fixture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,7 @@ def check_if_different_provider_used(file_path: Path) -> None:
set(ALL_DEPENDENCIES[key]["cross-providers-deps"])
)
excluded_versions = ALL_PROVIDERS[key].get("excluded-python-versions")
unique_sorted_dependencies[key]["excluded-python-versions"] = (
excluded_versions if excluded_versions else []
)
unique_sorted_dependencies[key]["excluded-python-versions"] = excluded_versions or []
if errors:
console.print()
console.print("[red]Errors found during verification. Exiting!")
Expand Down
4 changes: 1 addition & 3 deletions tests/models/test_dagrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ def create_dag_run(
session: Session,
):
now = timezone.utcnow()
if execution_date is None:
execution_date = now
execution_date = pendulum.instance(execution_date)
execution_date = pendulum.instance(execution_date or now)
if is_backfill:
run_type = DagRunType.BACKFILL_JOB
data_interval = dag.infer_automated_data_interval(execution_date)
Expand Down
6 changes: 2 additions & 4 deletions tests/operators/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,15 +832,13 @@ def f(exit_code):

if expected_state == TaskInstanceState.FAILED:
with pytest.raises(CalledProcessError):
self.run_as_task(
f, op_kwargs={"exit_code": actual_exit_code}, **(extra_kwargs if extra_kwargs else {})
)
self.run_as_task(f, op_kwargs={"exit_code": actual_exit_code}, **(extra_kwargs or {}))
else:
ti = self.run_as_task(
f,
return_ti=True,
op_kwargs={"exit_code": actual_exit_code},
**(extra_kwargs if extra_kwargs else {}),
**(extra_kwargs or {}),
)
assert ti.state == expected_state

Expand Down
2 changes: 1 addition & 1 deletion tests/providers/amazon/aws/system/utils/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class TestAmazonSystemTestHelpers:
def test_fetch_variable_success(
self, mock_getenv, env_value, ssm_value, default_value, expected_result
) -> None:
mock_getenv.return_value = env_value if env_value else ssm_value
mock_getenv.return_value = env_value or ssm_value

result = utils.fetch_variable(ANY, default_value) if default_value else utils.fetch_variable(ANY_STR)

Expand Down
6 changes: 2 additions & 4 deletions tests/providers/cncf/kubernetes/operators/test_pod.py
Original file line number Diff line number Diff line change
Expand Up @@ -1299,9 +1299,7 @@ def test_task_skip_when_pod_exit_with_certain_code(
self, remote_pod, extra_kwargs, actual_exit_code, expected_exc
):
"""Tests that an AirflowSkipException is raised when the container exits with the skip_on_exit_code"""
k = KubernetesPodOperator(
task_id="task", on_finish_action="delete_pod", **(extra_kwargs if extra_kwargs else {})
)
k = KubernetesPodOperator(task_id="task", on_finish_action="delete_pod", **(extra_kwargs or {}))

base_container = MagicMock()
base_container.name = k.base_container_name
Expand Down Expand Up @@ -1576,7 +1574,7 @@ def test_async_create_pod_with_skip_on_exit_code_should_skip(
in_cluster=True,
get_logs=True,
deferrable=True,
**(extra_kwargs if extra_kwargs else {}),
**(extra_kwargs or {}),
)

base_container = MagicMock()
Expand Down
2 changes: 1 addition & 1 deletion tests/providers/dbt/cloud/operators/test_dbt_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ def test_run_job_operator_link(self, conn_id, account_id, create_task_instance_o

assert url == (
EXPECTED_JOB_RUN_OP_EXTRA_LINK.format(
account_id=account_id if account_id else DEFAULT_ACCOUNT_ID,
account_id=account_id or DEFAULT_ACCOUNT_ID,
project_id=PROJECT_ID,
run_id=_run_response["data"]["id"],
)
Expand Down
2 changes: 1 addition & 1 deletion tests/providers/docker/decorators/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def do_run():
],
)
def test_skip_docker_operator(self, extra_kwargs, actual_exit_code, expected_state, dag_maker):
@task.docker(image="python:3.9-slim", auto_remove="force", **(extra_kwargs if extra_kwargs else {}))
@task.docker(image="python:3.9-slim", auto_remove="force", **(extra_kwargs or {}))
def f(exit_code):
raise SystemExit(exit_code)

Expand Down
4 changes: 2 additions & 2 deletions tests/providers/jenkins/sensors/test_jenkins.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class TestJenkinsBuildSensor:
)
@patch("jenkins.Jenkins")
def test_poke_buliding(self, mock_jenkins, build_number, build_state, result):
target_build_number = build_number if build_number else 10
target_build_number = build_number or 10

jenkins_mock = MagicMock()
jenkins_mock.get_job_info.return_value = {"lastBuild": {"number": target_build_number}}
Expand Down Expand Up @@ -91,7 +91,7 @@ def test_poke_buliding(self, mock_jenkins, build_number, build_state, result):
def test_poke_finish_building(
self, mock_jenkins, build_number, build_state, result, soft_fail, expected_exception
):
target_build_number = build_number if build_number else 10
target_build_number = build_number or 10

jenkins_mock = MagicMock()
jenkins_mock.get_job_info.return_value = {"lastBuild": {"number": target_build_number}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ def test_run_pipeline_operator_link(self, resource_group, factory, create_task_i
EXPECTED_PIPELINE_RUN_OP_EXTRA_LINK.format(
run_id=PIPELINE_RUN_RESPONSE["run_id"],
subscription_id=SUBSCRIPTION_ID,
resource_group_name=resource_group if resource_group else conn_resource_group_name,
factory_name=factory if factory else conn_factory_name,
resource_group_name=resource_group or conn_resource_group_name,
factory_name=factory or conn_factory_name,
)
)

Expand Down
2 changes: 1 addition & 1 deletion tests/system/providers/amazon/aws/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def _fetch_from_ssm(key: str, test_name: str | None = None) -> str:
:param key: The key to search for within the returned Parameter Value.
:return: The value of the provided key from SSM
"""
_test_name: str = test_name if test_name else _get_test_name()
_test_name: str = test_name or _get_test_name()
hook = SsmHook(aws_conn_id=None)
value: str = ""

Expand Down

0 comments on commit 789e22b

Please sign in to comment.