From d06b6cbcbffa5b6545e0a86abf71252c9ad69b89 Mon Sep 17 00:00:00 2001 From: Serge Smertin Date: Mon, 21 Oct 2024 14:12:47 +0200 Subject: [PATCH] Preemptively fix unknown errors of Python AST parsing coming from `astroid` and `ast` libraries This PR casts a wider net over exception catching in `Tree.maybe_parse(sources)` method. --- src/databricks/labs/ucx/source_code/python/python_ast.py | 7 ++----- tests/unit/source_code/notebooks/test_cells.py | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/databricks/labs/ucx/source_code/python/python_ast.py b/src/databricks/labs/ucx/source_code/python/python_ast.py index 15ee7574c1..be860d8fed 100644 --- a/src/databricks/labs/ucx/source_code/python/python_ast.py +++ b/src/databricks/labs/ucx/source_code/python/python_ast.py @@ -25,7 +25,6 @@ NodeNG, parse, Uninferable, - AstroidSyntaxError, ) from databricks.labs.ucx.source_code.base import ( @@ -77,9 +76,7 @@ def maybe_parse(cls, code: str) -> MaybeTree: root = parse(code) tree = Tree(root) return MaybeTree(tree, None) - except AstroidSyntaxError as e: - return cls._definitely_failure('syntax-error', code, e) - except SystemError as e: + except Exception as e: # pylint: disable=broad-exception-caught # see https://github.com/databrickslabs/ucx/issues/2976 return cls._definitely_failure('system-error', code, e) @@ -89,7 +86,7 @@ def _definitely_failure(message_code: str, source_code: str, e: Exception) -> Ma None, Failure( code=message_code, - message=f"Failed to parse code `{source_code}`: {e}. Report this as an issue on UCX GitHub.", + message=f"Failed to parse code `{source_code}`: {type(e)}: {e}. Report this as an issue on UCX GitHub.", # Lines and columns are both 0-based: the first line is line 0. start_line=0, start_col=0, diff --git a/tests/unit/source_code/notebooks/test_cells.py b/tests/unit/source_code/notebooks/test_cells.py index abbc871410..bffca0540a 100644 --- a/tests/unit/source_code/notebooks/test_cells.py +++ b/tests/unit/source_code/notebooks/test_cells.py @@ -195,7 +195,7 @@ def test_graph_builder_parse_error( problems = analyser.build_graph() codes = {_.code for _ in problems} - assert codes == {'syntax-error'} + assert codes == {'system-error'} def test_parses_python_cell_with_magic_commands(simple_dependency_resolver, mock_path_lookup) -> None: