本文整理汇总了Python中astroid.Expr方法的典型用法代码示例。如果您正苦于以下问题:Python astroid.Expr方法的具体用法?Python astroid.Expr怎么用?Python astroid.Expr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astroid
的用法示例。
在下文中一共展示了astroid.Expr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_conditional_expr
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Expr [as 别名]
def test_conditional_expr(self):
# See https://bitbucket.org/plas/thonny/issues/108/ast-marker-crashes-with-conditional
source = "a = True if True else False\nprint(a)"
m = self.create_mark_checker(source)
name_a = 'AssignName:a' if self.is_astroid_test else 'Name:a'
const_true = ('Const:True' if self.is_astroid_test else
'Name:True' if six.PY2 else
'Constant:True')
self.assertEqual(m.view_nodes_at(1, 0),
{name_a, "Assign:a = True if True else False", "Module:" + source})
self.assertEqual(m.view_nodes_at(1, 4),
{const_true, 'IfExp:True if True else False'})
if six.PY2:
self.assertEqual(m.view_nodes_at(2, 0), {"Print:print(a)"})
else:
self.assertEqual(m.view_nodes_at(2, 0), {"Name:print", "Call:print(a)", "Expr:print(a)"})
示例2: test_parens_around_func
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Expr [as 别名]
def test_parens_around_func(self):
source = textwrap.dedent(
'''
foo()
(foo)()
(lambda: 0)()
(lambda: ())()
(foo)((1))
(lambda: ())((2))
x = (obj.attribute.get_callback() or default_callback)()
''')
m = self.create_mark_checker(source)
self.assertEqual(m.view_nodes_at(2, 0), {"Name:foo", "Expr:foo()", "Call:foo()"})
self.assertEqual(m.view_nodes_at(3, 1), {"Name:foo"})
self.assertEqual(m.view_nodes_at(3, 0), {"Expr:(foo)()", "Call:(foo)()"})
self.assertEqual(m.view_nodes_at(4, 0), {"Expr:(lambda: 0)()", "Call:(lambda: 0)()"})
self.assertEqual(m.view_nodes_at(5, 0), {"Expr:(lambda: ())()", "Call:(lambda: ())()"})
self.assertEqual(m.view_nodes_at(6, 0), {"Expr:(foo)((1))", "Call:(foo)((1))"})
self.assertEqual(m.view_nodes_at(7, 0), {"Expr:(lambda: ())((2))", "Call:(lambda: ())((2))"})
self.assertEqual(m.view_nodes_at(8, 4),
{"Call:(obj.attribute.get_callback() or default_callback)()"})
self.assertIn('BoolOp:obj.attribute.get_callback() or default_callback', m.view_nodes_at(8, 5))
示例3: parse_snippet
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Expr [as 别名]
def parse_snippet(self, text, node):
"""
Returns the parsed AST tree for the given text, handling issues with indentation and newlines
when text is really an extracted part of larger code.
"""
# If text is indented, it's a statement, and we need to put in a scope for indents to be valid
# (using textwrap.dedent is insufficient because some lines may not indented, e.g. comments or
# multiline strings). If text is an expression but has newlines, we parenthesize it to make it
# parsable.
# For expressions and statements, we add a dummy statement '_' before it because if it's just a
# string contained in an astroid.Const or astroid.Expr it will end up in the doc attribute and be
# a pain to extract for comparison
indented = re.match(r'^[ \t]+\S', text)
if indented:
return self.module.parse('def dummy():\n' + text).body[0].body[0]
if util.is_expr(node):
return self.module.parse('_\n(' + text + ')').body[1].value
if util.is_module(node):
return self.module.parse(text)
return self.module.parse('_\n' + text).body[1]
示例4: _check_docstring
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Expr [as 别名]
def _check_docstring(self, node_type, node, report_missing=True,
confidence=interfaces.HIGH):
"""check the node has a non empty docstring"""
docstring = node.doc
if docstring is None:
if not report_missing:
return
lines = get_node_last_lineno(node) - node.lineno
if node_type == 'module' and not lines:
# If the module has no body, there's no reason
# to require a docstring.
return
max_lines = self.config.docstring_min_length
if node_type != 'module' and max_lines > -1 and lines < max_lines:
return
self.stats['undocumented_'+node_type] += 1
if (node.body and isinstance(node.body[0], astroid.Expr) and
isinstance(node.body[0].value, astroid.Call)):
# Most likely a string with a format call. Let's see.
func = utils.safe_infer(node.body[0].value.func)
if (isinstance(func, astroid.BoundMethod)
and isinstance(func.bound, astroid.Instance)):
# Strings in Python 3, others in Python 2.
if PY3K and func.bound.name == 'str':
return
elif func.bound.name in ('str', 'unicode', 'bytes'):
return
self.add_message('missing-docstring', node=node, args=(node_type,),
confidence=confidence)
elif not docstring.strip():
self.stats['undocumented_'+node_type] += 1
self.add_message('empty-docstring', node=node, args=(node_type,),
confidence=confidence)
示例5: _no_context_variadic_keywords
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Expr [as 别名]
def _no_context_variadic_keywords(node):
statement = node.statement()
scope = node.scope()
variadics = ()
if not isinstance(scope, astroid.FunctionDef):
return False
if isinstance(statement, astroid.Expr) and isinstance(statement.value, astroid.Call):
call = statement.value
variadics = call.keywords or ()
return _no_context_variadic(node, scope.args.kwarg, astroid.Keyword, variadics)
示例6: _no_context_variadic_positional
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Expr [as 别名]
def _no_context_variadic_positional(node):
statement = node.statement()
scope = node.scope()
variadics = ()
if not isinstance(scope, astroid.FunctionDef):
return False
if isinstance(statement, astroid.Expr) and isinstance(statement.value, astroid.Call):
call = statement.value
variadics = call.starargs
return _no_context_variadic(node, scope.args.vararg, astroid.Starred, variadics)
示例7: expr_node
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Expr [as 别名]
def expr_node(draw, value=None):
value = value or expr
node = astroid.Expr()
node.postinit(draw(value))
return node
示例8: test_expr
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Expr [as 别名]
def test_expr(expr):
module, _ = cs._parse_text(expr)
for expr_node in module.nodes_of_class(astroid.Expr):
assert expr_node.inf_type.getValue() == expr_node.value.inf_type.getValue()
示例9: test_bad_attribute_access
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Expr [as 别名]
def test_bad_attribute_access():
""" User tries to access a non-existing attribute; or misspells the attribute name.
"""
program = f'x = 1\n' \
f'x.wrong_name\n'
module, inferer = cs._parse_text(program)
expr_node = next(module.nodes_of_class(astroid.Expr))
expected_msg = 'TypeFail: Invalid attribute lookup x.wrong_name'
assert expr_node.inf_type.getValue() == expected_msg
示例10: test_function_def_call_no_args
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Expr [as 别名]
def test_function_def_call_no_args(function_name, return_value):
"""Test type setting in environment of a function call for a function with no parameters."""
program = _parse_to_function(function_name, [], repr(return_value)) + "\n" + function_name + "()\n"
module, inferer = cs._parse_text(program)
# there should be a single Expr node in this program
function_def_node = list(module.nodes_of_class(astroid.FunctionDef))[0]
return_tvar = function_def_node.type_environment.lookup_in_env('return')
return_type = inferer.type_constraints.resolve(return_tvar).getValue()
expr_node = next(module.nodes_of_class(astroid.Expr))
assert expr_node.inf_type.getValue() == return_type
示例11: _no_context_variadic_keywords
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Expr [as 别名]
def _no_context_variadic_keywords(node):
statement = node.statement()
scope = node.scope()
variadics = ()
if not isinstance(scope, astroid.FunctionDef):
return False
if isinstance(statement, (astroid.Return, astroid.Expr)) and isinstance(
statement.value, astroid.Call
):
call = statement.value
variadics = list(call.keywords or []) + call.kwargs
return _no_context_variadic(node, scope.args.kwarg, astroid.Keyword, variadics)
示例12: _no_context_variadic_positional
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Expr [as 别名]
def _no_context_variadic_positional(node):
statement = node.statement()
scope = node.scope()
variadics = ()
if not isinstance(scope, astroid.FunctionDef):
return False
if isinstance(statement, (astroid.Expr, astroid.Return)) and isinstance(
statement.value, astroid.Call
):
call = statement.value
variadics = call.starargs + call.kwargs
return _no_context_variadic(node, scope.args.vararg, astroid.Starred, variadics)
示例13: test_mark_tokens_simple
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Expr [as 别名]
def test_mark_tokens_simple(self):
source = tools.read_fixture('astroid', 'module.py')
m = self.create_mark_checker(source)
# Line 14 is: [indent 4] MY_DICT[key] = val
self.assertEqual(m.view_nodes_at(14, 4), {
"Name:MY_DICT",
"Subscript:MY_DICT[key]",
"Assign:MY_DICT[key] = val"
})
# Line 35 is: [indent 12] raise XXXError()
self.assertEqual(m.view_nodes_at(35, 12), {'Raise:raise XXXError()'})
self.assertEqual(m.view_nodes_at(35, 18), {'Call:XXXError()', 'Name:XXXError'})
# Line 53 is: [indent 12] autre = [a for (a, b) in MY_DICT if b]
self.assertEqual(m.view_nodes_at(53, 20), {'ListComp:[a for (a, b) in MY_DICT if b]'})
self.assertEqual(m.view_nodes_at(53, 21), {'Name:a'})
if self.is_astroid_test:
self.assertEqual(m.view_nodes_at(53, 23), {'Comprehension:for (a, b) in MY_DICT if b'})
else:
self.assertEqual(m.view_nodes_at(53, 23), {'comprehension:for (a, b) in MY_DICT if b'})
# Line 59 is: [indent 12] global_access(local, val=autre)
self.assertEqual(m.view_node_types_at(59, 12), {'Name', 'Call', 'Expr'})
self.assertEqual(m.view_nodes_at(59, 26), {'Name:local'})
if self.is_astroid_test:
self.assertEqual(m.view_nodes_at(59, 33), {'Keyword:val=autre'})
else:
self.assertEqual(m.view_nodes_at(59, 33), {'keyword:val=autre'})
self.assertEqual(m.view_nodes_at(59, 37), {'Name:autre'})
示例14: test_print_function
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Expr [as 别名]
def test_print_function(self):
# This testcase imports print as function (using from __future__). Check that we can parse it.
# verify_all_nodes doesn't work on Python 2 because the print() call parsed in isolation
# is viewed as a Print node since it doesn't see the future import
source = tools.read_fixture('astroid/nonregr.py')
m = self.create_mark_checker(source, verify=six.PY3)
# Line 16 is: [indent 8] print(v.get('yo'))
self.assertEqual(m.view_nodes_at(16, 8),
{ "Call:print(v.get('yo'))", "Expr:print(v.get('yo'))", "Name:print" })
self.assertEqual(m.view_nodes_at(16, 14), {"Call:v.get('yo')", "Attribute:v.get", "Name:v"})
# To make sure we can handle various hard cases, we include tests for issues reported for a
# similar project here: https://bitbucket.org/plas/thonny
示例15: test_splat
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Expr [as 别名]
def test_splat(self):
# See https://bitbucket.org/plas/thonny/issues/151/debugger-crashes-when-encountering-a-splat
source = textwrap.dedent("""
arr = [1,2,3,4,5]
def print_all(a, b, c, d, e):
print(a, b, c, d ,e)
print_all(*arr)
""")
m = self.create_mark_checker(source)
self.assertEqual(m.view_nodes_at(5, 0),
{ "Expr:print_all(*arr)", "Call:print_all(*arr)", "Name:print_all" })
if not six.PY2 or self.is_astroid_test:
self.assertEqual(m.view_nodes_at(5, 10), { "Starred:*arr" })
self.assertEqual(m.view_nodes_at(5, 11), { "Name:arr" })