Ejemplo n.º 1
0
 def test_nonpython_dollars_source(self, src):
     parsed = matcher.parse_ast(src)
     m = base_matchers.Bind('bound', ast_matchers.Call())
     [matchinfo] = matcher.find_iter(m, parsed)
     self.assertEqual(
         src,
         syntactic_template.PythonExprTemplate('$bound').substitute_match(
             parsed, matchinfo.match, {'bound': matchinfo.match}))
Ejemplo n.º 2
0
 message=
 'Use logging.exception inside an except handler to automatically log the full stack trace of the error',
 url=
 'https://refex.readthedocs.io/en/latest/guide/fixers/logging_exceptions.html',
 significant=True,
 category=_LOGGING_EXCEPTION_CATEGORY,
 matcher=base_matchers.AllOf(
     ast_matchers.Call(
         func=base_matchers.Bind(
             'logging_error',
             syntax_matchers.ExprPattern('logging.error')),
         args=base_matchers.Contains(
             base_matchers.AllOf(
                 _in_exception_handler(
                     'e',
                     on_conflict=matcher_.BindConflict.MERGE_IDENTICAL),
                 ast_matchers.Name(id=base_matchers.Bind(
                     'e',
                     on_conflict=matcher_.BindConflict.MERGE_IDENTICAL))
             )),
         keywords=base_matchers.Unless(
             base_matchers.Contains(
                 ast_matchers.keyword(arg='exc_info'))),
     ), ),
 replacement=dict(logging_error=syntactic_template.PythonStmtTemplate(
     'logging.exception')),
 example_fragment=textwrap.dedent("""
   try:
     x = bar() + baz()
   except KeyError as e:
     logging.error('Bad thing happened: %s', e)
Ejemplo n.º 3
0
 def test_nonparent_ancestor_hasparent(self):
     m = base_matchers.AllOf(ast_matchers.Num(),
                             syntax_matchers.HasParent(ast_matchers.Call()))
     self.assertEqual(self.get_all_match_strings(m, 'foo(x + 1)'), [])
Ejemplo n.º 4
0
 def test_nonparent_ancestor(self, matcher_type):
     m = base_matchers.AllOf(ast_matchers.Num(),
                             matcher_type(ast_matchers.Call()))
     self.assertEqual(self.get_all_match_strings(m, 'foo(x + 1)'), ['1'])
Ejemplo n.º 5
0
 def test_nonchild_descendant_haschild(self):
     m = base_matchers.AllOf(ast_matchers.Call(),
                             syntax_matchers.HasChild(ast_matchers.Num()))
     self.assertEqual(self.get_all_match_strings(m, 'foo(x + 1)'), [])
Ejemplo n.º 6
0
 def test_nonchild_descendant(self, matcher_type):
     m = base_matchers.AllOf(ast_matchers.Call(),
                             matcher_type(ast_matchers.Num()))
     self.assertEqual(self.get_all_match_strings(m, 'foo(x + 1)'),
                      ['foo(x + 1)'])