예제 #1
0
 def test_returns(self):
     self.assertEqual(
         self.get_all_match_strings(
             syntax_matchers.NamedFunctionDefinition(
                 returns=base_matchers.Unless(None)),
             textwrap.dedent("""\
             def f1(): pass
             def f2() -> None: pass
             def f3() -> int: return 3
         """),
         ),
         ['def f2() -> None: pass', 'def f3() -> int: return 3'],
     )
예제 #2
0
파일: idiom_fixers.py 프로젝트: ssbr/refex
def _function_containing(matcher):
    """Returns a ast_matchers matcher for a function where any statement in the body matches `matcher`."""
    return syntax_matchers.NamedFunctionDefinition(body=base_matchers.Contains(
        syntax_matchers.IsOrHasDescendant(matcher)))
예제 #3
0
파일: idiom_fixers.py 프로젝트: ssbr/refex
        matcher=matcher,
        replacement=replacement,
        **kwargs)


def _function_containing(matcher):
    """Returns a ast_matchers matcher for a function where any statement in the body matches `matcher`."""
    return syntax_matchers.NamedFunctionDefinition(body=base_matchers.Contains(
        syntax_matchers.IsOrHasDescendant(matcher)))


# Matches any function returning Optional[T] for some T.
_IN_FUNCTION_RETURNING_OPTIONAL = syntax_matchers.InNamedFunction(
    syntax_matchers.NamedFunctionDefinition(returns=base_matchers.AnyOf(
        syntax_matchers.ExprPattern('Optional[$_]'),
        syntax_matchers.ExprPattern('typing.Optional[$_]'),
        # TODO: May want to also include Union[None, ...].
        # TODO: match type comments as well.
    )))

# 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',
    ast_matchers.Return(value=base_matchers.Unless(
        base_matchers.AnyOf(base_matchers.Equals(None),
                            syntax_matchers.ExprPattern('None')))))

_NONE_RETURNS_FIXERS = [
    fixer.SimplePythonFixer(
        message=
예제 #4
0
 def test_body(self):
     self.assertEqual(
         self.get_all_match_strings(
             syntax_matchers.NamedFunctionDefinition(
                 body=syntax_matchers.HasDescendant(2)),
             'def f1(): pass\ndef f2(): 2\n'), ['def f2(): 2'])
예제 #5
0
 def test_default(self):
     self.assertEqual(
         self.get_all_match_strings(
             syntax_matchers.NamedFunctionDefinition(),
             'def f(): pass\nlambda: None\n'), ['def f(): pass'])