本文整理汇总了Python中astroid.extract_node函数的典型用法代码示例。如果您正苦于以下问题:Python extract_node函数的具体用法?Python extract_node怎么用?Python extract_node使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了extract_node函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_check_bad_docstring
def test_check_bad_docstring(self):
stmt = astroid.extract_node('def fff():\n """bad coment"""\n pass')
with self.assertAddsMessages(
Message(
"wrong-spelling-in-docstring",
line=2,
args=(
"coment",
"bad coment",
" ^^^^^^",
self._get_msg_suggestions("coment"),
),
)
):
self.checker.visit_functiondef(stmt)
stmt = astroid.extract_node('class Abc(object):\n """bad coment"""\n pass')
with self.assertAddsMessages(
Message(
"wrong-spelling-in-docstring",
line=2,
args=(
"coment",
"bad coment",
" ^^^^^^",
self._get_msg_suggestions("coment"),
),
)
):
self.checker.visit_classdef(stmt)
示例2: test_module_level_names
def test_module_level_names(self):
assign = astroid.extract_node("""
import collections
Class = collections.namedtuple("a", ("b", "c")) #@
""")
with self.assertNoMessages():
self.checker.visit_assignname(assign.targets[0])
assign = astroid.extract_node("""
class ClassA(object):
pass
ClassB = ClassA
""")
with self.assertNoMessages():
self.checker.visit_assignname(assign.targets[0])
module = astroid.parse("""
def A():
return 1, 2, 3
CONSTA, CONSTB, CONSTC = A()
CONSTD = A()""")
with self.assertNoMessages():
self.checker.visit_assignname(module.body[1].targets[0].elts[0])
self.checker.visit_assignname(module.body[2].targets[0])
assign = astroid.extract_node("""
CONST = "12 34 ".rstrip().split()""")
with self.assertNoMessages():
self.checker.visit_assignname(assign.targets[0])
示例3: test_skip_camel_cased_words
def test_skip_camel_cased_words(self):
stmt = astroid.extract_node(
'class ComentAbc(object):\n """comentAbc with a bad coment"""\n pass')
with self.assertAddsMessages(
Message('wrong-spelling-in-docstring', line=2,
args=('coment', 'comentAbc with a bad coment',
' ^^^^^^',
self._get_msg_suggestions('coment')))):
self.checker.visit_classdef(stmt)
# With just a single upper case letter in the end
stmt = astroid.extract_node(
'class ComentAbc(object):\n """argumentN with a bad coment"""\n pass')
with self.assertAddsMessages(
Message('wrong-spelling-in-docstring', line=2,
args=('coment', 'argumentN with a bad coment',
' ^^^^^^',
self._get_msg_suggestions('coment')))):
self.checker.visit_classdef(stmt)
# With just a single lower and upper case letter is not good
stmt = astroid.extract_node(
'class ComentAbc(object):\n """zN with a bad comment"""\n pass')
with self.assertAddsMessages(
Message('wrong-spelling-in-docstring', line=2,
args=('zN', 'zN with a bad comment',
'^^',
self._get_msg_suggestions('zN')))):
self.checker.visit_classdef(stmt)
示例4: test_custom_callback_string
def test_custom_callback_string(self):
""" Test the --calbacks option works. """
node = astroid.extract_node("""
def callback_one(abc):
''' should not emit unused-argument. '''
""")
with self.assertNoMessages():
self.checker.visit_functiondef(node)
self.checker.leave_functiondef(node)
node = astroid.extract_node("""
def two_callback(abc, defg):
''' should not emit unused-argument. '''
""")
with self.assertNoMessages():
self.checker.visit_functiondef(node)
self.checker.leave_functiondef(node)
node = astroid.extract_node("""
def normal_func(abc):
''' should emit unused-argument. '''
""")
with self.assertAddsMessages(
Message('unused-argument', node=node['abc'], args='abc')):
self.checker.visit_functiondef(node)
self.checker.leave_functiondef(node)
node = astroid.extract_node("""
def cb_func(abc):
''' Previous callbacks are overridden. '''
""")
with self.assertAddsMessages(
Message('unused-argument', node=node['abc'], args='abc')):
self.checker.visit_functiondef(node)
self.checker.leave_functiondef(node)
示例5: test_skip_camel_cased_words
def test_skip_camel_cased_words(self):
stmt = astroid.extract_node(
'class ComentAbc(object):\n """comentAbc with a bad coment"""\n pass')
with self.assertAddsMessages(
Message('wrong-spelling-in-docstring', line=2,
args=('coment', 'comentAbc with a bad coment',
' ^^^^^^',
self._get_msg_suggestions('coment')))):
self.checker.visit_classdef(stmt)
# With just a single upper case letter in the end
stmt = astroid.extract_node(
'class ComentAbc(object):\n """argumentN with a bad coment"""\n pass')
with self.assertAddsMessages(
Message('wrong-spelling-in-docstring', line=2,
args=('coment', 'argumentN with a bad coment',
' ^^^^^^',
self._get_msg_suggestions('coment')))):
self.checker.visit_classdef(stmt)
for ccn in ('xmlHttpRequest', 'newCustomer', 'newCustomerId',
'innerStopwatch', 'supportsIpv6OnIos', 'affine3D'):
stmt = astroid.extract_node(
'class TestClass(object):\n """{} comment"""\n pass'.format(ccn))
self.checker.visit_classdef(stmt)
assert self.linter.release_messages() == []
示例6: test_dict_not_iter_method
def test_dict_not_iter_method(self):
arg_node = astroid.extract_node("x.iterkeys(x) #@")
stararg_node = astroid.extract_node("x.iterkeys(*x) #@")
kwarg_node = astroid.extract_node("x.iterkeys(y=x) #@")
with self.assertNoMessages():
for node in (arg_node, stararg_node, kwarg_node):
self.checker.visit_call(node)
示例7: testSingleLineClassStmts
def testSingleLineClassStmts(self):
stmt = astroid.extract_node("""
class MyError(Exception): pass #@
""")
self.checker.config.single_line_class_stmt = False
with self.assertAddsMessages(Message('multiple-statements', node=stmt.body[0])):
self.visitFirst(stmt)
self.checker.config.single_line_class_stmt = True
with self.assertNoMessages():
self.visitFirst(stmt)
stmt = astroid.extract_node("""
class MyError(Exception): a='a' #@
""")
self.checker.config.single_line_class_stmt = False
with self.assertAddsMessages(Message('multiple-statements', node=stmt.body[0])):
self.visitFirst(stmt)
self.checker.config.single_line_class_stmt = True
with self.assertNoMessages():
self.visitFirst(stmt)
stmt = astroid.extract_node("""
class MyError(Exception): a='a'; b='b' #@
""")
self.checker.config.single_line_class_stmt = False
with self.assertAddsMessages(Message('multiple-statements', node=stmt.body[0])):
self.visitFirst(stmt)
self.checker.config.single_line_class_stmt = True
with self.assertAddsMessages(Message('multiple-statements', node=stmt.body[0])):
self.visitFirst(stmt)
示例8: test_not_next_method
def test_not_next_method(self):
arg_node = astroid.extract_node('x.next(x) #@')
stararg_node = astroid.extract_node('x.next(*x) #@')
kwarg_node = astroid.extract_node('x.next(y=x) #@')
with self.assertNoMessages():
for node in (arg_node, stararg_node, kwarg_node):
self.checker.visit_call(node)
示例9: test_dict_not_view_method
def test_dict_not_view_method(self):
arg_node = astroid.extract_node('x.viewkeys(x) #@')
stararg_node = astroid.extract_node('x.viewkeys(*x) #@')
kwarg_node = astroid.extract_node('x.viewkeys(y=x) #@')
non_dict_node = astroid.extract_node('x=[]\nx.viewkeys() #@')
with self.assertNoMessages():
for node in (arg_node, stararg_node, kwarg_node, non_dict_node):
self.checker.visit_call(node)
示例10: test_wrong_name_of_func_params_in_numpy_docstring
def test_wrong_name_of_func_params_in_numpy_docstring(self):
"""Example of functions with inconsistent parameter names in the
signature and in the Numpy style documentation
"""
node = astroid.extract_node("""
def function_foo(xarg, yarg, zarg):
'''function foo ...
Parameters
----------
xarg1: int
bla xarg
yarg: float
bla yarg
zarg1: str
bla zarg
'''
return xarg + yarg
""")
with self.assertAddsMessages(
Message(
msg_id='missing-param-doc',
node=node,
args=('xarg, xarg1, zarg, zarg1',)),
Message(
msg_id='missing-type-doc',
node=node,
args=('xarg, xarg1, zarg, zarg1',)),
):
self.checker.visit_functiondef(node)
node = astroid.extract_node("""
def function_foo(xarg, yarg):
'''function foo ...
Parameters
----------
yarg1: float
bla yarg
For the other parameters, see bla.
'''
return xarg + yarg
""")
with self.assertAddsMessages(
Message(
msg_id='missing-param-doc',
node=node,
args=('yarg1',)),
Message(
msg_id='missing-type-doc',
node=node,
args=('yarg1',))
):
self.checker.visit_functiondef(node)
示例11: test_old_raise_syntax
def test_old_raise_syntax(self):
node = astroid.extract_node('raise Exception, "test"')
message = testutils.Message('old-raise-syntax', node=node)
with self.assertAddsMessages(message):
self.checker.visit_raise(node)
node = astroid.extract_node('raise Exception, "test", tb')
message = testutils.Message('old-raise-syntax', node=node)
with self.assertAddsMessages(message):
self.checker.visit_raise(node)
示例12: testGetArgumentFromCall
def testGetArgumentFromCall():
node = astroid.extract_node("foo(a, not_this_one=1, this_one=2)")
arg = utils.get_argument_from_call(node, position=2, keyword="this_one")
assert 2 == arg.value
node = astroid.extract_node("foo(a)")
with pytest.raises(utils.NoSuchArgumentError):
utils.get_argument_from_call(node, position=1)
with pytest.raises(ValueError):
utils.get_argument_from_call(node, None, None)
name = utils.get_argument_from_call(node, position=0)
assert name.name == "a"
示例13: test_custom_callback_string
def test_custom_callback_string(self):
""" Test the --calbacks option works. """
def cleanup():
self.checker._to_consume = _to_consume
_to_consume = self.checker._to_consume
self.checker._to_consume = []
self.addCleanup(cleanup)
node = astroid.extract_node(
"""
def callback_one(abc):
''' should not emit unused-argument. '''
"""
)
with self.assertNoMessages():
self.checker.visit_functiondef(node)
self.checker.leave_functiondef(node)
node = astroid.extract_node(
"""
def two_callback(abc, defg):
''' should not emit unused-argument. '''
"""
)
with self.assertNoMessages():
self.checker.visit_functiondef(node)
self.checker.leave_functiondef(node)
node = astroid.extract_node(
"""
def normal_func(abc):
''' should emit unused-argument. '''
"""
)
with self.assertAddsMessages(Message("unused-argument", node=node["abc"], args="abc")):
self.checker.visit_functiondef(node)
self.checker.leave_functiondef(node)
node = astroid.extract_node(
"""
def cb_func(abc):
''' Previous callbacks are overriden. '''
"""
)
with self.assertAddsMessages(Message("unused-argument", node=node["abc"], args="abc")):
self.checker.visit_functiondef(node)
self.checker.leave_functiondef(node)
示例14: test_existing_func_params_in_numpy_docstring
def test_existing_func_params_in_numpy_docstring(self):
"""Example of a function with correctly documented parameters and
return values (Numpy style)
"""
node = astroid.extract_node("""
def function_foo(xarg, yarg, zarg, warg):
'''function foo ...
Parameters
----------
xarg: int
bla xarg
yarg: my.qualified.type
bla yarg
zarg: int
bla zarg
warg: my.qualified.type
bla warg
Returns
-------
float
sum
'''
return xarg + yarg
""")
with self.assertNoMessages():
self.checker.visit_functiondef(node)
示例15: test_missing_func_params_in_sphinx_docstring
def test_missing_func_params_in_sphinx_docstring(self):
"""Example of a function with missing Sphinx parameter documentation in
the docstring
"""
node = astroid.extract_node("""
def function_foo(x, y, z):
'''docstring ...
:param x: bla
:param int z: bar
'''
pass
""")
with self.assertAddsMessages(
Message(
msg_id='missing-param-doc',
node=node,
args=('y',)),
Message(
msg_id='missing-type-doc',
node=node,
args=('x, y',))
):
self.checker.visit_functiondef(node)