本文整理汇总了Python中astroid.Const方法的典型用法代码示例。如果您正苦于以下问题:Python astroid.Const方法的具体用法?Python astroid.Const怎么用?Python astroid.Const使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astroid
的用法示例。
在下文中一共展示了astroid.Const方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: lookup
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Const [as 别名]
def lookup(node, name):
"""Lookup the given special method name in the given *node*
If the special method was found, then a list of attributes
will be returned. Otherwise, `astroid.AttributeInferenceError`
is going to be raised.
"""
if isinstance(node, (astroid.List,
astroid.Tuple,
astroid.Const,
astroid.Dict,
astroid.Set)):
return _builtin_lookup(node, name)
elif isinstance(node, astroid.Instance):
return _lookup_in_mro(node, name)
elif isinstance(node, astroid.ClassDef):
return _class_lookup(node, name)
raise exceptions.AttributeInferenceError(
attribute=name,
target=node
)
示例2: returns_something
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Const [as 别名]
def returns_something(return_node):
"""Check if a return node returns a value other than None.
:param return_node: The return node to check.
:type return_node: astroid.Return
:rtype: bool
:return: True if the return node returns a value other than None,
False otherwise.
"""
returns = return_node.value
if returns is None:
return False
return not (isinstance(returns, astroid.Const) and returns.value is None)
示例3: _check_bad_exception_context
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Const [as 别名]
def _check_bad_exception_context(self, node):
"""Verify that the exception context is properly set.
An exception context can be only `None` or an exception.
"""
cause = utils.safe_infer(node.cause)
if cause in (astroid.YES, None):
return
if isinstance(cause, astroid.Const):
if cause.value is not None:
self.add_message('bad-exception-context',
node=node)
elif (not isinstance(cause, astroid.ClassDef) and
not utils.inherit_from_std_ex(cause)):
self.add_message('bad-exception-context',
node=node)
示例4: _check_using_constant_test
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Const [as 别名]
def _check_using_constant_test(self, node, test):
const_nodes = (
astroid.Module,
astroid.scoped_nodes.GeneratorExp,
astroid.Lambda, astroid.FunctionDef, astroid.ClassDef,
astroid.bases.Generator, astroid.UnboundMethod,
astroid.BoundMethod, astroid.Module)
structs = (astroid.Dict, astroid.Tuple, astroid.Set)
# These nodes are excepted, since they are not constant
# values, requiring a computation to happen. The only type
# of node in this list which doesn't have this property is
# Attribute, which is excepted because the conditional statement
# can be used to verify that the attribute was set inside a class,
# which is definitely a valid use case.
except_nodes = (astroid.Attribute, astroid.Call,
astroid.BinOp, astroid.BoolOp, astroid.UnaryOp,
astroid.Subscript)
inferred = None
emit = isinstance(test, (astroid.Const, ) + structs + const_nodes)
if not isinstance(test, except_nodes):
inferred = utils.safe_infer(test)
if emit or isinstance(inferred, const_nodes):
self.add_message('using-constant-test', node=node)
示例5: visit_compare
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Const [as 别名]
def visit_compare(self, node):
self._check_unidiomatic_typecheck(node)
# NOTE: this checker only works with binary comparisons like 'x == 42'
# but not 'x == y == 42'
if len(node.ops) != 1:
return
left = node.left
operator, right = node.ops[0]
if (operator in ('<', '<=', '>', '>=', '!=', '==')
and isinstance(left, astroid.Const)):
self._check_misplaced_constant(node, left, right, operator)
if operator == '==':
if isinstance(left, astroid.Const):
self._check_singleton_comparison(left, node)
elif isinstance(right, astroid.Const):
self._check_singleton_comparison(right, node)
if operator in ('is', 'is not'):
self._check_literal_comparison(right, node)
示例6: _check_len
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Const [as 别名]
def _check_len(self, node):
inferred = _safe_infer_call_result(node, node)
if not inferred or inferred is astroid.Uninferable:
return
if (isinstance(inferred, astroid.Instance)
and inferred.name == 'int'
and not isinstance(inferred, astroid.Const)):
# Assume it's good enough, since the int() call might wrap
# something that's uninferable for us
return
if not isinstance(inferred, astroid.Const):
self.add_message('invalid-length-returned', node=node)
return
value = inferred.value
if not isinstance(value, six.integer_types) or value < 0:
self.add_message('invalid-length-returned', node=node)
示例7: visit_call
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Const [as 别名]
def visit_call(self, node: NodeNG) -> None:
"""Called for every function call in the source code."""
if not isinstance(node.func, astroid.Name):
# It isn't a simple name, can't deduce what function it is.
return
if node.func.name not in self.TRANSLATION_FUNCTIONS:
# Not a function we care about.
return
if not self.linter.is_message_enabled(self.MESSAGE_ID, line=node.fromlineno):
return
first = node.args[0]
if isinstance(first, astroid.Const):
if isinstance(first.value, six.string_types):
# The first argument is a constant string! All is well!
return
# Bad!
self.add_message(self.MESSAGE_ID, args=node.func.name, node=node)
示例8: set_without_children
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Const [as 别名]
def set_without_children(node):
"""Populate ending locations for nodes that are guaranteed to never have
children. E.g. Const.
These node's end_col_offset are currently assigned based on their
computed string representation. This may differ from their actual
source code representation, however (mainly whitespace).
Precondition: `node` must not have a `last_child` (node).
"""
if not hasattr(node, 'end_lineno'):
node.end_lineno = node.fromlineno
# FIXME: using the as_string() is a bad technique because many different
# whitespace possibilities that may not be reflected in it!
if not hasattr(node, 'end_col_offset'):
node.end_col_offset = node.col_offset + len(node.as_string())
return node
示例9: lookup
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Const [as 别名]
def lookup(node, name):
"""Lookup the given special method name in the given *node*
If the special method was found, then a list of attributes
will be returned. Otherwise, `astroid.AttributeInferenceError`
is going to be raised.
"""
if isinstance(
node, (astroid.List, astroid.Tuple, astroid.Const, astroid.Dict, astroid.Set)
):
return _builtin_lookup(node, name)
if isinstance(node, astroid.Instance):
return _lookup_in_mro(node, name)
if isinstance(node, astroid.ClassDef):
return _class_lookup(node, name)
raise exceptions.AttributeInferenceError(attribute=name, target=node)
示例10: _check_bad_exception_context
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Const [as 别名]
def _check_bad_exception_context(self, node):
"""Verify that the exception context is properly set.
An exception context can be only `None` or an exception.
"""
cause = utils.safe_infer(node.cause)
if cause in (astroid.Uninferable, None):
return
if isinstance(cause, astroid.Const):
if cause.value is not None:
self.add_message("bad-exception-context", node=node)
elif not isinstance(cause, astroid.ClassDef) and not utils.inherit_from_std_ex(
cause
):
self.add_message("bad-exception-context", node=node)
示例11: _check_simplifiable_ifexp
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Const [as 别名]
def _check_simplifiable_ifexp(self, node):
if not isinstance(node.body, astroid.Const) or not isinstance(
node.orelse, astroid.Const
):
return
if not isinstance(node.body.value, bool) or not isinstance(
node.orelse.value, bool
):
return
if isinstance(node.test, astroid.Compare):
test_reduced_to = "test"
else:
test_reduced_to = "bool(test)"
if (node.body.value, node.orelse.value) == (True, False):
reduced_to = "'{}'".format(test_reduced_to)
elif (node.body.value, node.orelse.value) == (False, True):
reduced_to = "'not test'"
else:
return
self.add_message("simplifiable-if-expression", node=node, args=(reduced_to,))
示例12: visit_compare
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Const [as 别名]
def visit_compare(self, node):
self._check_callable_comparison(node)
self._check_logical_tautology(node)
self._check_unidiomatic_typecheck(node)
# NOTE: this checker only works with binary comparisons like 'x == 42'
# but not 'x == y == 42'
if len(node.ops) != 1:
return
left = node.left
operator, right = node.ops[0]
if operator in COMPARISON_OPERATORS and isinstance(left, astroid.Const):
self._check_misplaced_constant(node, left, right, operator)
if operator == "==":
if isinstance(left, astroid.Const):
self._check_singleton_comparison(left, node)
elif isinstance(right, astroid.Const):
self._check_singleton_comparison(right, node)
if operator == "!=":
if isinstance(right, astroid.Const):
self._check_singleton_comparison(right, node, negative_check=True)
if operator in ("is", "is not"):
self._check_literal_comparison(right, node)
示例13: _check_open_encoding
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Const [as 别名]
def _check_open_encoding(self, node):
"""Check that an open() call always has an encoding set."""
try:
mode_arg = utils.get_argument_from_call(node, position=1,
keyword='mode')
except utils.NoSuchArgumentError:
mode_arg = None
_encoding = None
try:
_encoding = utils.get_argument_from_call(node, position=2)
except utils.NoSuchArgumentError:
try:
_encoding = utils.get_argument_from_call(node,
keyword='encoding')
except utils.NoSuchArgumentError:
pass
if _encoding is None:
if mode_arg is None:
mode = None
else:
mode = utils.safe_infer(mode_arg)
if mode is not None and not isinstance(mode, astroid.Const):
# We can't say what mode is exactly.
return
if mode is None:
self.add_message('open-without-encoding', node=node)
elif 'b' in getattr(mode, 'value', ''):
# Files opened as binary don't need an encoding.
return
else:
self.add_message('open-without-encoding', node=node)
示例14: _is_constant_zero
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Const [as 别名]
def _is_constant_zero(node):
return isinstance(node, astroid.Const) and node.value == 0
示例15: _is_constant_empty_str
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import Const [as 别名]
def _is_constant_empty_str(node):
return isinstance(node, astroid.Const) and node.value == ''