fname
stringlengths 63
176
| rel_fname
stringclasses 706
values | line
int64 -1
4.5k
| name
stringlengths 1
81
| kind
stringclasses 2
values | category
stringclasses 2
values | info
stringlengths 0
77.9k
⌀ |
---|---|---|---|---|---|---|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/async.py
|
pylint/checkers/async.py
| 76 |
decorated_with
|
ref
|
function
|
if decorated_with(inferred.parent, self._async_generators):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/async.py
|
pylint/checkers/async.py
| 86 |
has_known_bases
|
ref
|
function
|
if not checker_utils.has_known_bases(inferred):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/async.py
|
pylint/checkers/async.py
| 89 |
match
|
ref
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/async.py
|
pylint/checkers/async.py
| 95 |
add_message
|
ref
|
function
|
self.add_message(
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/async.py
|
pylint/checkers/async.py
| 100 |
register
|
def
|
function
|
def register(linter: "PyLinter") -> None:
linter.register_checker(AsyncChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/async.py
|
pylint/checkers/async.py
| 101 |
register_checker
|
ref
|
function
|
linter.register_checker(AsyncChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/async.py
|
pylint/checkers/async.py
| 101 |
AsyncChecker
|
ref
|
function
|
linter.register_checker(AsyncChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 102 |
NamingStyle
|
def
|
class
|
get_regex
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 117 |
get_regex
|
def
|
function
|
def get_regex(cls, name_type):
return {
"module": cls.MOD_NAME_RGX,
"const": cls.CONST_NAME_RGX,
"class": cls.CLASS_NAME_RGX,
"function": cls.DEFAULT_NAME_RGX,
"method": cls.DEFAULT_NAME_RGX,
"attr": cls.DEFAULT_NAME_RGX,
"argument": cls.DEFAULT_NAME_RGX,
"variable": cls.DEFAULT_NAME_RGX,
"class_attribute": cls.CLASS_ATTRIBUTE_RGX,
"class_const": cls.CONST_NAME_RGX,
"inlinevar": cls.COMP_VAR_RGX,
}[name_type]
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 133 |
SnakeCaseStyle
|
def
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 146 |
CamelCaseStyle
|
def
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 157 |
PascalCaseStyle
|
def
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 168 |
UpperCaseStyle
|
def
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 179 |
AnyStyle
|
def
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 232 |
_redefines_import
|
def
|
function
|
def _redefines_import(node):
"""Detect that the given node (AssignName) is inside an
exception handler and redefines an import from the tryexcept body.
Returns _True if the node redefines an import, _False otherwise.
"""
current = node
while current and not isinstance(current.parent, nodes.ExceptHandler):
current = current.parent
if not current or not utils.error_of_type(current.parent, ImportError):
return _False
try_block = current.parent.parent
for import_node in try_block.nodes_of_class((nodes.ImportFrom, nodes.Import)):
for name, alias in import_node.names:
if alias:
if alias == node.name:
return _True
elif name == node.name:
return _True
return _False
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 240 |
error_of_type
|
ref
|
function
|
if not current or not utils.error_of_type(current.parent, ImportError):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 243 |
nodes_of_class
|
ref
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 262 |
in_loop
|
def
|
function
|
def in_loop(node: nodes.NodeNG) -> bool:
"""Return whether the node is inside a kind of for loop."""
return any(isinstance(parent, LOOPLIKE_NODES) for parent in node.node_ancestors())
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 264 |
node_ancestors
|
ref
|
function
|
return any(isinstance(parent, LOOPLIKE_NODES) for parent in node.node_ancestors())
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 267 |
in_nested_list
|
def
|
function
|
def in_nested_list(nested_list, obj):
"""Return true if the object is an element of <nested_list> or of a nested
list
"""
for elmt in nested_list:
if isinstance(elmt, (list, tuple)):
if in_nested_list(elmt, obj):
return _True
elif elmt == obj:
return _True
return _False
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 273 |
in_nested_list
|
ref
|
function
|
if in_nested_list(elmt, obj):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 280 |
_get_break_loop_node
|
def
|
function
|
def _get_break_loop_node(break_node):
"""Returns the loop node that holds the break node in arguments.
Args:
break_node (astroid.Break): the break node of interest.
Returns:
astroid.For or astroid.While: the loop node holding the break node.
"""
loop_nodes = (nodes.For, nodes.While)
parent = break_node.parent
while not isinstance(parent, loop_nodes) or break_node in getattr(
parent, "orelse", []
):
break_node = parent
parent = parent.parent
if parent is None:
break
return parent
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 301 |
_loop_exits_early
|
def
|
function
|
def _loop_exits_early(loop):
"""Returns true if a loop may end with a break statement.
Args:
loop (astroid.For, astroid.While): the loop node inspected.
Returns:
bool: _True if the loop may end with a break statement, _False otherwise.
"""
loop_nodes = (nodes.For, nodes.While)
definition_nodes = (nodes.FunctionDef, nodes.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(nodes.Break, skip_klass=definition_nodes)
if _get_break_loop_node(_node) not in inner_loop_nodes
)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 314 |
nodes_of_class
|
ref
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 319 |
nodes_of_class
|
ref
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 320 |
_get_break_loop_node
|
ref
|
function
|
if _get_break_loop_node(_node) not in inner_loop_nodes
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 324 |
_is_multi_naming_match
|
def
|
function
|
def _is_multi_naming_match(match, node_type, confidence):
return (
match is not None
and match.lastgroup is not None
and match.lastgroup not in EXEMPT_NAME_CATEGORIES
and (node_type != "method" or confidence != interfaces.INFERENCE_FAILURE)
)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 336 |
_get_properties
|
def
|
function
|
def _get_properties(config):
"""Returns a tuple of property classes and names.
Property classes are fully qualified, such as 'abc.abstractproperty' and
property names are the actual names, such as 'abstract_property'.
"""
property_classes = {BUILTIN_PROPERTY}
property_names = set() # Not returning 'property', it has its own check.
if config is not None:
property_classes.update(config.property_classes)
property_names.update(
prop.rsplit(".", 1)[-1] for prop in config.property_classes
)
return property_classes, property_names
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 352 |
_determine_function_name_type
|
def
|
function
|
def _determine_function_name_type(node: nodes.FunctionDef, config=None):
"""Determine the name type whose regex the function's name should match.
:param node: A function node.
:param config: Configuration from which to pull additional property classes.
:type config: :class:`optparse.Values`
:returns: One of ('function', 'method', 'attr')
:rtype: str
"""
property_classes, property_names = _get_properties(config)
if not node.is_method():
return "function"
if is_property_setter(node) or is_property_deleter(node):
# If the function is decorated using the prop_method.{setter,getter}
# form, treat it like an attribute as well.
return "attr"
decorators = node.decorators.nodes if node.decorators else []
for decorator in decorators:
# If the function is a property (decorated with @property
# or @abc.abstractproperty), the name type is 'attr'.
if isinstance(decorator, nodes.Name) or (
isinstance(decorator, nodes.Attribute)
and decorator.attrname in property_names
):
inferred = utils.safe_infer(decorator)
if (
inferred
and hasattr(inferred, "qname")
and inferred.qname() in property_classes
):
return "attr"
return "method"
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 362 |
_get_properties
|
ref
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 363 |
is_method
|
ref
|
function
|
if not node.is_method():
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 366 |
is_property_setter
|
ref
|
function
|
if is_property_setter(node) or is_property_deleter(node):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 366 |
is_property_deleter
|
ref
|
function
|
if is_property_setter(node) or is_property_deleter(node):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 379 |
safe_infer
|
ref
|
function
|
inferred = utils.safe_infer(decorator)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 383 |
qname
|
ref
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 389 |
_has_abstract_methods
|
def
|
function
|
def _has_abstract_methods(node):
"""Determine if the given `node` has abstract methods.
The methods should be made abstract by decorating them
with `abc` decorators.
"""
return len(utils.unimplemented_abstract_methods(node)) > 0
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 395 |
unimplemented_abstract_methods
|
ref
|
function
|
return len(utils.unimplemented_abstract_methods(node)) > 0
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 398 |
report_by_type_stats
|
def
|
function
|
def report_by_type_stats(
sect,
stats: LinterStats,
old_stats: Optional[LinterStats],
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 412 |
get_node_count
|
ref
|
function
|
total = stats.get_node_count(node_type)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 415 |
get_undocumented
|
ref
|
function
|
undocumented_node = stats.get_undocumented(node_type)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 419 |
get_bad_names
|
ref
|
function
|
badname_node = stats.get_bad_names(node_type)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 425 |
get_node_count
|
ref
|
function
|
new = stats.get_node_count(node_type)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 426 |
get_node_count
|
ref
|
function
|
old = old_stats.get_node_count(node_type) if old_stats else None
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 427 |
diff_string
|
ref
|
function
|
diff_str = lint_utils.diff_string(old, new) if old else None
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 436 |
Table
|
ref
|
function
|
sect.append(reporter_nodes.Table(children=lines, cols=6, rheaders=1))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 439 |
redefined_by_decorator
|
def
|
function
|
def redefined_by_decorator(node):
"""Return _True if the object is a method redefined via decorator.
For example:
@property
def x(self): return self._x
@x.setter
def x(self, value): self._x = value
"""
if node.decorators:
for decorator in node.decorators.nodes:
if (
isinstance(decorator, nodes.Attribute)
and getattr(decorator.expr, "name", None) == node.name
):
return _True
return _False
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 458 |
_BasicChecker
|
def
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 463 |
BasicErrorChecker
|
def
|
class
|
visit_classdef _too_many_starred_for_tuple visit_assign visit_starred visit_functiondef _check_name_used_prior_global _check_nonlocal_and_global visit_return visit_yield visit_yieldfrom visit_continue visit_break visit_for visit_while visit_unaryop _check_nonlocal_without_binding visit_nonlocal visit_call _check_inferred_class_is_abstract _check_yield_outside_func _check_else_on_loop _check_in_loop _check_redefinition
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 573 |
check_messages
|
ref
|
function
|
@utils.check_messages("function-redefined")
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 574 |
visit_classdef
|
def
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 575 |
_check_redefinition
|
ref
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 577 |
_too_many_starred_for_tuple
|
def
|
function
|
def _too_many_starred_for_tuple(self, assign_tuple):
starred_count = 0
for elem in assign_tuple.itered():
if isinstance(elem, nodes.Tuple):
return self._too_many_starred_for_tuple(elem)
if isinstance(elem, nodes.Starred):
starred_count += 1
return starred_count > 1
@utils.check_messages("too-many-star-expressions", "invalid-star-assignment-target")
def visit_assign(self, node: nodes.Assign) -> None:
# Check *a, *b = ...
assign_target = node.targets[0]
# Check *a = b
if isinstance(node.targets[0], nodes.Starred):
self.add_message("invalid-star-assignment-target", node=node)
if not isinstance(assign_target, nodes.Tuple):
return
if self._too_many_starred_for_tuple(assign_target):
self.add_message("too-many-star-expressions", node=node)
@utils.check_messages("star-needs-assignment-target")
def visit_starred(self, node: nodes.Starred) -> None:
"""Check that a Starred expression is used in an assignment target."""
if isinstance(node.parent, nodes.Call):
# f(*args) is converted to Call(args=[Starred]), so ignore
# them for this check.
return
if isinstance(node.parent, (nodes.List, nodes.Tuple, nodes.Set, nodes.Dict)):
# PEP 448 unpacking.
return
stmt = node.statement(future=_True)
if not isinstance(stmt, nodes.Assign):
return
if stmt.value is node or stmt.value.parent_of(node):
self.add_message("star-needs-assignment-target", node=node)
@utils.check_messages(
"init-is-generator",
"return-in-init",
"function-redefined",
"return-arg-in-generator",
"duplicate-argument-name",
"nonlocal-and-global",
"used-prior-global-declaration",
)
def visit_functiondef(self, node: nodes.FunctionDef) -> None:
self._check_nonlocal_and_global(node)
self._check_name_used_prior_global(node)
if not redefined_by_decorator(
node
) and not utils.is_registered_in_singledispatch_function(node):
self._check_redefinition(node.is_method() and "method" or "function", node)
# checks for max returns, branch, return in __init__
returns = node.nodes_of_class(
nodes.Return, skip_klass=(nodes.FunctionDef, nodes.ClassDef)
)
if node.is_method() and node.name == "__init__":
if node.is_generator():
self.add_message("init-is-generator", node=node)
else:
values = [r.value for r in returns]
# Are we returning anything but None from constructors
if any(v for v in values if not utils.is_none(v)):
self.add_message("return-in-init", node=node)
# Check for duplicate names by clustering args with same name for detailed report
arg_clusters = collections.defaultdict(list)
arguments: Iterator[Any] = filter(None, [node.args.args, node.args.kwonlyargs])
for arg in itertools.chain.from_iterable(arguments):
arg_clusters[arg.name].append(arg)
# provide detailed report about each repeated argument
for argument_duplicates in arg_clusters.values():
if len(argument_duplicates) != 1:
for argument in argument_duplicates:
self.add_message(
"duplicate-argument-name",
line=argument.lineno,
node=argument,
args=(argument.name,),
)
visit_asyncfunctiondef = visit_functiondef
def _check_name_used_prior_global(self, node):
scope_globals = {
name: child
for child in node.nodes_of_class(nodes.Global)
for name in child.names
if child.scope() is node
}
if not scope_globals:
return
for node_name in node.nodes_of_class(nodes.Name):
if node_name.scope() is not node:
continue
name = node_name.name
corresponding_global = scope_globals.get(name)
if not corresponding_global:
continue
global_lineno = corresponding_global.fromlineno
if global_lineno and global_lineno > node_name.fromlineno:
self.add_message(
"used-prior-global-declaration", node=node_name, args=(name,)
)
def _check_nonlocal_and_global(self, node):
"""Check that a name is both nonlocal and global."""
def same_scope(current):
return current.scope() is node
from_iter = itertools.chain.from_iterable
nonlocals = set(
from_iter(
child.names
for child in node.nodes_of_class(nodes.Nonlocal)
if same_scope(child)
)
)
if not nonlocals:
return
global_vars = set(
from_iter(
child.names
for child in node.nodes_of_class(nodes.Global)
if same_scope(child)
)
)
for name in nonlocals.intersection(global_vars):
self.add_message("nonlocal-and-global", args=(name,), node=node)
@utils.check_messages("return-outside-function")
def visit_return(self, node: nodes.Return) -> None:
if not isinstance(node.frame(future=_True), nodes.FunctionDef):
self.add_message("return-outside-function", node=node)
@utils.check_messages("yield-outside-function")
def visit_yield(self, node: nodes.Yield) -> None:
self._check_yield_outside_func(node)
@utils.check_messages("yield-outside-function")
def visit_yieldfrom(self, node: nodes.YieldFrom) -> None:
self._check_yield_outside_func(node)
@utils.check_messages("not-in-loop", "continue-in-finally")
def visit_continue(self, node: nodes.Continue) -> None:
self._check_in_loop(node, "continue")
@utils.check_messages("not-in-loop")
def visit_break(self, node: nodes.Break) -> None:
self._check_in_loop(node, "break")
@utils.check_messages("useless-else-on-loop")
def visit_for(self, node: nodes.For) -> None:
self._check_else_on_loop(node)
@utils.check_messages("useless-else-on-loop")
def visit_while(self, node: nodes.While) -> None:
self._check_else_on_loop(node)
@utils.check_messages("nonexistent-operator")
def visit_unaryop(self, node: nodes.UnaryOp) -> None:
"""Check use of the non-existent ++ and -- operators."""
if (
(node.op in "+-")
and isinstance(node.operand, nodes.UnaryOp)
and (node.operand.op == node.op)
):
self.add_message("nonexistent-operator", node=node, args=node.op * 2)
def _check_nonlocal_without_binding(self, node, name):
current_scope = node.scope()
while _True:
if current_scope.parent is None:
break
if not isinstance(current_scope, (nodes.ClassDef, nodes.FunctionDef)):
self.add_message("nonlocal-without-binding", args=(name,), node=node)
return
if name not in current_scope.locals:
current_scope = current_scope.parent.scope()
continue
# Okay, found it.
return
if not isinstance(current_scope, nodes.FunctionDef):
self.add_message("nonlocal-without-binding", args=(name,), node=node)
@utils.check_messages("nonlocal-without-binding")
def visit_nonlocal(self, node: nodes.Nonlocal) -> None:
for name in node.names:
self._check_nonlocal_without_binding(node, name)
@utils.check_messages("abstract-class-instantiated")
def visit_call(self, node: nodes.Call) -> None:
"""Check instantiating abstract class with
abc.ABCMeta as metaclass.
"""
for inferred in infer_all(node.func):
self._check_inferred_class_is_abstract(inferred, node)
def _check_inferred_class_is_abstract(self, inferred, node):
if not isinstance(inferred, nodes.ClassDef):
return
klass = utils.node_frame_class(node)
if klass is inferred:
# Don't emit the warning if the class is instantiated
# in its own body or if the call is not an instance
# creation. If the class is instantiated into its own
# body, we're expecting that it knows what it is doing.
return
# __init__ was called
abstract_methods = _has_abstract_methods(inferred)
if not abstract_methods:
return
metaclass = inferred.metaclass()
if metaclass is None:
# Python 3.4 has `abc.ABC`, which won't be detected
# by ClassNode.metaclass()
for ancestor in inferred.ancestors():
if ancestor.qname() == "abc.ABC":
self.add_message(
"abstract-class-instantiated", args=(inferred.name,), node=node
)
break
return
if metaclass.qname() in ABC_METACLASSES:
self.add_message(
"abstract-class-instantiated", args=(inferred.name,), node=node
)
def _check_yield_outside_func(self, node):
if not isinstance(node.frame(future=_True), (nodes.FunctionDef, nodes.Lambda)):
self.add_message("yield-outside-function", node=node)
def _check_else_on_loop(self, node):
"""Check that any loop with an else clause has a break statement."""
if node.orelse and not _loop_exits_early(node):
self.add_message(
"useless-else-on-loop",
node=node,
# This is not optimal, but the line previous
# to the first statement in the else clause
# will usually be the one that contains the else:.
line=node.orelse[0].lineno - 1,
)
def _check_in_loop(self, node, node_name):
"""Check that a node is inside a for or while loop."""
for parent in node.node_ancestors():
if isinstance(parent, (nodes.For, nodes.While)):
if node not in parent.orelse:
return
if isinstance(parent, (nodes.ClassDef, nodes.FunctionDef)):
break
if (
isinstance(parent, nodes.TryFinally)
and node in parent.finalbody
and isinstance(node, nodes.Continue)
):
self.add_message("continue-in-finally", node=node)
self.add_message("not-in-loop", node=node, args=node_name)
def _check_redefinition(self, redeftype, node):
"""Check for redefinition of a function / method / class name."""
parent_frame = node.parent.frame(future=_True)
# Ignore function stubs created for type information
redefinitions = [
i
for i in parent_frame.locals[node.name]
if not (isinstance(i.parent, nodes.AnnAssign) and i.parent.simple)
]
defined_self = next(
(local for local in redefinitions if not utils.is_overload_stub(local)),
node,
)
if defined_self is not node and not astroid.are_exclusive(node, defined_self):
# Additional checks for methods which are not considered
# redefined, since they are already part of the base API.
if (
isinstance(parent_frame, nodes.ClassDef)
and node.name in REDEFINABLE_METHODS
):
return
# Skip typing.overload() functions.
if utils.is_overload_stub(node):
return
# Exempt functions redefined on a condition.
if isinstance(node.parent, nodes.If):
# Exempt "if not <func>" cases
if (
isinstance(node.parent.test, nodes.UnaryOp)
and node.parent.test.op == "not"
and isinstance(node.parent.test.operand, nodes.Name)
and node.parent.test.operand.name == node.name
):
return
# Exempt "if <func> is not None" cases
# pylint: disable=too-many-boolean-expressions
if (
isinstance(node.parent.test, nodes.Compare)
and isinstance(node.parent.test.left, nodes.Name)
and node.parent.test.left.name == node.name
and node.parent.test.ops[0][0] == "is"
and isinstance(node.parent.test.ops[0][1], nodes.Const)
and node.parent.test.ops[0][1].value is None
):
return
# Check if we have forward references for this node.
try:
redefinition_index = redefinitions.index(node)
except ValueError:
pass
else:
for redefinition in redefinitions[:redefinition_index]:
inferred = utils.safe_infer(redefinition)
if (
inferred
and isinstance(inferred, astroid.Instance)
and inferred.qname() == TYPING_FORWARD_REF_QNAME
):
return
dummy_variables_rgx = lint_utils.get_global_option(
self, "dummy-variables-rgx", default=None
)
if dummy_variables_rgx and dummy_variables_rgx.match(node.name):
return
self.add_message(
"function-redefined",
node=node,
args=(redeftype, defined_self.fromlineno),
)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 579 |
itered
|
ref
|
function
|
for elem in assign_tuple.itered():
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 581 |
_too_many_starred_for_tuple
|
ref
|
function
|
return self._too_many_starred_for_tuple(elem)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 586 |
check_messages
|
ref
|
function
|
@utils.check_messages("too-many-star-expressions", "invalid-star-assignment-target")
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 587 |
visit_assign
|
def
|
function
|
def visit_assign(self, node: nodes.Assign) -> None:
# Check *a, *b = ...
assign_target = node.targets[0]
# Check *a = b
if isinstance(node.targets[0], nodes.Starred):
self.add_message("invalid-star-assignment-target", node=node)
if not isinstance(assign_target, nodes.Tuple):
return
if self._too_many_starred_for_tuple(assign_target):
self.add_message("too-many-star-expressions", node=node)
@utils.check_messages("star-needs-assignment-target")
def visit_starred(self, node: nodes.Starred) -> None:
"""Check that a Starred expression is used in an assignment target."""
if isinstance(node.parent, nodes.Call):
# f(*args) is converted to Call(args=[Starred]), so ignore
# them for this check.
return
if isinstance(node.parent, (nodes.List, nodes.Tuple, nodes.Set, nodes.Dict)):
# PEP 448 unpacking.
return
stmt = node.statement(future=_True)
if not isinstance(stmt, nodes.Assign):
return
if stmt.value is node or stmt.value.parent_of(node):
self.add_message("star-needs-assignment-target", node=node)
@utils.check_messages(
"init-is-generator",
"return-in-init",
"function-redefined",
"return-arg-in-generator",
"duplicate-argument-name",
"nonlocal-and-global",
"used-prior-global-declaration",
)
def visit_functiondef(self, node: nodes.FunctionDef) -> None:
self._check_nonlocal_and_global(node)
self._check_name_used_prior_global(node)
if not redefined_by_decorator(
node
) and not utils.is_registered_in_singledispatch_function(node):
self._check_redefinition(node.is_method() and "method" or "function", node)
# checks for max returns, branch, return in __init__
returns = node.nodes_of_class(
nodes.Return, skip_klass=(nodes.FunctionDef, nodes.ClassDef)
)
if node.is_method() and node.name == "__init__":
if node.is_generator():
self.add_message("init-is-generator", node=node)
else:
values = [r.value for r in returns]
# Are we returning anything but None from constructors
if any(v for v in values if not utils.is_none(v)):
self.add_message("return-in-init", node=node)
# Check for duplicate names by clustering args with same name for detailed report
arg_clusters = collections.defaultdict(list)
arguments: Iterator[Any] = filter(None, [node.args.args, node.args.kwonlyargs])
for arg in itertools.chain.from_iterable(arguments):
arg_clusters[arg.name].append(arg)
# provide detailed report about each repeated argument
for argument_duplicates in arg_clusters.values():
if len(argument_duplicates) != 1:
for argument in argument_duplicates:
self.add_message(
"duplicate-argument-name",
line=argument.lineno,
node=argument,
args=(argument.name,),
)
visit_asyncfunctiondef = visit_functiondef
def _check_name_used_prior_global(self, node):
scope_globals = {
name: child
for child in node.nodes_of_class(nodes.Global)
for name in child.names
if child.scope() is node
}
if not scope_globals:
return
for node_name in node.nodes_of_class(nodes.Name):
if node_name.scope() is not node:
continue
name = node_name.name
corresponding_global = scope_globals.get(name)
if not corresponding_global:
continue
global_lineno = corresponding_global.fromlineno
if global_lineno and global_lineno > node_name.fromlineno:
self.add_message(
"used-prior-global-declaration", node=node_name, args=(name,)
)
def _check_nonlocal_and_global(self, node):
"""Check that a name is both nonlocal and global."""
def same_scope(current):
return current.scope() is node
from_iter = itertools.chain.from_iterable
nonlocals = set(
from_iter(
child.names
for child in node.nodes_of_class(nodes.Nonlocal)
if same_scope(child)
)
)
if not nonlocals:
return
global_vars = set(
from_iter(
child.names
for child in node.nodes_of_class(nodes.Global)
if same_scope(child)
)
)
for name in nonlocals.intersection(global_vars):
self.add_message("nonlocal-and-global", args=(name,), node=node)
@utils.check_messages("return-outside-function")
def visit_return(self, node: nodes.Return) -> None:
if not isinstance(node.frame(future=_True), nodes.FunctionDef):
self.add_message("return-outside-function", node=node)
@utils.check_messages("yield-outside-function")
def visit_yield(self, node: nodes.Yield) -> None:
self._check_yield_outside_func(node)
@utils.check_messages("yield-outside-function")
def visit_yieldfrom(self, node: nodes.YieldFrom) -> None:
self._check_yield_outside_func(node)
@utils.check_messages("not-in-loop", "continue-in-finally")
def visit_continue(self, node: nodes.Continue) -> None:
self._check_in_loop(node, "continue")
@utils.check_messages("not-in-loop")
def visit_break(self, node: nodes.Break) -> None:
self._check_in_loop(node, "break")
@utils.check_messages("useless-else-on-loop")
def visit_for(self, node: nodes.For) -> None:
self._check_else_on_loop(node)
@utils.check_messages("useless-else-on-loop")
def visit_while(self, node: nodes.While) -> None:
self._check_else_on_loop(node)
@utils.check_messages("nonexistent-operator")
def visit_unaryop(self, node: nodes.UnaryOp) -> None:
"""Check use of the non-existent ++ and -- operators."""
if (
(node.op in "+-")
and isinstance(node.operand, nodes.UnaryOp)
and (node.operand.op == node.op)
):
self.add_message("nonexistent-operator", node=node, args=node.op * 2)
def _check_nonlocal_without_binding(self, node, name):
current_scope = node.scope()
while _True:
if current_scope.parent is None:
break
if not isinstance(current_scope, (nodes.ClassDef, nodes.FunctionDef)):
self.add_message("nonlocal-without-binding", args=(name,), node=node)
return
if name not in current_scope.locals:
current_scope = current_scope.parent.scope()
continue
# Okay, found it.
return
if not isinstance(current_scope, nodes.FunctionDef):
self.add_message("nonlocal-without-binding", args=(name,), node=node)
@utils.check_messages("nonlocal-without-binding")
def visit_nonlocal(self, node: nodes.Nonlocal) -> None:
for name in node.names:
self._check_nonlocal_without_binding(node, name)
@utils.check_messages("abstract-class-instantiated")
def visit_call(self, node: nodes.Call) -> None:
"""Check instantiating abstract class with
abc.ABCMeta as metaclass.
"""
for inferred in infer_all(node.func):
self._check_inferred_class_is_abstract(inferred, node)
def _check_inferred_class_is_abstract(self, inferred, node):
if not isinstance(inferred, nodes.ClassDef):
return
klass = utils.node_frame_class(node)
if klass is inferred:
# Don't emit the warning if the class is instantiated
# in its own body or if the call is not an instance
# creation. If the class is instantiated into its own
# body, we're expecting that it knows what it is doing.
return
# __init__ was called
abstract_methods = _has_abstract_methods(inferred)
if not abstract_methods:
return
metaclass = inferred.metaclass()
if metaclass is None:
# Python 3.4 has `abc.ABC`, which won't be detected
# by ClassNode.metaclass()
for ancestor in inferred.ancestors():
if ancestor.qname() == "abc.ABC":
self.add_message(
"abstract-class-instantiated", args=(inferred.name,), node=node
)
break
return
if metaclass.qname() in ABC_METACLASSES:
self.add_message(
"abstract-class-instantiated", args=(inferred.name,), node=node
)
def _check_yield_outside_func(self, node):
if not isinstance(node.frame(future=_True), (nodes.FunctionDef, nodes.Lambda)):
self.add_message("yield-outside-function", node=node)
def _check_else_on_loop(self, node):
"""Check that any loop with an else clause has a break statement."""
if node.orelse and not _loop_exits_early(node):
self.add_message(
"useless-else-on-loop",
node=node,
# This is not optimal, but the line previous
# to the first statement in the else clause
# will usually be the one that contains the else:.
line=node.orelse[0].lineno - 1,
)
def _check_in_loop(self, node, node_name):
"""Check that a node is inside a for or while loop."""
for parent in node.node_ancestors():
if isinstance(parent, (nodes.For, nodes.While)):
if node not in parent.orelse:
return
if isinstance(parent, (nodes.ClassDef, nodes.FunctionDef)):
break
if (
isinstance(parent, nodes.TryFinally)
and node in parent.finalbody
and isinstance(node, nodes.Continue)
):
self.add_message("continue-in-finally", node=node)
self.add_message("not-in-loop", node=node, args=node_name)
def _check_redefinition(self, redeftype, node):
"""Check for redefinition of a function / method / class name."""
parent_frame = node.parent.frame(future=_True)
# Ignore function stubs created for type information
redefinitions = [
i
for i in parent_frame.locals[node.name]
if not (isinstance(i.parent, nodes.AnnAssign) and i.parent.simple)
]
defined_self = next(
(local for local in redefinitions if not utils.is_overload_stub(local)),
node,
)
if defined_self is not node and not astroid.are_exclusive(node, defined_self):
# Additional checks for methods which are not considered
# redefined, since they are already part of the base API.
if (
isinstance(parent_frame, nodes.ClassDef)
and node.name in REDEFINABLE_METHODS
):
return
# Skip typing.overload() functions.
if utils.is_overload_stub(node):
return
# Exempt functions redefined on a condition.
if isinstance(node.parent, nodes.If):
# Exempt "if not <func>" cases
if (
isinstance(node.parent.test, nodes.UnaryOp)
and node.parent.test.op == "not"
and isinstance(node.parent.test.operand, nodes.Name)
and node.parent.test.operand.name == node.name
):
return
# Exempt "if <func> is not None" cases
# pylint: disable=too-many-boolean-expressions
if (
isinstance(node.parent.test, nodes.Compare)
and isinstance(node.parent.test.left, nodes.Name)
and node.parent.test.left.name == node.name
and node.parent.test.ops[0][0] == "is"
and isinstance(node.parent.test.ops[0][1], nodes.Const)
and node.parent.test.ops[0][1].value is None
):
return
# Check if we have forward references for this node.
try:
redefinition_index = redefinitions.index(node)
except ValueError:
pass
else:
for redefinition in redefinitions[:redefinition_index]:
inferred = utils.safe_infer(redefinition)
if (
inferred
and isinstance(inferred, astroid.Instance)
and inferred.qname() == TYPING_FORWARD_REF_QNAME
):
return
dummy_variables_rgx = lint_utils.get_global_option(
self, "dummy-variables-rgx", default=None
)
if dummy_variables_rgx and dummy_variables_rgx.match(node.name):
return
self.add_message(
"function-redefined",
node=node,
args=(redeftype, defined_self.fromlineno),
)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 592 |
add_message
|
ref
|
function
|
self.add_message("invalid-star-assignment-target", node=node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 596 |
_too_many_starred_for_tuple
|
ref
|
function
|
if self._too_many_starred_for_tuple(assign_target):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 597 |
add_message
|
ref
|
function
|
self.add_message("too-many-star-expressions", node=node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 599 |
check_messages
|
ref
|
function
|
@utils.check_messages("star-needs-assignment-target")
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 600 |
visit_starred
|
def
|
function
|
def visit_starred(self, node: nodes.Starred) -> None:
"""Check that a Starred expression is used in an assignment target."""
if isinstance(node.parent, nodes.Call):
# f(*args) is converted to Call(args=[Starred]), so ignore
# them for this check.
return
if isinstance(node.parent, (nodes.List, nodes.Tuple, nodes.Set, nodes.Dict)):
# PEP 448 unpacking.
return
stmt = node.statement(future=_True)
if not isinstance(stmt, nodes.Assign):
return
if stmt.value is node or stmt.value.parent_of(node):
self.add_message("star-needs-assignment-target", node=node)
@utils.check_messages(
"init-is-generator",
"return-in-init",
"function-redefined",
"return-arg-in-generator",
"duplicate-argument-name",
"nonlocal-and-global",
"used-prior-global-declaration",
)
def visit_functiondef(self, node: nodes.FunctionDef) -> None:
self._check_nonlocal_and_global(node)
self._check_name_used_prior_global(node)
if not redefined_by_decorator(
node
) and not utils.is_registered_in_singledispatch_function(node):
self._check_redefinition(node.is_method() and "method" or "function", node)
# checks for max returns, branch, return in __init__
returns = node.nodes_of_class(
nodes.Return, skip_klass=(nodes.FunctionDef, nodes.ClassDef)
)
if node.is_method() and node.name == "__init__":
if node.is_generator():
self.add_message("init-is-generator", node=node)
else:
values = [r.value for r in returns]
# Are we returning anything but None from constructors
if any(v for v in values if not utils.is_none(v)):
self.add_message("return-in-init", node=node)
# Check for duplicate names by clustering args with same name for detailed report
arg_clusters = collections.defaultdict(list)
arguments: Iterator[Any] = filter(None, [node.args.args, node.args.kwonlyargs])
for arg in itertools.chain.from_iterable(arguments):
arg_clusters[arg.name].append(arg)
# provide detailed report about each repeated argument
for argument_duplicates in arg_clusters.values():
if len(argument_duplicates) != 1:
for argument in argument_duplicates:
self.add_message(
"duplicate-argument-name",
line=argument.lineno,
node=argument,
args=(argument.name,),
)
visit_asyncfunctiondef = visit_functiondef
def _check_name_used_prior_global(self, node):
scope_globals = {
name: child
for child in node.nodes_of_class(nodes.Global)
for name in child.names
if child.scope() is node
}
if not scope_globals:
return
for node_name in node.nodes_of_class(nodes.Name):
if node_name.scope() is not node:
continue
name = node_name.name
corresponding_global = scope_globals.get(name)
if not corresponding_global:
continue
global_lineno = corresponding_global.fromlineno
if global_lineno and global_lineno > node_name.fromlineno:
self.add_message(
"used-prior-global-declaration", node=node_name, args=(name,)
)
def _check_nonlocal_and_global(self, node):
"""Check that a name is both nonlocal and global."""
def same_scope(current):
return current.scope() is node
from_iter = itertools.chain.from_iterable
nonlocals = set(
from_iter(
child.names
for child in node.nodes_of_class(nodes.Nonlocal)
if same_scope(child)
)
)
if not nonlocals:
return
global_vars = set(
from_iter(
child.names
for child in node.nodes_of_class(nodes.Global)
if same_scope(child)
)
)
for name in nonlocals.intersection(global_vars):
self.add_message("nonlocal-and-global", args=(name,), node=node)
@utils.check_messages("return-outside-function")
def visit_return(self, node: nodes.Return) -> None:
if not isinstance(node.frame(future=_True), nodes.FunctionDef):
self.add_message("return-outside-function", node=node)
@utils.check_messages("yield-outside-function")
def visit_yield(self, node: nodes.Yield) -> None:
self._check_yield_outside_func(node)
@utils.check_messages("yield-outside-function")
def visit_yieldfrom(self, node: nodes.YieldFrom) -> None:
self._check_yield_outside_func(node)
@utils.check_messages("not-in-loop", "continue-in-finally")
def visit_continue(self, node: nodes.Continue) -> None:
self._check_in_loop(node, "continue")
@utils.check_messages("not-in-loop")
def visit_break(self, node: nodes.Break) -> None:
self._check_in_loop(node, "break")
@utils.check_messages("useless-else-on-loop")
def visit_for(self, node: nodes.For) -> None:
self._check_else_on_loop(node)
@utils.check_messages("useless-else-on-loop")
def visit_while(self, node: nodes.While) -> None:
self._check_else_on_loop(node)
@utils.check_messages("nonexistent-operator")
def visit_unaryop(self, node: nodes.UnaryOp) -> None:
"""Check use of the non-existent ++ and -- operators."""
if (
(node.op in "+-")
and isinstance(node.operand, nodes.UnaryOp)
and (node.operand.op == node.op)
):
self.add_message("nonexistent-operator", node=node, args=node.op * 2)
def _check_nonlocal_without_binding(self, node, name):
current_scope = node.scope()
while _True:
if current_scope.parent is None:
break
if not isinstance(current_scope, (nodes.ClassDef, nodes.FunctionDef)):
self.add_message("nonlocal-without-binding", args=(name,), node=node)
return
if name not in current_scope.locals:
current_scope = current_scope.parent.scope()
continue
# Okay, found it.
return
if not isinstance(current_scope, nodes.FunctionDef):
self.add_message("nonlocal-without-binding", args=(name,), node=node)
@utils.check_messages("nonlocal-without-binding")
def visit_nonlocal(self, node: nodes.Nonlocal) -> None:
for name in node.names:
self._check_nonlocal_without_binding(node, name)
@utils.check_messages("abstract-class-instantiated")
def visit_call(self, node: nodes.Call) -> None:
"""Check instantiating abstract class with
abc.ABCMeta as metaclass.
"""
for inferred in infer_all(node.func):
self._check_inferred_class_is_abstract(inferred, node)
def _check_inferred_class_is_abstract(self, inferred, node):
if not isinstance(inferred, nodes.ClassDef):
return
klass = utils.node_frame_class(node)
if klass is inferred:
# Don't emit the warning if the class is instantiated
# in its own body or if the call is not an instance
# creation. If the class is instantiated into its own
# body, we're expecting that it knows what it is doing.
return
# __init__ was called
abstract_methods = _has_abstract_methods(inferred)
if not abstract_methods:
return
metaclass = inferred.metaclass()
if metaclass is None:
# Python 3.4 has `abc.ABC`, which won't be detected
# by ClassNode.metaclass()
for ancestor in inferred.ancestors():
if ancestor.qname() == "abc.ABC":
self.add_message(
"abstract-class-instantiated", args=(inferred.name,), node=node
)
break
return
if metaclass.qname() in ABC_METACLASSES:
self.add_message(
"abstract-class-instantiated", args=(inferred.name,), node=node
)
def _check_yield_outside_func(self, node):
if not isinstance(node.frame(future=_True), (nodes.FunctionDef, nodes.Lambda)):
self.add_message("yield-outside-function", node=node)
def _check_else_on_loop(self, node):
"""Check that any loop with an else clause has a break statement."""
if node.orelse and not _loop_exits_early(node):
self.add_message(
"useless-else-on-loop",
node=node,
# This is not optimal, but the line previous
# to the first statement in the else clause
# will usually be the one that contains the else:.
line=node.orelse[0].lineno - 1,
)
def _check_in_loop(self, node, node_name):
"""Check that a node is inside a for or while loop."""
for parent in node.node_ancestors():
if isinstance(parent, (nodes.For, nodes.While)):
if node not in parent.orelse:
return
if isinstance(parent, (nodes.ClassDef, nodes.FunctionDef)):
break
if (
isinstance(parent, nodes.TryFinally)
and node in parent.finalbody
and isinstance(node, nodes.Continue)
):
self.add_message("continue-in-finally", node=node)
self.add_message("not-in-loop", node=node, args=node_name)
def _check_redefinition(self, redeftype, node):
"""Check for redefinition of a function / method / class name."""
parent_frame = node.parent.frame(future=_True)
# Ignore function stubs created for type information
redefinitions = [
i
for i in parent_frame.locals[node.name]
if not (isinstance(i.parent, nodes.AnnAssign) and i.parent.simple)
]
defined_self = next(
(local for local in redefinitions if not utils.is_overload_stub(local)),
node,
)
if defined_self is not node and not astroid.are_exclusive(node, defined_self):
# Additional checks for methods which are not considered
# redefined, since they are already part of the base API.
if (
isinstance(parent_frame, nodes.ClassDef)
and node.name in REDEFINABLE_METHODS
):
return
# Skip typing.overload() functions.
if utils.is_overload_stub(node):
return
# Exempt functions redefined on a condition.
if isinstance(node.parent, nodes.If):
# Exempt "if not <func>" cases
if (
isinstance(node.parent.test, nodes.UnaryOp)
and node.parent.test.op == "not"
and isinstance(node.parent.test.operand, nodes.Name)
and node.parent.test.operand.name == node.name
):
return
# Exempt "if <func> is not None" cases
# pylint: disable=too-many-boolean-expressions
if (
isinstance(node.parent.test, nodes.Compare)
and isinstance(node.parent.test.left, nodes.Name)
and node.parent.test.left.name == node.name
and node.parent.test.ops[0][0] == "is"
and isinstance(node.parent.test.ops[0][1], nodes.Const)
and node.parent.test.ops[0][1].value is None
):
return
# Check if we have forward references for this node.
try:
redefinition_index = redefinitions.index(node)
except ValueError:
pass
else:
for redefinition in redefinitions[:redefinition_index]:
inferred = utils.safe_infer(redefinition)
if (
inferred
and isinstance(inferred, astroid.Instance)
and inferred.qname() == TYPING_FORWARD_REF_QNAME
):
return
dummy_variables_rgx = lint_utils.get_global_option(
self, "dummy-variables-rgx", default=None
)
if dummy_variables_rgx and dummy_variables_rgx.match(node.name):
return
self.add_message(
"function-redefined",
node=node,
args=(redeftype, defined_self.fromlineno),
)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 610 |
statement
|
ref
|
function
|
stmt = node.statement(future=True)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 614 |
parent_of
|
ref
|
function
|
if stmt.value is node or stmt.value.parent_of(node):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 615 |
add_message
|
ref
|
function
|
self.add_message("star-needs-assignment-target", node=node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 617 |
check_messages
|
ref
|
function
|
@utils.check_messages(
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 626 |
visit_functiondef
|
def
|
function
|
def visit_functiondef(self, node: nodes.FunctionDef) -> None:
self._check_nonlocal_and_global(node)
self._check_name_used_prior_global(node)
if not redefined_by_decorator(
node
) and not utils.is_registered_in_singledispatch_function(node):
self._check_redefinition(node.is_method() and "method" or "function", node)
# checks for max returns, branch, return in __init__
returns = node.nodes_of_class(
nodes.Return, skip_klass=(nodes.FunctionDef, nodes.ClassDef)
)
if node.is_method() and node.name == "__init__":
if node.is_generator():
self.add_message("init-is-generator", node=node)
else:
values = [r.value for r in returns]
# Are we returning anything but None from constructors
if any(v for v in values if not utils.is_none(v)):
self.add_message("return-in-init", node=node)
# Check for duplicate names by clustering args with same name for detailed report
arg_clusters = collections.defaultdict(list)
arguments: Iterator[Any] = filter(None, [node.args.args, node.args.kwonlyargs])
for arg in itertools.chain.from_iterable(arguments):
arg_clusters[arg.name].append(arg)
# provide detailed report about each repeated argument
for argument_duplicates in arg_clusters.values():
if len(argument_duplicates) != 1:
for argument in argument_duplicates:
self.add_message(
"duplicate-argument-name",
line=argument.lineno,
node=argument,
args=(argument.name,),
)
visit_asyncfunctiondef = visit_functiondef
def _check_name_used_prior_global(self, node):
scope_globals = {
name: child
for child in node.nodes_of_class(nodes.Global)
for name in child.names
if child.scope() is node
}
if not scope_globals:
return
for node_name in node.nodes_of_class(nodes.Name):
if node_name.scope() is not node:
continue
name = node_name.name
corresponding_global = scope_globals.get(name)
if not corresponding_global:
continue
global_lineno = corresponding_global.fromlineno
if global_lineno and global_lineno > node_name.fromlineno:
self.add_message(
"used-prior-global-declaration", node=node_name, args=(name,)
)
def _check_nonlocal_and_global(self, node):
"""Check that a name is both nonlocal and global."""
def same_scope(current):
return current.scope() is node
from_iter = itertools.chain.from_iterable
nonlocals = set(
from_iter(
child.names
for child in node.nodes_of_class(nodes.Nonlocal)
if same_scope(child)
)
)
if not nonlocals:
return
global_vars = set(
from_iter(
child.names
for child in node.nodes_of_class(nodes.Global)
if same_scope(child)
)
)
for name in nonlocals.intersection(global_vars):
self.add_message("nonlocal-and-global", args=(name,), node=node)
@utils.check_messages("return-outside-function")
def visit_return(self, node: nodes.Return) -> None:
if not isinstance(node.frame(future=_True), nodes.FunctionDef):
self.add_message("return-outside-function", node=node)
@utils.check_messages("yield-outside-function")
def visit_yield(self, node: nodes.Yield) -> None:
self._check_yield_outside_func(node)
@utils.check_messages("yield-outside-function")
def visit_yieldfrom(self, node: nodes.YieldFrom) -> None:
self._check_yield_outside_func(node)
@utils.check_messages("not-in-loop", "continue-in-finally")
def visit_continue(self, node: nodes.Continue) -> None:
self._check_in_loop(node, "continue")
@utils.check_messages("not-in-loop")
def visit_break(self, node: nodes.Break) -> None:
self._check_in_loop(node, "break")
@utils.check_messages("useless-else-on-loop")
def visit_for(self, node: nodes.For) -> None:
self._check_else_on_loop(node)
@utils.check_messages("useless-else-on-loop")
def visit_while(self, node: nodes.While) -> None:
self._check_else_on_loop(node)
@utils.check_messages("nonexistent-operator")
def visit_unaryop(self, node: nodes.UnaryOp) -> None:
"""Check use of the non-existent ++ and -- operators."""
if (
(node.op in "+-")
and isinstance(node.operand, nodes.UnaryOp)
and (node.operand.op == node.op)
):
self.add_message("nonexistent-operator", node=node, args=node.op * 2)
def _check_nonlocal_without_binding(self, node, name):
current_scope = node.scope()
while _True:
if current_scope.parent is None:
break
if not isinstance(current_scope, (nodes.ClassDef, nodes.FunctionDef)):
self.add_message("nonlocal-without-binding", args=(name,), node=node)
return
if name not in current_scope.locals:
current_scope = current_scope.parent.scope()
continue
# Okay, found it.
return
if not isinstance(current_scope, nodes.FunctionDef):
self.add_message("nonlocal-without-binding", args=(name,), node=node)
@utils.check_messages("nonlocal-without-binding")
def visit_nonlocal(self, node: nodes.Nonlocal) -> None:
for name in node.names:
self._check_nonlocal_without_binding(node, name)
@utils.check_messages("abstract-class-instantiated")
def visit_call(self, node: nodes.Call) -> None:
"""Check instantiating abstract class with
abc.ABCMeta as metaclass.
"""
for inferred in infer_all(node.func):
self._check_inferred_class_is_abstract(inferred, node)
def _check_inferred_class_is_abstract(self, inferred, node):
if not isinstance(inferred, nodes.ClassDef):
return
klass = utils.node_frame_class(node)
if klass is inferred:
# Don't emit the warning if the class is instantiated
# in its own body or if the call is not an instance
# creation. If the class is instantiated into its own
# body, we're expecting that it knows what it is doing.
return
# __init__ was called
abstract_methods = _has_abstract_methods(inferred)
if not abstract_methods:
return
metaclass = inferred.metaclass()
if metaclass is None:
# Python 3.4 has `abc.ABC`, which won't be detected
# by ClassNode.metaclass()
for ancestor in inferred.ancestors():
if ancestor.qname() == "abc.ABC":
self.add_message(
"abstract-class-instantiated", args=(inferred.name,), node=node
)
break
return
if metaclass.qname() in ABC_METACLASSES:
self.add_message(
"abstract-class-instantiated", args=(inferred.name,), node=node
)
def _check_yield_outside_func(self, node):
if not isinstance(node.frame(future=_True), (nodes.FunctionDef, nodes.Lambda)):
self.add_message("yield-outside-function", node=node)
def _check_else_on_loop(self, node):
"""Check that any loop with an else clause has a break statement."""
if node.orelse and not _loop_exits_early(node):
self.add_message(
"useless-else-on-loop",
node=node,
# This is not optimal, but the line previous
# to the first statement in the else clause
# will usually be the one that contains the else:.
line=node.orelse[0].lineno - 1,
)
def _check_in_loop(self, node, node_name):
"""Check that a node is inside a for or while loop."""
for parent in node.node_ancestors():
if isinstance(parent, (nodes.For, nodes.While)):
if node not in parent.orelse:
return
if isinstance(parent, (nodes.ClassDef, nodes.FunctionDef)):
break
if (
isinstance(parent, nodes.TryFinally)
and node in parent.finalbody
and isinstance(node, nodes.Continue)
):
self.add_message("continue-in-finally", node=node)
self.add_message("not-in-loop", node=node, args=node_name)
def _check_redefinition(self, redeftype, node):
"""Check for redefinition of a function / method / class name."""
parent_frame = node.parent.frame(future=_True)
# Ignore function stubs created for type information
redefinitions = [
i
for i in parent_frame.locals[node.name]
if not (isinstance(i.parent, nodes.AnnAssign) and i.parent.simple)
]
defined_self = next(
(local for local in redefinitions if not utils.is_overload_stub(local)),
node,
)
if defined_self is not node and not astroid.are_exclusive(node, defined_self):
# Additional checks for methods which are not considered
# redefined, since they are already part of the base API.
if (
isinstance(parent_frame, nodes.ClassDef)
and node.name in REDEFINABLE_METHODS
):
return
# Skip typing.overload() functions.
if utils.is_overload_stub(node):
return
# Exempt functions redefined on a condition.
if isinstance(node.parent, nodes.If):
# Exempt "if not <func>" cases
if (
isinstance(node.parent.test, nodes.UnaryOp)
and node.parent.test.op == "not"
and isinstance(node.parent.test.operand, nodes.Name)
and node.parent.test.operand.name == node.name
):
return
# Exempt "if <func> is not None" cases
# pylint: disable=too-many-boolean-expressions
if (
isinstance(node.parent.test, nodes.Compare)
and isinstance(node.parent.test.left, nodes.Name)
and node.parent.test.left.name == node.name
and node.parent.test.ops[0][0] == "is"
and isinstance(node.parent.test.ops[0][1], nodes.Const)
and node.parent.test.ops[0][1].value is None
):
return
# Check if we have forward references for this node.
try:
redefinition_index = redefinitions.index(node)
except ValueError:
pass
else:
for redefinition in redefinitions[:redefinition_index]:
inferred = utils.safe_infer(redefinition)
if (
inferred
and isinstance(inferred, astroid.Instance)
and inferred.qname() == TYPING_FORWARD_REF_QNAME
):
return
dummy_variables_rgx = lint_utils.get_global_option(
self, "dummy-variables-rgx", default=None
)
if dummy_variables_rgx and dummy_variables_rgx.match(node.name):
return
self.add_message(
"function-redefined",
node=node,
args=(redeftype, defined_self.fromlineno),
)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 627 |
_check_nonlocal_and_global
|
ref
|
function
|
self._check_nonlocal_and_global(node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 628 |
_check_name_used_prior_global
|
ref
|
function
|
self._check_name_used_prior_global(node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 629 |
redefined_by_decorator
|
ref
|
function
|
if not redefined_by_decorator(
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 631 |
is_registered_in_singledispatch_function
|
ref
|
function
|
) and not utils.is_registered_in_singledispatch_function(node):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 632 |
_check_redefinition
|
ref
|
function
|
self._check_redefinition(node.is_method() and "method" or "function", node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 632 |
is_method
|
ref
|
function
|
self._check_redefinition(node.is_method() and "method" or "function", node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 634 |
nodes_of_class
|
ref
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 637 |
is_method
|
ref
|
function
|
if node.is_method() and node.name == "__init__":
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 638 |
is_generator
|
ref
|
function
|
if node.is_generator():
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 639 |
add_message
|
ref
|
function
|
self.add_message("init-is-generator", node=node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 643 |
is_none
|
ref
|
function
|
if any(v for v in values if not utils.is_none(v)):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 644 |
add_message
|
ref
|
function
|
self.add_message("return-in-init", node=node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 649 |
from_iterable
|
ref
|
function
|
for arg in itertools.chain.from_iterable(arguments):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 656 |
add_message
|
ref
|
function
|
self.add_message(
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 665 |
_check_name_used_prior_global
|
def
|
function
|
def _check_name_used_prior_global(self, node):
scope_globals = {
name: child
for child in node.nodes_of_class(nodes.Global)
for name in child.names
if child.scope() is node
}
if not scope_globals:
return
for node_name in node.nodes_of_class(nodes.Name):
if node_name.scope() is not node:
continue
name = node_name.name
corresponding_global = scope_globals.get(name)
if not corresponding_global:
continue
global_lineno = corresponding_global.fromlineno
if global_lineno and global_lineno > node_name.fromlineno:
self.add_message(
"used-prior-global-declaration", node=node_name, args=(name,)
)
def _check_nonlocal_and_global(self, node):
"""Check that a name is both nonlocal and global."""
def same_scope(current):
return current.scope() is node
from_iter = itertools.chain.from_iterable
nonlocals = set(
from_iter(
child.names
for child in node.nodes_of_class(nodes.Nonlocal)
if same_scope(child)
)
)
if not nonlocals:
return
global_vars = set(
from_iter(
child.names
for child in node.nodes_of_class(nodes.Global)
if same_scope(child)
)
)
for name in nonlocals.intersection(global_vars):
self.add_message("nonlocal-and-global", args=(name,), node=node)
@utils.check_messages("return-outside-function")
def visit_return(self, node: nodes.Return) -> None:
if not isinstance(node.frame(future=_True), nodes.FunctionDef):
self.add_message("return-outside-function", node=node)
@utils.check_messages("yield-outside-function")
def visit_yield(self, node: nodes.Yield) -> None:
self._check_yield_outside_func(node)
@utils.check_messages("yield-outside-function")
def visit_yieldfrom(self, node: nodes.YieldFrom) -> None:
self._check_yield_outside_func(node)
@utils.check_messages("not-in-loop", "continue-in-finally")
def visit_continue(self, node: nodes.Continue) -> None:
self._check_in_loop(node, "continue")
@utils.check_messages("not-in-loop")
def visit_break(self, node: nodes.Break) -> None:
self._check_in_loop(node, "break")
@utils.check_messages("useless-else-on-loop")
def visit_for(self, node: nodes.For) -> None:
self._check_else_on_loop(node)
@utils.check_messages("useless-else-on-loop")
def visit_while(self, node: nodes.While) -> None:
self._check_else_on_loop(node)
@utils.check_messages("nonexistent-operator")
def visit_unaryop(self, node: nodes.UnaryOp) -> None:
"""Check use of the non-existent ++ and -- operators."""
if (
(node.op in "+-")
and isinstance(node.operand, nodes.UnaryOp)
and (node.operand.op == node.op)
):
self.add_message("nonexistent-operator", node=node, args=node.op * 2)
def _check_nonlocal_without_binding(self, node, name):
current_scope = node.scope()
while _True:
if current_scope.parent is None:
break
if not isinstance(current_scope, (nodes.ClassDef, nodes.FunctionDef)):
self.add_message("nonlocal-without-binding", args=(name,), node=node)
return
if name not in current_scope.locals:
current_scope = current_scope.parent.scope()
continue
# Okay, found it.
return
if not isinstance(current_scope, nodes.FunctionDef):
self.add_message("nonlocal-without-binding", args=(name,), node=node)
@utils.check_messages("nonlocal-without-binding")
def visit_nonlocal(self, node: nodes.Nonlocal) -> None:
for name in node.names:
self._check_nonlocal_without_binding(node, name)
@utils.check_messages("abstract-class-instantiated")
def visit_call(self, node: nodes.Call) -> None:
"""Check instantiating abstract class with
abc.ABCMeta as metaclass.
"""
for inferred in infer_all(node.func):
self._check_inferred_class_is_abstract(inferred, node)
def _check_inferred_class_is_abstract(self, inferred, node):
if not isinstance(inferred, nodes.ClassDef):
return
klass = utils.node_frame_class(node)
if klass is inferred:
# Don't emit the warning if the class is instantiated
# in its own body or if the call is not an instance
# creation. If the class is instantiated into its own
# body, we're expecting that it knows what it is doing.
return
# __init__ was called
abstract_methods = _has_abstract_methods(inferred)
if not abstract_methods:
return
metaclass = inferred.metaclass()
if metaclass is None:
# Python 3.4 has `abc.ABC`, which won't be detected
# by ClassNode.metaclass()
for ancestor in inferred.ancestors():
if ancestor.qname() == "abc.ABC":
self.add_message(
"abstract-class-instantiated", args=(inferred.name,), node=node
)
break
return
if metaclass.qname() in ABC_METACLASSES:
self.add_message(
"abstract-class-instantiated", args=(inferred.name,), node=node
)
def _check_yield_outside_func(self, node):
if not isinstance(node.frame(future=_True), (nodes.FunctionDef, nodes.Lambda)):
self.add_message("yield-outside-function", node=node)
def _check_else_on_loop(self, node):
"""Check that any loop with an else clause has a break statement."""
if node.orelse and not _loop_exits_early(node):
self.add_message(
"useless-else-on-loop",
node=node,
# This is not optimal, but the line previous
# to the first statement in the else clause
# will usually be the one that contains the else:.
line=node.orelse[0].lineno - 1,
)
def _check_in_loop(self, node, node_name):
"""Check that a node is inside a for or while loop."""
for parent in node.node_ancestors():
if isinstance(parent, (nodes.For, nodes.While)):
if node not in parent.orelse:
return
if isinstance(parent, (nodes.ClassDef, nodes.FunctionDef)):
break
if (
isinstance(parent, nodes.TryFinally)
and node in parent.finalbody
and isinstance(node, nodes.Continue)
):
self.add_message("continue-in-finally", node=node)
self.add_message("not-in-loop", node=node, args=node_name)
def _check_redefinition(self, redeftype, node):
"""Check for redefinition of a function / method / class name."""
parent_frame = node.parent.frame(future=_True)
# Ignore function stubs created for type information
redefinitions = [
i
for i in parent_frame.locals[node.name]
if not (isinstance(i.parent, nodes.AnnAssign) and i.parent.simple)
]
defined_self = next(
(local for local in redefinitions if not utils.is_overload_stub(local)),
node,
)
if defined_self is not node and not astroid.are_exclusive(node, defined_self):
# Additional checks for methods which are not considered
# redefined, since they are already part of the base API.
if (
isinstance(parent_frame, nodes.ClassDef)
and node.name in REDEFINABLE_METHODS
):
return
# Skip typing.overload() functions.
if utils.is_overload_stub(node):
return
# Exempt functions redefined on a condition.
if isinstance(node.parent, nodes.If):
# Exempt "if not <func>" cases
if (
isinstance(node.parent.test, nodes.UnaryOp)
and node.parent.test.op == "not"
and isinstance(node.parent.test.operand, nodes.Name)
and node.parent.test.operand.name == node.name
):
return
# Exempt "if <func> is not None" cases
# pylint: disable=too-many-boolean-expressions
if (
isinstance(node.parent.test, nodes.Compare)
and isinstance(node.parent.test.left, nodes.Name)
and node.parent.test.left.name == node.name
and node.parent.test.ops[0][0] == "is"
and isinstance(node.parent.test.ops[0][1], nodes.Const)
and node.parent.test.ops[0][1].value is None
):
return
# Check if we have forward references for this node.
try:
redefinition_index = redefinitions.index(node)
except ValueError:
pass
else:
for redefinition in redefinitions[:redefinition_index]:
inferred = utils.safe_infer(redefinition)
if (
inferred
and isinstance(inferred, astroid.Instance)
and inferred.qname() == TYPING_FORWARD_REF_QNAME
):
return
dummy_variables_rgx = lint_utils.get_global_option(
self, "dummy-variables-rgx", default=None
)
if dummy_variables_rgx and dummy_variables_rgx.match(node.name):
return
self.add_message(
"function-redefined",
node=node,
args=(redeftype, defined_self.fromlineno),
)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 669 |
nodes_of_class
|
ref
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 671 |
scope
|
ref
|
function
|
if child.scope() is node
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 677 |
nodes_of_class
|
ref
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 678 |
scope
|
ref
|
function
|
if node_name.scope() is not node:
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 688 |
add_message
|
ref
|
function
|
self.add_message(
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 692 |
_check_nonlocal_and_global
|
def
|
function
|
def _check_nonlocal_and_global(self, node):
"""Check that a name is both nonlocal and global."""
def same_scope(current):
return current.scope() is node
from_iter = itertools.chain.from_iterable
nonlocals = set(
from_iter(
child.names
for child in node.nodes_of_class(nodes.Nonlocal)
if same_scope(child)
)
)
if not nonlocals:
return
global_vars = set(
from_iter(
child.names
for child in node.nodes_of_class(nodes.Global)
if same_scope(child)
)
)
for name in nonlocals.intersection(global_vars):
self.add_message("nonlocal-and-global", args=(name,), node=node)
@utils.check_messages("return-outside-function")
def visit_return(self, node: nodes.Return) -> None:
if not isinstance(node.frame(future=_True), nodes.FunctionDef):
self.add_message("return-outside-function", node=node)
@utils.check_messages("yield-outside-function")
def visit_yield(self, node: nodes.Yield) -> None:
self._check_yield_outside_func(node)
@utils.check_messages("yield-outside-function")
def visit_yieldfrom(self, node: nodes.YieldFrom) -> None:
self._check_yield_outside_func(node)
@utils.check_messages("not-in-loop", "continue-in-finally")
def visit_continue(self, node: nodes.Continue) -> None:
self._check_in_loop(node, "continue")
@utils.check_messages("not-in-loop")
def visit_break(self, node: nodes.Break) -> None:
self._check_in_loop(node, "break")
@utils.check_messages("useless-else-on-loop")
def visit_for(self, node: nodes.For) -> None:
self._check_else_on_loop(node)
@utils.check_messages("useless-else-on-loop")
def visit_while(self, node: nodes.While) -> None:
self._check_else_on_loop(node)
@utils.check_messages("nonexistent-operator")
def visit_unaryop(self, node: nodes.UnaryOp) -> None:
"""Check use of the non-existent ++ and -- operators."""
if (
(node.op in "+-")
and isinstance(node.operand, nodes.UnaryOp)
and (node.operand.op == node.op)
):
self.add_message("nonexistent-operator", node=node, args=node.op * 2)
def _check_nonlocal_without_binding(self, node, name):
current_scope = node.scope()
while _True:
if current_scope.parent is None:
break
if not isinstance(current_scope, (nodes.ClassDef, nodes.FunctionDef)):
self.add_message("nonlocal-without-binding", args=(name,), node=node)
return
if name not in current_scope.locals:
current_scope = current_scope.parent.scope()
continue
# Okay, found it.
return
if not isinstance(current_scope, nodes.FunctionDef):
self.add_message("nonlocal-without-binding", args=(name,), node=node)
@utils.check_messages("nonlocal-without-binding")
def visit_nonlocal(self, node: nodes.Nonlocal) -> None:
for name in node.names:
self._check_nonlocal_without_binding(node, name)
@utils.check_messages("abstract-class-instantiated")
def visit_call(self, node: nodes.Call) -> None:
"""Check instantiating abstract class with
abc.ABCMeta as metaclass.
"""
for inferred in infer_all(node.func):
self._check_inferred_class_is_abstract(inferred, node)
def _check_inferred_class_is_abstract(self, inferred, node):
if not isinstance(inferred, nodes.ClassDef):
return
klass = utils.node_frame_class(node)
if klass is inferred:
# Don't emit the warning if the class is instantiated
# in its own body or if the call is not an instance
# creation. If the class is instantiated into its own
# body, we're expecting that it knows what it is doing.
return
# __init__ was called
abstract_methods = _has_abstract_methods(inferred)
if not abstract_methods:
return
metaclass = inferred.metaclass()
if metaclass is None:
# Python 3.4 has `abc.ABC`, which won't be detected
# by ClassNode.metaclass()
for ancestor in inferred.ancestors():
if ancestor.qname() == "abc.ABC":
self.add_message(
"abstract-class-instantiated", args=(inferred.name,), node=node
)
break
return
if metaclass.qname() in ABC_METACLASSES:
self.add_message(
"abstract-class-instantiated", args=(inferred.name,), node=node
)
def _check_yield_outside_func(self, node):
if not isinstance(node.frame(future=_True), (nodes.FunctionDef, nodes.Lambda)):
self.add_message("yield-outside-function", node=node)
def _check_else_on_loop(self, node):
"""Check that any loop with an else clause has a break statement."""
if node.orelse and not _loop_exits_early(node):
self.add_message(
"useless-else-on-loop",
node=node,
# This is not optimal, but the line previous
# to the first statement in the else clause
# will usually be the one that contains the else:.
line=node.orelse[0].lineno - 1,
)
def _check_in_loop(self, node, node_name):
"""Check that a node is inside a for or while loop."""
for parent in node.node_ancestors():
if isinstance(parent, (nodes.For, nodes.While)):
if node not in parent.orelse:
return
if isinstance(parent, (nodes.ClassDef, nodes.FunctionDef)):
break
if (
isinstance(parent, nodes.TryFinally)
and node in parent.finalbody
and isinstance(node, nodes.Continue)
):
self.add_message("continue-in-finally", node=node)
self.add_message("not-in-loop", node=node, args=node_name)
def _check_redefinition(self, redeftype, node):
"""Check for redefinition of a function / method / class name."""
parent_frame = node.parent.frame(future=_True)
# Ignore function stubs created for type information
redefinitions = [
i
for i in parent_frame.locals[node.name]
if not (isinstance(i.parent, nodes.AnnAssign) and i.parent.simple)
]
defined_self = next(
(local for local in redefinitions if not utils.is_overload_stub(local)),
node,
)
if defined_self is not node and not astroid.are_exclusive(node, defined_self):
# Additional checks for methods which are not considered
# redefined, since they are already part of the base API.
if (
isinstance(parent_frame, nodes.ClassDef)
and node.name in REDEFINABLE_METHODS
):
return
# Skip typing.overload() functions.
if utils.is_overload_stub(node):
return
# Exempt functions redefined on a condition.
if isinstance(node.parent, nodes.If):
# Exempt "if not <func>" cases
if (
isinstance(node.parent.test, nodes.UnaryOp)
and node.parent.test.op == "not"
and isinstance(node.parent.test.operand, nodes.Name)
and node.parent.test.operand.name == node.name
):
return
# Exempt "if <func> is not None" cases
# pylint: disable=too-many-boolean-expressions
if (
isinstance(node.parent.test, nodes.Compare)
and isinstance(node.parent.test.left, nodes.Name)
and node.parent.test.left.name == node.name
and node.parent.test.ops[0][0] == "is"
and isinstance(node.parent.test.ops[0][1], nodes.Const)
and node.parent.test.ops[0][1].value is None
):
return
# Check if we have forward references for this node.
try:
redefinition_index = redefinitions.index(node)
except ValueError:
pass
else:
for redefinition in redefinitions[:redefinition_index]:
inferred = utils.safe_infer(redefinition)
if (
inferred
and isinstance(inferred, astroid.Instance)
and inferred.qname() == TYPING_FORWARD_REF_QNAME
):
return
dummy_variables_rgx = lint_utils.get_global_option(
self, "dummy-variables-rgx", default=None
)
if dummy_variables_rgx and dummy_variables_rgx.match(node.name):
return
self.add_message(
"function-redefined",
node=node,
args=(redeftype, defined_self.fromlineno),
)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 695 |
same_scope
|
def
|
function
|
def same_scope(current):
return current.scope() is node
from_iter = itertools.chain.from_iterable
nonlocals = set(
from_iter(
child.names
for child in node.nodes_of_class(nodes.Nonlocal)
if same_scope(child)
)
)
if not nonlocals:
return
global_vars = set(
from_iter(
child.names
for child in node.nodes_of_class(nodes.Global)
if same_scope(child)
)
)
for name in nonlocals.intersection(global_vars):
self.add_message("nonlocal-and-global", args=(name,), node=node)
@utils.check_messages("return-outside-function")
def visit_return(self, node: nodes.Return) -> None:
if not isinstance(node.frame(future=_True), nodes.FunctionDef):
self.add_message("return-outside-function", node=node)
@utils.check_messages("yield-outside-function")
def visit_yield(self, node: nodes.Yield) -> None:
self._check_yield_outside_func(node)
@utils.check_messages("yield-outside-function")
def visit_yieldfrom(self, node: nodes.YieldFrom) -> None:
self._check_yield_outside_func(node)
@utils.check_messages("not-in-loop", "continue-in-finally")
def visit_continue(self, node: nodes.Continue) -> None:
self._check_in_loop(node, "continue")
@utils.check_messages("not-in-loop")
def visit_break(self, node: nodes.Break) -> None:
self._check_in_loop(node, "break")
@utils.check_messages("useless-else-on-loop")
def visit_for(self, node: nodes.For) -> None:
self._check_else_on_loop(node)
@utils.check_messages("useless-else-on-loop")
def visit_while(self, node: nodes.While) -> None:
self._check_else_on_loop(node)
@utils.check_messages("nonexistent-operator")
def visit_unaryop(self, node: nodes.UnaryOp) -> None:
"""Check use of the non-existent ++ and -- operators."""
if (
(node.op in "+-")
and isinstance(node.operand, nodes.UnaryOp)
and (node.operand.op == node.op)
):
self.add_message("nonexistent-operator", node=node, args=node.op * 2)
def _check_nonlocal_without_binding(self, node, name):
current_scope = node.scope()
while _True:
if current_scope.parent is None:
break
if not isinstance(current_scope, (nodes.ClassDef, nodes.FunctionDef)):
self.add_message("nonlocal-without-binding", args=(name,), node=node)
return
if name not in current_scope.locals:
current_scope = current_scope.parent.scope()
continue
# Okay, found it.
return
if not isinstance(current_scope, nodes.FunctionDef):
self.add_message("nonlocal-without-binding", args=(name,), node=node)
@utils.check_messages("nonlocal-without-binding")
def visit_nonlocal(self, node: nodes.Nonlocal) -> None:
for name in node.names:
self._check_nonlocal_without_binding(node, name)
@utils.check_messages("abstract-class-instantiated")
def visit_call(self, node: nodes.Call) -> None:
"""Check instantiating abstract class with
abc.ABCMeta as metaclass.
"""
for inferred in infer_all(node.func):
self._check_inferred_class_is_abstract(inferred, node)
def _check_inferred_class_is_abstract(self, inferred, node):
if not isinstance(inferred, nodes.ClassDef):
return
klass = utils.node_frame_class(node)
if klass is inferred:
# Don't emit the warning if the class is instantiated
# in its own body or if the call is not an instance
# creation. If the class is instantiated into its own
# body, we're expecting that it knows what it is doing.
return
# __init__ was called
abstract_methods = _has_abstract_methods(inferred)
if not abstract_methods:
return
metaclass = inferred.metaclass()
if metaclass is None:
# Python 3.4 has `abc.ABC`, which won't be detected
# by ClassNode.metaclass()
for ancestor in inferred.ancestors():
if ancestor.qname() == "abc.ABC":
self.add_message(
"abstract-class-instantiated", args=(inferred.name,), node=node
)
break
return
if metaclass.qname() in ABC_METACLASSES:
self.add_message(
"abstract-class-instantiated", args=(inferred.name,), node=node
)
def _check_yield_outside_func(self, node):
if not isinstance(node.frame(future=_True), (nodes.FunctionDef, nodes.Lambda)):
self.add_message("yield-outside-function", node=node)
def _check_else_on_loop(self, node):
"""Check that any loop with an else clause has a break statement."""
if node.orelse and not _loop_exits_early(node):
self.add_message(
"useless-else-on-loop",
node=node,
# This is not optimal, but the line previous
# to the first statement in the else clause
# will usually be the one that contains the else:.
line=node.orelse[0].lineno - 1,
)
def _check_in_loop(self, node, node_name):
"""Check that a node is inside a for or while loop."""
for parent in node.node_ancestors():
if isinstance(parent, (nodes.For, nodes.While)):
if node not in parent.orelse:
return
if isinstance(parent, (nodes.ClassDef, nodes.FunctionDef)):
break
if (
isinstance(parent, nodes.TryFinally)
and node in parent.finalbody
and isinstance(node, nodes.Continue)
):
self.add_message("continue-in-finally", node=node)
self.add_message("not-in-loop", node=node, args=node_name)
def _check_redefinition(self, redeftype, node):
"""Check for redefinition of a function / method / class name."""
parent_frame = node.parent.frame(future=_True)
# Ignore function stubs created for type information
redefinitions = [
i
for i in parent_frame.locals[node.name]
if not (isinstance(i.parent, nodes.AnnAssign) and i.parent.simple)
]
defined_self = next(
(local for local in redefinitions if not utils.is_overload_stub(local)),
node,
)
if defined_self is not node and not astroid.are_exclusive(node, defined_self):
# Additional checks for methods which are not considered
# redefined, since they are already part of the base API.
if (
isinstance(parent_frame, nodes.ClassDef)
and node.name in REDEFINABLE_METHODS
):
return
# Skip typing.overload() functions.
if utils.is_overload_stub(node):
return
# Exempt functions redefined on a condition.
if isinstance(node.parent, nodes.If):
# Exempt "if not <func>" cases
if (
isinstance(node.parent.test, nodes.UnaryOp)
and node.parent.test.op == "not"
and isinstance(node.parent.test.operand, nodes.Name)
and node.parent.test.operand.name == node.name
):
return
# Exempt "if <func> is not None" cases
# pylint: disable=too-many-boolean-expressions
if (
isinstance(node.parent.test, nodes.Compare)
and isinstance(node.parent.test.left, nodes.Name)
and node.parent.test.left.name == node.name
and node.parent.test.ops[0][0] == "is"
and isinstance(node.parent.test.ops[0][1], nodes.Const)
and node.parent.test.ops[0][1].value is None
):
return
# Check if we have forward references for this node.
try:
redefinition_index = redefinitions.index(node)
except ValueError:
pass
else:
for redefinition in redefinitions[:redefinition_index]:
inferred = utils.safe_infer(redefinition)
if (
inferred
and isinstance(inferred, astroid.Instance)
and inferred.qname() == TYPING_FORWARD_REF_QNAME
):
return
dummy_variables_rgx = lint_utils.get_global_option(
self, "dummy-variables-rgx", default=None
)
if dummy_variables_rgx and dummy_variables_rgx.match(node.name):
return
self.add_message(
"function-redefined",
node=node,
args=(redeftype, defined_self.fromlineno),
)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 696 |
scope
|
ref
|
function
|
return current.scope() is node
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 700 |
from_iter
|
ref
|
function
|
from_iter(
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 702 |
nodes_of_class
|
ref
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 703 |
same_scope
|
ref
|
function
|
if same_scope(child)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 711 |
from_iter
|
ref
|
function
|
from_iter(
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 713 |
nodes_of_class
|
ref
|
class
| |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 714 |
same_scope
|
ref
|
function
|
if same_scope(child)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 718 |
add_message
|
ref
|
function
|
self.add_message("nonlocal-and-global", args=(name,), node=node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 720 |
check_messages
|
ref
|
function
|
@utils.check_messages("return-outside-function")
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 721 |
visit_return
|
def
|
function
|
def visit_return(self, node: nodes.Return) -> None:
if not isinstance(node.frame(future=_True), nodes.FunctionDef):
self.add_message("return-outside-function", node=node)
@utils.check_messages("yield-outside-function")
def visit_yield(self, node: nodes.Yield) -> None:
self._check_yield_outside_func(node)
@utils.check_messages("yield-outside-function")
def visit_yieldfrom(self, node: nodes.YieldFrom) -> None:
self._check_yield_outside_func(node)
@utils.check_messages("not-in-loop", "continue-in-finally")
def visit_continue(self, node: nodes.Continue) -> None:
self._check_in_loop(node, "continue")
@utils.check_messages("not-in-loop")
def visit_break(self, node: nodes.Break) -> None:
self._check_in_loop(node, "break")
@utils.check_messages("useless-else-on-loop")
def visit_for(self, node: nodes.For) -> None:
self._check_else_on_loop(node)
@utils.check_messages("useless-else-on-loop")
def visit_while(self, node: nodes.While) -> None:
self._check_else_on_loop(node)
@utils.check_messages("nonexistent-operator")
def visit_unaryop(self, node: nodes.UnaryOp) -> None:
"""Check use of the non-existent ++ and -- operators."""
if (
(node.op in "+-")
and isinstance(node.operand, nodes.UnaryOp)
and (node.operand.op == node.op)
):
self.add_message("nonexistent-operator", node=node, args=node.op * 2)
def _check_nonlocal_without_binding(self, node, name):
current_scope = node.scope()
while _True:
if current_scope.parent is None:
break
if not isinstance(current_scope, (nodes.ClassDef, nodes.FunctionDef)):
self.add_message("nonlocal-without-binding", args=(name,), node=node)
return
if name not in current_scope.locals:
current_scope = current_scope.parent.scope()
continue
# Okay, found it.
return
if not isinstance(current_scope, nodes.FunctionDef):
self.add_message("nonlocal-without-binding", args=(name,), node=node)
@utils.check_messages("nonlocal-without-binding")
def visit_nonlocal(self, node: nodes.Nonlocal) -> None:
for name in node.names:
self._check_nonlocal_without_binding(node, name)
@utils.check_messages("abstract-class-instantiated")
def visit_call(self, node: nodes.Call) -> None:
"""Check instantiating abstract class with
abc.ABCMeta as metaclass.
"""
for inferred in infer_all(node.func):
self._check_inferred_class_is_abstract(inferred, node)
def _check_inferred_class_is_abstract(self, inferred, node):
if not isinstance(inferred, nodes.ClassDef):
return
klass = utils.node_frame_class(node)
if klass is inferred:
# Don't emit the warning if the class is instantiated
# in its own body or if the call is not an instance
# creation. If the class is instantiated into its own
# body, we're expecting that it knows what it is doing.
return
# __init__ was called
abstract_methods = _has_abstract_methods(inferred)
if not abstract_methods:
return
metaclass = inferred.metaclass()
if metaclass is None:
# Python 3.4 has `abc.ABC`, which won't be detected
# by ClassNode.metaclass()
for ancestor in inferred.ancestors():
if ancestor.qname() == "abc.ABC":
self.add_message(
"abstract-class-instantiated", args=(inferred.name,), node=node
)
break
return
if metaclass.qname() in ABC_METACLASSES:
self.add_message(
"abstract-class-instantiated", args=(inferred.name,), node=node
)
def _check_yield_outside_func(self, node):
if not isinstance(node.frame(future=_True), (nodes.FunctionDef, nodes.Lambda)):
self.add_message("yield-outside-function", node=node)
def _check_else_on_loop(self, node):
"""Check that any loop with an else clause has a break statement."""
if node.orelse and not _loop_exits_early(node):
self.add_message(
"useless-else-on-loop",
node=node,
# This is not optimal, but the line previous
# to the first statement in the else clause
# will usually be the one that contains the else:.
line=node.orelse[0].lineno - 1,
)
def _check_in_loop(self, node, node_name):
"""Check that a node is inside a for or while loop."""
for parent in node.node_ancestors():
if isinstance(parent, (nodes.For, nodes.While)):
if node not in parent.orelse:
return
if isinstance(parent, (nodes.ClassDef, nodes.FunctionDef)):
break
if (
isinstance(parent, nodes.TryFinally)
and node in parent.finalbody
and isinstance(node, nodes.Continue)
):
self.add_message("continue-in-finally", node=node)
self.add_message("not-in-loop", node=node, args=node_name)
def _check_redefinition(self, redeftype, node):
"""Check for redefinition of a function / method / class name."""
parent_frame = node.parent.frame(future=_True)
# Ignore function stubs created for type information
redefinitions = [
i
for i in parent_frame.locals[node.name]
if not (isinstance(i.parent, nodes.AnnAssign) and i.parent.simple)
]
defined_self = next(
(local for local in redefinitions if not utils.is_overload_stub(local)),
node,
)
if defined_self is not node and not astroid.are_exclusive(node, defined_self):
# Additional checks for methods which are not considered
# redefined, since they are already part of the base API.
if (
isinstance(parent_frame, nodes.ClassDef)
and node.name in REDEFINABLE_METHODS
):
return
# Skip typing.overload() functions.
if utils.is_overload_stub(node):
return
# Exempt functions redefined on a condition.
if isinstance(node.parent, nodes.If):
# Exempt "if not <func>" cases
if (
isinstance(node.parent.test, nodes.UnaryOp)
and node.parent.test.op == "not"
and isinstance(node.parent.test.operand, nodes.Name)
and node.parent.test.operand.name == node.name
):
return
# Exempt "if <func> is not None" cases
# pylint: disable=too-many-boolean-expressions
if (
isinstance(node.parent.test, nodes.Compare)
and isinstance(node.parent.test.left, nodes.Name)
and node.parent.test.left.name == node.name
and node.parent.test.ops[0][0] == "is"
and isinstance(node.parent.test.ops[0][1], nodes.Const)
and node.parent.test.ops[0][1].value is None
):
return
# Check if we have forward references for this node.
try:
redefinition_index = redefinitions.index(node)
except ValueError:
pass
else:
for redefinition in redefinitions[:redefinition_index]:
inferred = utils.safe_infer(redefinition)
if (
inferred
and isinstance(inferred, astroid.Instance)
and inferred.qname() == TYPING_FORWARD_REF_QNAME
):
return
dummy_variables_rgx = lint_utils.get_global_option(
self, "dummy-variables-rgx", default=None
)
if dummy_variables_rgx and dummy_variables_rgx.match(node.name):
return
self.add_message(
"function-redefined",
node=node,
args=(redeftype, defined_self.fromlineno),
)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 722 |
frame
|
ref
|
function
|
if not isinstance(node.frame(future=True), nodes.FunctionDef):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/base.py
|
pylint/checkers/base.py
| 723 |
add_message
|
ref
|
function
|
self.add_message("return-outside-function", node=node)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.