本文整理汇总了Python中astroid.ClassDef方法的典型用法代码示例。如果您正苦于以下问题:Python astroid.ClassDef方法的具体用法?Python astroid.ClassDef怎么用?Python astroid.ClassDef使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astroid
的用法示例。
在下文中一共展示了astroid.ClassDef方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: lookup
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import ClassDef [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: _check_bad_exception_context
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import ClassDef [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)
示例3: is_enum_class
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import ClassDef [as 别名]
def is_enum_class(node):
"""Check if a class definition defines an Enum class.
:param node: The class node to check.
:type node: astroid.ClassDef
:returns: True if the given node represents an Enum class. False otherwise.
:rtype: bool
"""
for base in node.bases:
try:
inferred_bases = base.inferred()
except astroid.InferenceError:
continue
for ancestor in inferred_bases:
if not isinstance(ancestor, astroid.ClassDef):
continue
if ancestor.name == 'Enum' and ancestor.root().name == 'enum':
return True
return False
示例4: _loop_exits_early
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import ClassDef [as 别名]
def _loop_exits_early(loop):
"""
Returns true if a loop may ends up in a break statement.
Args:
loop (astroid.For, astroid.While): the loop node inspected.
Returns:
bool: True if the loop may ends up in a break statement, False otherwise.
"""
loop_nodes = (astroid.For, astroid.While)
definition_nodes = (astroid.FunctionDef, astroid.ClassDef)
inner_loop_nodes = [
_node for _node in loop.nodes_of_class(loop_nodes,
skip_klass=definition_nodes)
if _node != loop
]
return any(
_node for _node in loop.nodes_of_class(astroid.Break,
skip_klass=definition_nodes)
if _get_break_loop_node(_node) not in inner_loop_nodes
)
示例5: _check_in_loop
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import ClassDef [as 别名]
def _check_in_loop(self, node, node_name):
"""check that a node is inside a for or while loop"""
_node = node.parent
while _node:
if isinstance(_node, (astroid.For, astroid.While)):
if node not in _node.orelse:
return
if isinstance(_node, (astroid.ClassDef, astroid.FunctionDef)):
break
if (isinstance(_node, astroid.TryFinally)
and node in _node.finalbody
and isinstance(node, astroid.Continue)):
self.add_message('continue-in-finally', node=node)
_node = _node.parent
self.add_message('not-in-loop', node=node, args=node_name)
示例6: _check_using_constant_test
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import ClassDef [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)
示例7: _check_type_x_is_y
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import ClassDef [as 别名]
def _check_type_x_is_y(self, node, left, operator, right):
"""Check for expressions like type(x) == Y."""
left_func = utils.safe_infer(left.func)
if not (isinstance(left_func, astroid.ClassDef)
and left_func.qname() == TYPE_QNAME):
return
if operator in ('is', 'is not') and _is_one_arg_pos_call(right):
right_func = utils.safe_infer(right.func)
if (isinstance(right_func, astroid.ClassDef)
and right_func.qname() == TYPE_QNAME):
# type(x) == type(a)
right_arg = utils.safe_infer(right.args[0])
if not isinstance(right_arg, LITERAL_NODE_TYPES):
# not e.g. type(x) == type([])
return
self.add_message('unidiomatic-typecheck', node=node)
示例8: visit_call
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import ClassDef [as 别名]
def visit_call(self, node):
"""Visit a Call node."""
try:
for inferred in node.func.infer():
if inferred is astroid.Uninferable:
continue
if inferred.root().name == OPEN_MODULE:
if getattr(node.func, 'name', None) in OPEN_FILES:
self._check_open_mode(node)
if inferred.root().name == UNITTEST_CASE:
self._check_redundant_assert(node, inferred)
if isinstance(inferred, astroid.ClassDef) and inferred.qname() == THREADING_THREAD:
self._check_bad_thread_instantiation(node)
if isinstance(inferred, astroid.FunctionDef) and inferred.qname() == COPY_COPY:
self._check_shallow_copy_environ(node)
self._check_deprecated_method(node, inferred)
except astroid.InferenceError:
return
示例9: visit_classdef
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import ClassDef [as 别名]
def visit_classdef(self, node):
def _metaclass_name(metaclass):
if isinstance(metaclass, (astroid.ClassDef, astroid.FunctionDef)):
return metaclass.name
return metaclass.as_string()
metaclass = node.declared_metaclass()
if not metaclass:
return
if isinstance(metaclass, astroid.FunctionDef):
# Try to infer the result.
metaclass = _infer_from_metaclass_constructor(node, metaclass)
if not metaclass:
# Don't do anything if we cannot infer the result.
return
if isinstance(metaclass, astroid.ClassDef):
if _is_invalid_metaclass(metaclass):
self.add_message('invalid-metaclass', node=node,
args=(_metaclass_name(metaclass), ))
else:
self.add_message('invalid-metaclass', node=node,
args=(_metaclass_name(metaclass), ))
示例10: _check_proper_bases
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import ClassDef [as 别名]
def _check_proper_bases(self, node):
"""
Detect that a class inherits something which is not
a class or a type.
"""
for base in node.bases:
ancestor = safe_infer(base)
if ancestor in (astroid.YES, None):
continue
if (isinstance(ancestor, astroid.Instance) and
ancestor.is_subtype_of('%s.type' % (BUILTINS,))):
continue
if (not isinstance(ancestor, astroid.ClassDef) or
_is_invalid_base_class(ancestor)):
self.add_message('inherit-non-class',
args=base.as_string(), node=node)
示例11: _is_iterator
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import ClassDef [as 别名]
def _is_iterator(node):
if node is astroid.YES:
# Just ignore YES objects.
return True
if isinstance(node, Generator):
# Generators can be itered.
return True
if isinstance(node, astroid.Instance):
try:
node.local_attr(NEXT_METHOD)
return True
except astroid.NotFoundError:
pass
elif isinstance(node, astroid.ClassDef):
metaclass = node.metaclass()
if metaclass and isinstance(metaclass, astroid.ClassDef):
try:
metaclass.local_attr(NEXT_METHOD)
return True
except astroid.NotFoundError:
pass
return False
示例12: test_classdef_attribute_assign
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import ClassDef [as 别名]
def test_classdef_attribute_assign():
"""Test whether type of attributes are properly being set."""
program = f'class Network:\n' \
f' def __init__(self, name, id):\n' \
f' self.name = name\n' \
f' self.id = id' \
f'\n' \
f'rogers = Network("Rogers", 5)\n' \
f'rogers.name = "BoB"\n' \
f'self = Network("asdf", 5)\n' \
f'self.name = "asdfaBoB"\n' \
f'\n'
module, inferer = cs._parse_text(program)
classdef_node = next(module.nodes_of_class(astroid.ClassDef))
for attribute_lst in classdef_node.instance_attrs.values():
for instance in attribute_lst:
attribute_type = inferer.type_constraints\
.resolve(classdef_node.type_environment.lookup_in_env(instance.attrname))
value_type = inferer.type_constraints.resolve(instance.parent.value.inf_type.getValue())
assert attribute_type == value_type
示例13: lookup
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import ClassDef [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)
示例14: _is_enum_class
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import ClassDef [as 别名]
def _is_enum_class(node: astroid.ClassDef) -> bool:
"""Check if a class definition defines an Enum class.
:param node: The class node to check.
:type node: astroid.ClassDef
:returns: True if the given node represents an Enum class. False otherwise.
:rtype: bool
"""
for base in node.bases:
try:
inferred_bases = base.inferred()
except astroid.InferenceError:
continue
for ancestor in inferred_bases:
if not isinstance(ancestor, astroid.ClassDef):
continue
if ancestor.name == "Enum" and ancestor.root().name == "enum":
return True
return False
示例15: _is_dataclass
# 需要导入模块: import astroid [as 别名]
# 或者: from astroid import ClassDef [as 别名]
def _is_dataclass(node: astroid.ClassDef) -> bool:
"""Check if a class definition defines a Python 3.7+ dataclass
:param node: The class node to check.
:type node: astroid.ClassDef
:returns: True if the given node represents a dataclass class. False otherwise.
:rtype: bool
"""
if not node.decorators:
return False
root_locals = node.root().locals
for decorator in node.decorators.nodes:
if isinstance(decorator, astroid.Call):
decorator = decorator.func
if not isinstance(decorator, (astroid.Name, astroid.Attribute)):
continue
if isinstance(decorator, astroid.Name):
name = decorator.name
else:
name = decorator.attrname
if name == DATACLASS_DECORATOR and DATACLASS_DECORATOR in root_locals:
return True
return False