-
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.
Migrate Python linters from
ast
(standard library) to astroid
pac…
…kage (#1835) ## Changes Migrate Python linters from ast to astroid Implement minimal inference ### Linked issues Progresses #1205 ### Functionality - [ ] added relevant user documentation - [ ] added new CLI command - [ ] modified existing command: `databricks labs ucx ...` - [ ] added a new workflow - [ ] modified existing workflow: `...` - [ ] added a new table - [ ] modified existing table: `...` ### Tests - [ ] manually tested - [x] added unit tests - [ ] added integration tests - [ ] verified on staging environment (screenshot attached) --------- Co-authored-by: Eric Vergnaud <[email protected]>
- Loading branch information
1 parent
20474c3
commit f346f0a
Showing
22 changed files
with
526 additions
and
449 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
53 changes: 31 additions & 22 deletions
53
src/databricks/labs/ucx/source_code/linters/ast_helpers.py
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 |
---|---|---|
@@ -1,30 +1,39 @@ | ||
import ast | ||
import logging | ||
|
||
from astroid import Attribute, Call, Name # type: ignore | ||
|
||
class AstHelper: | ||
@staticmethod | ||
def get_full_attribute_name(node: ast.Attribute) -> str: | ||
return AstHelper._get_value(node) | ||
logger = logging.getLogger(__file__) | ||
|
||
@staticmethod | ||
def get_full_function_name(node: ast.Call) -> str | None: | ||
if isinstance(node.func, ast.Attribute): | ||
return AstHelper._get_value(node.func) | ||
missing_handlers: set[str] = set() | ||
|
||
if isinstance(node.func, ast.Name): | ||
return node.func.id | ||
|
||
class AstHelper: | ||
@classmethod | ||
def get_full_attribute_name(cls, node: Attribute) -> str: | ||
return cls._get_attribute_value(node) | ||
|
||
@classmethod | ||
def get_full_function_name(cls, node: Call) -> str | None: | ||
if not isinstance(node, Call): | ||
return None | ||
if isinstance(node.func, Attribute): | ||
return cls._get_attribute_value(node.func) | ||
if isinstance(node.func, Name): | ||
return node.func.name | ||
return None | ||
|
||
@staticmethod | ||
def _get_value(node: ast.Attribute): | ||
if isinstance(node.value, ast.Name): | ||
return node.value.id + '.' + node.attr | ||
|
||
if isinstance(node.value, ast.Attribute): | ||
value = AstHelper._get_value(node.value) | ||
if not value: | ||
return None | ||
return value + '.' + node.attr | ||
|
||
@classmethod | ||
def _get_attribute_value(cls, node: Attribute): | ||
if isinstance(node.expr, Name): | ||
return node.expr.name + '.' + node.attrname | ||
if isinstance(node.expr, Attribute): | ||
parent = cls._get_attribute_value(node.expr) | ||
return node.attrname if parent is None else parent + '.' + node.attrname | ||
if isinstance(node.expr, Call): | ||
name = cls.get_full_function_name(node.expr) | ||
return node.attrname if name is None else name + '.' + node.attrname | ||
name = type(node.expr).__name__ | ||
if name not in missing_handlers: | ||
missing_handlers.add(name) | ||
logger.debug(f"Missing handler for {name}") | ||
return None |
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
Oops, something went wrong.