Skip to content

Commit

Permalink
Move DebugLabeledMatcher to base_matchers.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 567721061
  • Loading branch information
ssbr authored and copybara-github committed Sep 22, 2023
1 parent 3da7449 commit 9cc48a7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
9 changes: 6 additions & 3 deletions refex/fix/fixers/idiom_fixers.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,16 @@ def _function_containing(matcher):
# Matches any returning that's not "return" or "return None" (which are two
# different ast node values: ast.Return(value=None) and
# ast.Return(value=ast.Name(id='None')) respectively)
_NON_NONE_RETURN = matcher_.DebugLabeledMatcher(
_NON_NONE_RETURN = base_matchers.DebugLabeledMatcher(
'Non-none return',
ast_matchers.Return(
value=base_matchers.Unless(
base_matchers.AnyOf(
base_matchers.Equals(None), syntax_matchers.ExprPattern(
'None')))))
base_matchers.Equals(None), syntax_matchers.ExprPattern('None')
)
)
),
)

_NONE_RETURNS_FIXERS = [
fixer.SimplePythonFixer(
Expand Down
18 changes: 0 additions & 18 deletions refex/python/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,24 +921,6 @@ def bind_variables(self):
type_filter = None


@attr.s(frozen=True)
class DebugLabeledMatcher(Matcher):
"""A matcher which wraps another matcher with a label for debugging."""
debug_label = attr.ib(type=Text)
submatcher = submatcher_attrib(type=Matcher)
log_level = attr.ib(default=logging.DEBUG, type=int)

@property
def _log_level(self):
"""See base class."""
return self.log_level

def __str__(self):
return '[%s]' % (self.debug_label,)

def _match(self, *args, **kwargs):
"""See base class."""
return self.submatcher.match(*args, **kwargs)


def accumulating_matcher(f):
Expand Down
22 changes: 22 additions & 0 deletions refex/python/matchers/base_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
from typing import Container, Dict, Hashable, Iterable, List, Sequence
import weakref

from absl import logging
import attr
import cached_property
from refex import formatting
Expand Down Expand Up @@ -440,6 +441,27 @@ def _match(self, context, candidate):
self.replacements))


@attr.s(frozen=True)
class DebugLabeledMatcher(matcher.Matcher):
"""A matcher which wraps another matcher with a label for debugging."""

debug_label = attr.ib(type=str)
submatcher = matcher.submatcher_attrib(type=matcher.Matcher)
log_level = attr.ib(default=logging.DEBUG, type=int)

@property
def _log_level(self):
"""See base class."""
return self.log_level

def __str__(self):
return '[%s]' % (self.debug_label,)

def _match(self, *args, **kwargs):
"""See base class."""
return self.submatcher.match(*args, **kwargs)


######################
# Recursive matchers #
######################
Expand Down

0 comments on commit 9cc48a7

Please sign in to comment.