-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more value inference for
dbutils.notebook.run(...)
(#1860)
- Loading branch information
1 parent
b19c848
commit 879a5b4
Showing
10 changed files
with
291 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import pytest | ||
from astroid import Attribute, Call, Const, Expr # type: ignore | ||
|
||
from databricks.labs.ucx.source_code.linters.python_ast import Tree | ||
|
||
|
||
def test_extract_call_by_name(): | ||
tree = Tree.parse("o.m1().m2().m3()") | ||
stmt = tree.first_statement() | ||
assert isinstance(stmt, Expr) | ||
assert isinstance(stmt.value, Call) | ||
act = Tree.extract_call_by_name(stmt.value, "m2") | ||
assert isinstance(act, Call) | ||
assert isinstance(act.func, Attribute) | ||
assert act.func.attrname == "m2" | ||
|
||
|
||
def test_extract_call_by_name_none(): | ||
tree = Tree.parse("o.m1().m2().m3()") | ||
stmt = tree.first_statement() | ||
assert isinstance(stmt, Expr) | ||
assert isinstance(stmt.value, Call) | ||
act = Tree.extract_call_by_name(stmt.value, "m5000") | ||
assert act is None | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"code, arg_index, arg_name, expected", | ||
[ | ||
("o.m1()", 1, "second", None), | ||
("o.m1(3)", 1, "second", None), | ||
("o.m1(first=3)", 1, "second", None), | ||
("o.m1(4, 3)", None, None, None), | ||
("o.m1(4, 3)", None, "second", None), | ||
("o.m1(4, 3)", 1, "second", 3), | ||
("o.m1(4, 3)", 1, None, 3), | ||
("o.m1(first=4, second=3)", 1, "second", 3), | ||
("o.m1(second=3, first=4)", 1, "second", 3), | ||
("o.m1(second=3, first=4)", None, "second", 3), | ||
("o.m1(second=3)", 1, "second", 3), | ||
("o.m1(4, 3, 2)", 1, "second", 3), | ||
], | ||
) | ||
def test_linter_gets_arg(code, arg_index, arg_name, expected): | ||
tree = Tree.parse(code) | ||
stmt = tree.first_statement() | ||
assert isinstance(stmt, Expr) | ||
assert isinstance(stmt.value, Call) | ||
act = Tree.get_arg(stmt.value, arg_index, arg_name) | ||
if expected is None: | ||
assert act is None | ||
else: | ||
assert isinstance(act, Const) | ||
assert act.value == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"code, expected", | ||
[ | ||
("o.m1()", 0), | ||
("o.m1(3)", 1), | ||
("o.m1(first=3)", 1), | ||
("o.m1(3, 3)", 2), | ||
("o.m1(first=3, second=3)", 2), | ||
("o.m1(3, second=3)", 2), | ||
("o.m1(3, *b, **c, second=3)", 4), | ||
], | ||
) | ||
def test_args_count(code, expected): | ||
tree = Tree.parse(code) | ||
stmt = tree.first_statement() | ||
assert isinstance(stmt, Expr) | ||
assert isinstance(stmt.value, Call) | ||
act = Tree.args_count(stmt.value) | ||
assert act == expected | ||
|
||
|
||
def test_tree_walks_nodes_once(): | ||
nodes = set() | ||
count = 0 | ||
tree = Tree.parse("o.m1().m2().m3()") | ||
for node in tree.walk(): | ||
nodes.add(node) | ||
count += 1 | ||
assert len(nodes) == count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from pathlib import Path | ||
|
||
from databricks.labs.ucx.source_code.notebooks.loaders import NotebookLoader | ||
from databricks.sdk.service.workspace import Language | ||
|
||
|
||
def test_detects_language(): | ||
|
||
class NotebookLoaderForTesting(NotebookLoader): | ||
|
||
@classmethod | ||
def detect_language(cls, path: Path, content: str): | ||
return cls._detect_language(path, content) | ||
|
||
assert NotebookLoaderForTesting.detect_language(Path("hi.py"), "stuff") == Language.PYTHON | ||
assert NotebookLoaderForTesting.detect_language(Path("hi.sql"), "stuff") == Language.SQL | ||
assert NotebookLoaderForTesting.detect_language(Path("hi"), "# Databricks notebook source") == Language.PYTHON | ||
assert NotebookLoaderForTesting.detect_language(Path("hi"), "-- Databricks notebook source") == Language.SQL | ||
assert not NotebookLoaderForTesting.detect_language(Path("hi"), "stuff") |
Oops, something went wrong.