summaryrefslogtreecommitdiff
path: root/logilab/common/visitor.py
diff options
context:
space:
mode:
Diffstat (limited to 'logilab/common/visitor.py')
-rw-r--r--logilab/common/visitor.py17
1 files changed, 11 insertions, 6 deletions
diff --git a/logilab/common/visitor.py b/logilab/common/visitor.py
index ed2b70f..0698bae 100644
--- a/logilab/common/visitor.py
+++ b/logilab/common/visitor.py
@@ -21,21 +21,24 @@
"""
+from typing import Any, Callable, Optional, Union
+from logilab.common.types import Node, HTMLWriter, TextWriter
__docformat__ = "restructuredtext en"
-def no_filter(_):
+
+def no_filter(_: Node) -> int:
return 1
# Iterators ###################################################################
class FilteredIterator(object):
- def __init__(self, node, list_func, filter_func=None):
+ def __init__(self, node: Node, list_func: Callable, filter_func: Optional[Any] = None) -> None:
self._next = [(node, 0)]
if filter_func is None:
filter_func = no_filter
self._list = list_func(node, filter_func)
- def __next__(self):
+ def __next__(self) -> Optional[Node]:
try:
return self._list.pop(0)
except :
@@ -89,18 +92,20 @@ class VisitedMixIn(object):
"""
Visited interface allow node visitors to use the node
"""
- def get_visit_name(self):
+ def get_visit_name(self) -> str:
"""
return the visit name for the mixed class. When calling 'accept', the
method <'visit_' + name returned by this method> will be called on the
visitor
"""
try:
- return self.TYPE.replace('-', '_')
+ # mypy: "VisitedMixIn" has no attribute "TYPE"
+ # dynamic attribute
+ return self.TYPE.replace('-', '_') # type: ignore
except:
return self.__class__.__name__.lower()
- def accept(self, visitor, *args, **kwargs):
+ def accept(self, visitor: Union[HTMLWriter, TextWriter], *args: Any, **kwargs: Any) -> Optional[Any]:
func = getattr(visitor, 'visit_%s' % self.get_visit_name())
return func(self, *args, **kwargs)