Skip to content

Commit a94fdf1

Browse files
authored
Tech debt: fix situations where next() isn't being used properly (#2885)
## Changes This PR updates/fixes the way Python's builtin [`next()`](https://docs.python.org/3/library/functions.html#next) function is used. Specifically, in a few places the code assumed that `None` will be returned if there is no next value but this is incorrect.
1 parent 4303926 commit a94fdf1

File tree

4 files changed

+7
-8
lines changed

4 files changed

+7
-8
lines changed

src/databricks/labs/ucx/source_code/linters/imports.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def _raise_advice_if_unresolved(cls, node: NodeNG, session_state: CurrentSession
139139
def get_dbutils_notebook_run_path_arg(node: Call):
140140
if len(node.args) > 0:
141141
return node.args[0]
142-
arg = next(kw for kw in node.keywords if kw.arg == "path")
142+
arg = next((kw for kw in node.keywords if kw.arg == "path"), None)
143143
return arg.value if arg is not None else None
144144

145145
@staticmethod

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,11 @@ def requires_isolated_pi(self) -> str:
289289

290290
@classmethod
291291
def of_language(cls, language: Language) -> CellLanguage:
292-
# TODO: Should this not raise a ValueError if the language is not found?
293-
# It also causes a GeneratorExit exception to be raised. Maybe an explicit loop is better.
294-
return next((cl for cl in CellLanguage if cl.language == language))
292+
for cell_language in CellLanguage:
293+
if cell_language.language == language:
294+
return cell_language
295+
msg = f"Unsupported cell language: {language}"
296+
raise ValueError(msg)
295297

296298
@classmethod
297299
def of_magic_name(cls, magic_name: str) -> CellLanguage | None:

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ def _is_egg_folder(path: Path) -> bool:
8787
and any(subfolder.name.lower() == "egg-info" for subfolder in path.iterdir())
8888
)
8989

90-
def has_path(self, path: Path):
91-
return next(p for p in self._sys_paths if path == p) is not None
92-
9390
def prepend_path(self, path: Path):
9491
self._sys_paths.insert(0, path)
9592

tests/unit/hive_metastore/test_table_migrate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ def test_migrate_views_should_be_properly_sequenced(ws):
10281028
table_keys = [task.args[0].src.key for task in tasks]
10291029
assert table_keys.index("hive_metastore.db1_src.v1_src") > table_keys.index("hive_metastore.db1_src.v3_src")
10301030
assert table_keys.index("hive_metastore.db1_src.v3_src") > table_keys.index("hive_metastore.db1_src.v2_src")
1031-
assert next((key for key in table_keys if key == "hive_metastore.db1_src.t1_src"), None) is None
1031+
assert not any(key for key in table_keys if key == "hive_metastore.db1_src.t1_src")
10321032

10331033

10341034
def test_table_in_mount_mapping_with_table_owner():

0 commit comments

Comments
 (0)