Skip to content

Commit

Permalink
Remove unnecessary base class and dead code in `pylint.pyreverse.util…
Browse files Browse the repository at this point in the history
…s` (#6712)

* Remove unnecessary base class and dead code

* Add ChangeLog and whatsnew entry
  • Loading branch information
DudeNr33 authored May 27, 2022
1 parent a7ff1fd commit b454b12
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 43 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ Release date: TBA

Ref #5392

* ``pylint.pyreverse.ASTWalker`` has been removed, as it was only used internally by a single child class.

Ref #6712

* ``interfaces.implements`` has been deprecated and will be removed in 3.0. Please use standard inheritance
patterns instead of ``__implements__``.

Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/2.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ Other Changes

Closes #6644

* ``pylint.pyreverse.ASTWalker`` has been removed, as it was only used internally by a single child class.

Ref #6712


Deprecations
============
Expand Down
49 changes: 6 additions & 43 deletions pylint/pyreverse/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
from astroid import nodes

if TYPE_CHECKING:
from pylint.pyreverse.diadefslib import DiaDefGenerator
from pylint.pyreverse.diagrams import ClassDiagram, PackageDiagram
from pylint.pyreverse.inspector import Linker

_CallbackT = Callable[
[nodes.NodeNG],
Expand Down Expand Up @@ -121,8 +119,8 @@ def show_attr(self, node: nodes.NodeNG | str) -> bool:
return not self.__mode & VIS_MOD[visibility]


class ASTWalker:
"""A walker visiting a tree in preorder, calling on the handler:.
class LocalsVisitor:
"""Visit a project by traversing the locals dictionary.
* visit_<class name> on entering a node, where class name is the class of
the node in lower case
Expand All @@ -131,62 +129,27 @@ class ASTWalker:
the node in lower case
"""

def __init__(self, handler: DiaDefGenerator | Linker | LocalsVisitor) -> None:
self.handler = handler
def __init__(self) -> None:
self._cache: dict[type[nodes.NodeNG], _CallbackTupleT] = {}

def walk(self, node: nodes.NodeNG, _done: set[nodes.NodeNG] | None = None) -> None:
"""Walk on the tree from <node>, getting callbacks from handler."""
if _done is None:
_done = set()
if node in _done:
raise AssertionError((id(node), node, node.parent))
_done.add(node)
self.visit(node)
for child_node in node.get_children():
assert child_node is not node
self.walk(child_node, _done)
self.leave(node)
assert node.parent is not node
self._visited: set[nodes.NodeNG] = set()

def get_callbacks(self, node: nodes.NodeNG) -> _CallbackTupleT:
"""Get callbacks from handler for the visited node."""
klass = node.__class__
methods = self._cache.get(klass)
if methods is None:
handler = self.handler
kid = klass.__name__.lower()
e_method = getattr(
handler, f"visit_{kid}", getattr(handler, "visit_default", None)
self, f"visit_{kid}", getattr(self, "visit_default", None)
)
l_method = getattr(
handler, f"leave_{kid}", getattr(handler, "leave_default", None)
self, f"leave_{kid}", getattr(self, "leave_default", None)
)
self._cache[klass] = (e_method, l_method)
else:
e_method, l_method = methods
return e_method, l_method

def visit(self, node: nodes.NodeNG) -> Any:
"""Walk on the tree from <node>, getting callbacks from handler."""
method = self.get_callbacks(node)[0]
if method is not None:
method(node)

def leave(self, node: nodes.NodeNG) -> None:
"""Walk on the tree from <node>, getting callbacks from handler."""
method = self.get_callbacks(node)[1]
if method is not None:
method(node)


class LocalsVisitor(ASTWalker):
"""Visit a project by traversing the locals dictionary."""

def __init__(self) -> None:
super().__init__(self)
self._visited: set[nodes.NodeNG] = set()

def visit(self, node: nodes.NodeNG) -> Any:
"""Launch the visit starting from the given node."""
if node in self._visited:
Expand Down

0 comments on commit b454b12

Please sign in to comment.