Skip to content

Commit 674e39f

Browse files
authored
[chore] Rewrite multiline list comprehensions as for-loops and enforce it as a linter rule (#2820)
Improve readability of parts of the codebase
1 parent 352126d commit 674e39f

File tree

21 files changed

+143
-151
lines changed

21 files changed

+143
-151
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ dependencies = [
7676
"mypy~=1.9.0",
7777
"pylint~=3.3.1",
7878
"pylint-pytest==2.0.0a0",
79-
"databricks-labs-pylint~=0.4.0",
79+
"databricks-labs-pylint~=0.5",
8080
"databricks-labs-pytester>=0.2.1",
8181
"pytest~=8.3.3",
8282
"pytest-cov~=4.1.0",
@@ -241,6 +241,7 @@ limit-inference-results = 100
241241
load-plugins = [
242242
"databricks.labs.pylint.mocking",
243243
"databricks.labs.pylint.eradicate",
244+
"databricks.labs.pylint.readability",
244245
"pylint_pytest",
245246
"pylint.extensions.bad_builtin",
246247
"pylint.extensions.broad_try_clause",

src/databricks/labs/ucx/cli.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,14 @@ def revert_cluster_remap(w: WorkspaceClient, prompts: Prompts):
550550
"""Reverting Re-mapping of clusters from UC"""
551551
logger.info("Reverting the Remapping of the Clusters from UC")
552552
ctx = WorkspaceContext(w)
553-
cluster_ids = [
554-
cluster_files.path.split("/")[-1].split(".")[0]
555-
for cluster_files in ctx.installation.files()
556-
if cluster_files.path is not None and cluster_files.path.find("backup/clusters") > 0
557-
]
553+
cluster_ids = []
554+
for cluster_files in ctx.installation.files():
555+
if cluster_files.path is None:
556+
continue
557+
if cluster_files.path.find("backup/clusters") == 0:
558+
continue
559+
cluster_id = cluster_files.path.split("/")[-1].split(".")[0]
560+
cluster_ids.append(cluster_id)
558561
if not cluster_ids:
559562
logger.info("There is no cluster files in the backup folder. Skipping the reverting process")
560563
return

src/databricks/labs/ucx/hive_metastore/grants.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -623,9 +623,12 @@ def _get_privilege(self, table: Table, locations: dict[str, str], mounts: list[M
623623

624624
def _get_database_grants(self, tables: list[Table], principals: list[str]) -> list[Grant]:
625625
databases = {table.database for table in tables}
626-
return [
627-
Grant(principal, "USAGE", "hive_metastore", database) for database in databases for principal in principals
628-
]
626+
grants = []
627+
for database in databases:
628+
for principal in principals:
629+
grant = Grant(principal, "USAGE", "hive_metastore", database)
630+
grants.append(grant)
631+
return grants
629632

630633
def _get_grants(
631634
self, locations: dict[str, str], principals: list[str], tables: list[Table], mounts: list[Mount]
@@ -635,33 +638,21 @@ def _get_grants(
635638
for table in tables:
636639
privilege = self._get_privilege(table, locations, mounts)
637640
if privilege == "READ_FILES":
638-
grants.extend(
639-
[Grant(principal, "SELECT", table.catalog, table.database, table.name) for principal in principals]
640-
)
641+
for principal in principals:
642+
grants.append(Grant(principal, "SELECT", table.catalog, table.database, table.name))
641643
filtered_tables.append(table)
642644
continue
643645
if privilege == "WRITE_FILES":
644-
grants.extend(
645-
[
646-
Grant(principal, "ALL PRIVILEGES", table.catalog, table.database, table.name)
647-
for principal in principals
648-
]
649-
)
646+
for principal in principals:
647+
grants.append(Grant(principal, "ALL PRIVILEGES", table.catalog, table.database, table.name))
650648
filtered_tables.append(table)
651649
continue
652650
if table.view_text is not None:
653-
grants.extend(
654-
[
655-
Grant(principal, "ALL PRIVILEGES", table.catalog, table.database, view=table.name)
656-
for principal in principals
657-
]
658-
)
651+
for principal in principals:
652+
grants.append(Grant(principal, "ALL PRIVILEGES", table.catalog, table.database, view=table.name))
659653
filtered_tables.append(table)
660-
661654
database_grants = self._get_database_grants(filtered_tables, principals)
662-
663655
grants.extend(database_grants)
664-
665656
return grants
666657

667658
def _get_cluster_principal_mapping(self, cluster_id: str, object_type: str) -> list[str]:

src/databricks/labs/ucx/installer/logs.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,11 @@ def _record_task(self, task_name: str, log: TextIO, log_creation_timestamp: dt.d
139139
log (TextIO): The task log
140140
log_creation_timestamp (datetime.datetime): The log creation timestamp.
141141
"""
142-
log_records = [
143-
LogRecord(
142+
log_records = []
143+
for partial_log_record in parse_logs(log):
144+
if logging.getLevelName(partial_log_record.level) < logging.WARNING:
145+
continue
146+
log_record = LogRecord(
144147
timestamp=int(
145148
log_creation_timestamp.replace(
146149
hour=partial_log_record.time.hour,
@@ -156,9 +159,7 @@ def _record_task(self, task_name: str, log: TextIO, log_creation_timestamp: dt.d
156159
component=partial_log_record.component,
157160
message=partial_log_record.message,
158161
)
159-
for partial_log_record in parse_logs(log)
160-
if logging.getLevelName(partial_log_record.level) >= logging.WARNING
161-
]
162+
log_records.append(log_record)
162163
return log_records
163164

164165
def snapshot(self) -> list[LogRecord]:

src/databricks/labs/ucx/source_code/graph.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,12 @@ def register_dependency(self, dependency: Dependency) -> MaybeGraph:
9191
if not container:
9292
problem = DependencyProblem('dependency-register-failed', 'Failed to register dependency', dependency.path)
9393
return MaybeGraph(child_graph, [problem])
94-
problems = container.build_dependency_graph(child_graph)
95-
return MaybeGraph(
96-
child_graph,
97-
[
98-
dataclasses.replace(
99-
problem,
100-
source_path=dependency.path if problem.is_path_missing() else problem.source_path,
101-
)
102-
for problem in problems
103-
],
104-
)
94+
problems = []
95+
for problem in container.build_dependency_graph(child_graph):
96+
if problem.is_path_missing():
97+
problem = dataclasses.replace(problem, source_path=dependency.path)
98+
problems.append(problem)
99+
return MaybeGraph(child_graph, problems)
105100

106101
def locate_dependency(self, path: Path) -> MaybeGraph:
107102
# need a list since unlike JS, Python won't let you assign closure variables

src/databricks/labs/ucx/source_code/notebooks/cells.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,17 @@ def is_runnable(self) -> bool:
103103
def build_dependency_graph(self, parent: DependencyGraph) -> list[DependencyProblem]:
104104
context = parent.new_dependency_graph_context()
105105
analyzer = PythonCodeAnalyzer(context, self._original_code)
106-
python_dependency_problems = analyzer.build_graph()
107-
# Position information for the Python code is within the code and needs to be mapped to the location within the parent nodebook.
108-
return [
109-
dataclasses.replace(
106+
# Position information for the Python code is within the code and needs to be mapped to the location within
107+
# the parent nodebook.
108+
problems = []
109+
for problem in analyzer.build_graph():
110+
problem = dataclasses.replace(
110111
problem,
111112
start_line=self.original_offset + problem.start_line,
112113
end_line=self.original_offset + problem.end_line,
113114
)
114-
for problem in python_dependency_problems
115-
]
115+
problems.append(problem)
116+
return problems
116117

117118
def build_inherited_context(self, graph: DependencyGraph, child_path: Path) -> InheritedContext:
118119
context = graph.new_dependency_graph_context()
@@ -178,11 +179,17 @@ def build_dependency_graph(self, parent: DependencyGraph) -> list[DependencyProb
178179
path, idx, line = self._read_notebook_path()
179180
if path is not None:
180181
start_line = self._original_offset + idx
181-
problems = parent.register_notebook(path, True)
182-
return [
183-
dataclasses.replace(problem, start_line=start_line, start_col=0, end_line=start_line, end_col=len(line))
184-
for problem in problems
185-
]
182+
problems = []
183+
for problem in parent.register_notebook(path, True):
184+
problem = dataclasses.replace(
185+
problem,
186+
start_line=start_line,
187+
start_col=0,
188+
end_line=start_line,
189+
end_col=len(line),
190+
)
191+
problems.append(problem)
192+
return problems
186193
start_line = self._original_offset
187194
problem = DependencyProblem(
188195
'invalid-run-cell',

src/databricks/labs/ucx/source_code/queries.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,15 @@ def refresh_report(self, sql_backend: SqlBackend, inventory_database: str):
7878
)
7979
# dump dfsas
8080
assessment_end = datetime.now(timezone.utc)
81-
all_dfsas = [
82-
dataclasses.replace(
83-
dfsa, assessment_start_timestamp=assessment_start, assessment_end_timestamp=assessment_end
81+
processed = []
82+
for dfsa in all_dfsas:
83+
dfsa = dataclasses.replace(
84+
dfsa,
85+
assessment_start_timestamp=assessment_start,
86+
assessment_end_timestamp=assessment_end,
8487
)
85-
for dfsa in all_dfsas
86-
]
87-
self._directfs_crawler.dump_all(all_dfsas)
88+
processed.append(dfsa)
89+
self._directfs_crawler.dump_all(processed)
8890

8991
def _dashboard_ids_in_scope(self) -> list[str]:
9092
if self._include_dashboard_ids is not None: # an empty list is accepted

src/databricks/labs/ucx/workspace_access/clusters.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ def __init__(self, installation: Installation, ws: WorkspaceClient, prompts: Pro
1616
self._installation = installation
1717

1818
def list_cluster(self):
19-
clusters = [
20-
clusters
21-
for clusters in self._ws.clusters.list()
22-
if clusters.cluster_source is not None and clusters.cluster_source.name != "JOB"
23-
]
19+
clusters = []
20+
for cluster_info in self._ws.clusters.list():
21+
if cluster_info.cluster_source is None:
22+
continue
23+
if cluster_info.cluster_source.name == "JOB":
24+
continue
25+
clusters.append(cluster_info)
2426
return clusters
2527

2628
def _get_access_mode(self, access_mode: str):

src/databricks/labs/ucx/workspace_access/generic.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,16 +414,21 @@ def inner() -> Iterator[ml.ModelDatabricks]:
414414

415415

416416
def experiments_listing(ws: WorkspaceClient):
417+
def _get_repo_nb_tag(experiment):
418+
repo_nb_tag = []
419+
for tag in experiment.tags:
420+
if tag.key == "mlflow.experiment.sourceType" and tag.value == "REPO_NOTEBOOK":
421+
repo_nb_tag.append(tag)
422+
return repo_nb_tag
423+
417424
def inner() -> Iterator[ml.Experiment]:
418425
for experiment in ws.experiments.list_experiments():
419426
# We filter-out notebook-based experiments, because they are covered by notebooks listing in
420427
# workspace-based notebook experiment
421428
if experiment.tags:
422429
nb_tag = [t for t in experiment.tags if t.key == "mlflow.experimentType" and t.value == "NOTEBOOK"]
423430
# repo-based notebook experiment
424-
repo_nb_tag = [
425-
t for t in experiment.tags if t.key == "mlflow.experiment.sourceType" and t.value == "REPO_NOTEBOOK"
426-
]
431+
repo_nb_tag = _get_repo_nb_tag(experiment)
427432
if nb_tag or repo_nb_tag:
428433
continue
429434

src/databricks/labs/ucx/workspace_access/groups.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -857,11 +857,10 @@ def _check_for_deleted_workspace_groups(
857857
) -> None:
858858
attributes = "id,displayName"
859859
expected_deletions = {group.id_in_workspace for group in deleted_workspace_groups}
860-
pending_deletions = [
861-
GroupDeletionIncompleteError(group.id, group.display_name)
862-
for group in self._list_workspace_groups("WorkspaceGroup", attributes)
863-
if group.id in expected_deletions
864-
]
860+
pending_deletions = []
861+
for group in self._list_workspace_groups("WorkspaceGroup", attributes):
862+
if group.id in expected_deletions:
863+
pending_deletions.append(GroupDeletionIncompleteError(group.id, group.display_name))
865864
if pending_deletions:
866865
if logger.isEnabledFor(still_present_log_level):
867866
logger.log(

0 commit comments

Comments
 (0)