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/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 97 | _common_cond_list_set | def | function | def _common_cond_list_set(
node: nodes.Expr,
iter_obj: nodes.NodeNG,
infer_val: Union[nodes.List, nodes.Set],
) -> bool:
return (infer_val == utils.safe_infer(iter_obj)) and (
node.value.func.expr.name == iter_obj.name
)
@staticmethod
def _is_node_assigns_subscript_name(node: nodes.NodeNG) -> bool:
return isinstance(node, nodes.Assign) and (
isinstance(node.targets[0], nodes.Subscript)
and (isinstance(node.targets[0].value, nodes.Name))
)
def _modified_iterating_list_cond(
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
) -> bool:
if not self._is_node_expr_that_calls_attribute_name(node):
return _False
infer_val = utils.safe_infer(node.value.func.expr)
if not isinstance(infer_val, nodes.List):
return _False
return (
self._common_cond_list_set(node, iter_obj, infer_val)
and node.value.func.attrname in _LIST_MODIFIER_METHODS
)
def _modified_iterating_dict_cond(
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
) -> bool:
if not self._is_node_assigns_subscript_name(node):
return _False
infer_val = utils.safe_infer(node.targets[0].value)
if not isinstance(infer_val, nodes.Dict):
return _False
if infer_val != utils.safe_infer(iter_obj):
return _False
return node.targets[0].value.name == iter_obj.name
def _modified_iterating_set_cond(
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
) -> bool:
if not self._is_node_expr_that_calls_attribute_name(node):
return _False
infer_val = utils.safe_infer(node.value.func.expr)
if not isinstance(infer_val, nodes.Set):
return _False
return (
self._common_cond_list_set(node, iter_obj, infer_val)
and node.value.func.attrname in _SET_MODIFIER_METHODS
)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 102 | safe_infer | ref | function | return (infer_val == utils.safe_infer(iter_obj)) and (
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 107 | _is_node_assigns_subscript_name | def | function | def _is_node_assigns_subscript_name(node: nodes.NodeNG) -> bool:
return isinstance(node, nodes.Assign) and (
isinstance(node.targets[0], nodes.Subscript)
and (isinstance(node.targets[0].value, nodes.Name))
)
def _modified_iterating_list_cond(
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
) -> bool:
if not self._is_node_expr_that_calls_attribute_name(node):
return _False
infer_val = utils.safe_infer(node.value.func.expr)
if not isinstance(infer_val, nodes.List):
return _False
return (
self._common_cond_list_set(node, iter_obj, infer_val)
and node.value.func.attrname in _LIST_MODIFIER_METHODS
)
def _modified_iterating_dict_cond(
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
) -> bool:
if not self._is_node_assigns_subscript_name(node):
return _False
infer_val = utils.safe_infer(node.targets[0].value)
if not isinstance(infer_val, nodes.Dict):
return _False
if infer_val != utils.safe_infer(iter_obj):
return _False
return node.targets[0].value.name == iter_obj.name
def _modified_iterating_set_cond(
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
) -> bool:
if not self._is_node_expr_that_calls_attribute_name(node):
return _False
infer_val = utils.safe_infer(node.value.func.expr)
if not isinstance(infer_val, nodes.Set):
return _False
return (
self._common_cond_list_set(node, iter_obj, infer_val)
and node.value.func.attrname in _SET_MODIFIER_METHODS
)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 113 | _modified_iterating_list_cond | def | function | def _modified_iterating_list_cond(
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
) -> bool:
if not self._is_node_expr_that_calls_attribute_name(node):
return _False
infer_val = utils.safe_infer(node.value.func.expr)
if not isinstance(infer_val, nodes.List):
return _False
return (
self._common_cond_list_set(node, iter_obj, infer_val)
and node.value.func.attrname in _LIST_MODIFIER_METHODS
)
def _modified_iterating_dict_cond(
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
) -> bool:
if not self._is_node_assigns_subscript_name(node):
return _False
infer_val = utils.safe_infer(node.targets[0].value)
if not isinstance(infer_val, nodes.Dict):
return _False
if infer_val != utils.safe_infer(iter_obj):
return _False
return node.targets[0].value.name == iter_obj.name
def _modified_iterating_set_cond(
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
) -> bool:
if not self._is_node_expr_that_calls_attribute_name(node):
return _False
infer_val = utils.safe_infer(node.value.func.expr)
if not isinstance(infer_val, nodes.Set):
return _False
return (
self._common_cond_list_set(node, iter_obj, infer_val)
and node.value.func.attrname in _SET_MODIFIER_METHODS
)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 116 | _is_node_expr_that_calls_attribute_name | ref | function | if not self._is_node_expr_that_calls_attribute_name(node):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 118 | safe_infer | ref | function | infer_val = utils.safe_infer(node.value.func.expr)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 122 | _common_cond_list_set | ref | function | self._common_cond_list_set(node, iter_obj, infer_val)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 126 | _modified_iterating_dict_cond | def | function | def _modified_iterating_dict_cond(
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
) -> bool:
if not self._is_node_assigns_subscript_name(node):
return _False
infer_val = utils.safe_infer(node.targets[0].value)
if not isinstance(infer_val, nodes.Dict):
return _False
if infer_val != utils.safe_infer(iter_obj):
return _False
return node.targets[0].value.name == iter_obj.name
def _modified_iterating_set_cond(
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
) -> bool:
if not self._is_node_expr_that_calls_attribute_name(node):
return _False
infer_val = utils.safe_infer(node.value.func.expr)
if not isinstance(infer_val, nodes.Set):
return _False
return (
self._common_cond_list_set(node, iter_obj, infer_val)
and node.value.func.attrname in _SET_MODIFIER_METHODS
)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 129 | _is_node_assigns_subscript_name | ref | function | if not self._is_node_assigns_subscript_name(node):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 131 | safe_infer | ref | function | infer_val = utils.safe_infer(node.targets[0].value)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 134 | safe_infer | ref | function | if infer_val != utils.safe_infer(iter_obj):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 138 | _modified_iterating_set_cond | def | function | def _modified_iterating_set_cond(
self, node: nodes.NodeNG, iter_obj: nodes.NodeNG
) -> bool:
if not self._is_node_expr_that_calls_attribute_name(node):
return _False
infer_val = utils.safe_infer(node.value.func.expr)
if not isinstance(infer_val, nodes.Set):
return _False
return (
self._common_cond_list_set(node, iter_obj, infer_val)
and node.value.func.attrname in _SET_MODIFIER_METHODS
)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 141 | _is_node_expr_that_calls_attribute_name | ref | function | if not self._is_node_expr_that_calls_attribute_name(node):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 143 | safe_infer | ref | function | infer_val = utils.safe_infer(node.value.func.expr)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 147 | _common_cond_list_set | ref | function | self._common_cond_list_set(node, iter_obj, infer_val)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 152 | register | def | function | def register(linter: "PyLinter") -> None:
linter.register_checker(ModifiedIterationChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 153 | register_checker | ref | function | linter.register_checker(ModifiedIterationChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/modified_iterating_checker.py | pylint/checkers/modified_iterating_checker.py | 153 | ModifiedIterationChecker | ref | function | linter.register_checker(ModifiedIterationChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/newstyle.py | pylint/checkers/newstyle.py | 45 | NewStyleConflictChecker | def | class | visit_functiondef |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/newstyle.py | pylint/checkers/newstyle.py | 62 | check_messages | ref | function | @check_messages("bad-super-call")
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/newstyle.py | pylint/checkers/newstyle.py | 63 | visit_functiondef | def | function | def visit_functiondef(self, node: nodes.FunctionDef) -> None:
"""Check use of super."""
# ignore actual functions or method within a new style class
if not node.is_method():
return
klass = node.parent.frame(future=_True)
for stmt in node.nodes_of_class(nodes.Call):
if node_frame_class(stmt) != node_frame_class(node):
# Don't look down in other scopes.
continue
expr = stmt.func
if not isinstance(expr, nodes.Attribute):
continue
call = expr.expr
# skip the test if using super
if not (
isinstance(call, nodes.Call)
and isinstance(call.func, nodes.Name)
and call.func.name == "super"
):
continue
# super should not be used on an old style class
if klass.newstyle or not has_known_bases(klass):
# super first arg should not be the class
if not call.args:
continue
# calling super(type(self), self) can lead to recursion loop
# in derived classes
arg0 = call.args[0]
if (
isinstance(arg0, nodes.Call)
and isinstance(arg0.func, nodes.Name)
and arg0.func.name == "type"
):
self.add_message("bad-super-call", node=call, args=("type",))
continue
# calling super(self.__class__, self) can lead to recursion loop
# in derived classes
if (
len(call.args) >= 2
and isinstance(call.args[1], nodes.Name)
and call.args[1].name == "self"
and isinstance(arg0, nodes.Attribute)
and arg0.attrname == "__class__"
):
self.add_message(
"bad-super-call", node=call, args=("self.__class__",)
)
continue
try:
supcls = call.args and next(call.args[0].infer(), None)
except astroid.InferenceError:
continue
if klass is not supcls:
name = None
# if supcls is not Uninferable, then supcls was inferred
# and use its name. Otherwise, try to look
# for call.args[0].name
if supcls:
name = supcls.name
elif call.args and hasattr(call.args[0], "name"):
name = call.args[0].name
if name:
self.add_message("bad-super-call", node=call, args=(name,))
visit_asyncfunctiondef = visit_functiondef
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/newstyle.py | pylint/checkers/newstyle.py | 66 | is_method | ref | function | if not node.is_method():
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/newstyle.py | pylint/checkers/newstyle.py | 68 | frame | ref | function | klass = node.parent.frame(future=True)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/newstyle.py | pylint/checkers/newstyle.py | 69 | nodes_of_class | ref | class | |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/newstyle.py | pylint/checkers/newstyle.py | 70 | node_frame_class | ref | class | |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/newstyle.py | pylint/checkers/newstyle.py | 70 | node_frame_class | ref | class | |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/newstyle.py | pylint/checkers/newstyle.py | 88 | has_known_bases | ref | function | if klass.newstyle or not has_known_bases(klass):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/newstyle.py | pylint/checkers/newstyle.py | 101 | add_message | ref | function | self.add_message("bad-super-call", node=call, args=("type",))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/newstyle.py | pylint/checkers/newstyle.py | 113 | add_message | ref | function | self.add_message(
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/newstyle.py | pylint/checkers/newstyle.py | 119 | infer | ref | function | supcls = call.args and next(call.args[0].infer(), None)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/newstyle.py | pylint/checkers/newstyle.py | 133 | add_message | ref | function | self.add_message("bad-super-call", node=call, args=(name,))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/newstyle.py | pylint/checkers/newstyle.py | 138 | register | def | function | def register(linter: "PyLinter") -> None:
linter.register_checker(NewStyleConflictChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/newstyle.py | pylint/checkers/newstyle.py | 139 | register_checker | ref | function | linter.register_checker(NewStyleConflictChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/newstyle.py | pylint/checkers/newstyle.py | 139 | NewStyleConflictChecker | ref | function | linter.register_checker(NewStyleConflictChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 26 | Py37Str | def | class | isascii |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 42 | NonAsciiNameChecker | def | class | _check_name visit_module visit_functiondef visit_global visit_assignname visit_classdef _check_module_import visit_import visit_importfrom visit_call |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 89 | _check_name | def | function | def _check_name(
self, node_type: str, name: Optional[str], node: nodes.NodeNG
) -> None:
"""Check whether a name is using non-ASCII characters."""
if name is None:
# For some nodes i.e. *kwargs from a dict, the name will be empty
return
if not (Py37Str(name).isascii()):
type_label = constants.HUMAN_READABLE_TYPES[node_type]
args = (type_label.capitalize(), name)
msg = "non-ascii-name"
# Some node types have customized messages
if node_type == "file":
msg = "non-ascii-file-name"
elif node_type == "module":
msg = "non-ascii-module-import"
self.add_message(msg, node=node, args=args, confidence=interfaces.HIGH)
@utils.check_messages("non-ascii-name")
def visit_module(self, node: nodes.Module) -> None:
self._check_name("file", node.name.split(".")[-1], node)
@utils.check_messages("non-ascii-name")
def visit_functiondef(
self, node: Union[nodes.FunctionDef, nodes.AsyncFunctionDef]
) -> None:
self._check_name("function", node.name, node)
# Check argument names
arguments = node.args
# Check position only arguments
if arguments.posonlyargs:
for pos_only_arg in arguments.posonlyargs:
self._check_name("argument", pos_only_arg.name, pos_only_arg)
# Check "normal" arguments
if arguments.args:
for arg in arguments.args:
self._check_name("argument", arg.name, arg)
# Check key word only arguments
if arguments.kwonlyargs:
for kwarg in arguments.kwonlyargs:
self._check_name("argument", kwarg.name, kwarg)
visit_asyncfunctiondef = visit_functiondef
@utils.check_messages("non-ascii-name")
def visit_global(self, node: nodes.Global) -> None:
for name in node.names:
self._check_name("const", name, node)
@utils.check_messages("non-ascii-name")
def visit_assignname(self, node: nodes.AssignName) -> None:
"""Check module level assigned names."""
# The NameChecker from which this Checker originates knows a lot of different
# versions of variables, i.e. constants, inline variables etc.
# To simplify we use only `variable` here, as we don't need to apply different
# rules to different types of variables.
frame = node.frame()
if isinstance(frame, nodes.FunctionDef):
if node.parent in frame.body:
# Only perform the check if the assignment was done in within the body
# of the function (and not the function parameter definition
# (will be handled in visit_functiondef)
# or within a decorator (handled in visit_call)
self._check_name("variable", node.name, node)
elif isinstance(frame, nodes.ClassDef):
self._check_name("attr", node.name, node)
else:
# Possibilities here:
# - isinstance(node.assign_type(), nodes.Comprehension) == inlinevar
# - isinstance(frame, nodes.Module) == variable (constant?)
# - some other kind of assigment missed but still most likely a variable
self._check_name("variable", node.name, node)
@utils.check_messages("non-ascii-name")
def visit_classdef(self, node: nodes.ClassDef) -> None:
self._check_name("class", node.name, node)
for attr, anodes in node.instance_attrs.items():
if not any(node.instance_attr_ancestors(attr)):
self._check_name("attr", attr, anodes[0])
def _check_module_import(self, node: Union[nodes.ImportFrom, nodes.Import]) -> None:
for module_name, alias in node.names:
name = alias or module_name
self._check_name("module", name, node)
@utils.check_messages("non-ascii-name")
def visit_import(self, node: nodes.Import) -> None:
self._check_module_import(node)
@utils.check_messages("non-ascii-name")
def visit_importfrom(self, node: nodes.ImportFrom) -> None:
self._check_module_import(node)
@utils.check_messages("non-ascii-name")
def visit_call(self, node: nodes.Call) -> None:
"""Check if the used keyword args are correct."""
for keyword in node.keywords:
self._check_name("argument", keyword.arg, keyword)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 98 | Py37Str | ref | function | if not (Py37Str(name).isascii()):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 110 | add_message | ref | function | self.add_message(msg, node=node, args=args, confidence=interfaces.HIGH)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 112 | check_messages | ref | function | @utils.check_messages("non-ascii-name")
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 113 | visit_module | def | function | def visit_module(self, node: nodes.Module) -> None:
self._check_name("file", node.name.split(".")[-1], node)
@utils.check_messages("non-ascii-name")
def visit_functiondef(
self, node: Union[nodes.FunctionDef, nodes.AsyncFunctionDef]
) -> None:
self._check_name("function", node.name, node)
# Check argument names
arguments = node.args
# Check position only arguments
if arguments.posonlyargs:
for pos_only_arg in arguments.posonlyargs:
self._check_name("argument", pos_only_arg.name, pos_only_arg)
# Check "normal" arguments
if arguments.args:
for arg in arguments.args:
self._check_name("argument", arg.name, arg)
# Check key word only arguments
if arguments.kwonlyargs:
for kwarg in arguments.kwonlyargs:
self._check_name("argument", kwarg.name, kwarg)
visit_asyncfunctiondef = visit_functiondef
@utils.check_messages("non-ascii-name")
def visit_global(self, node: nodes.Global) -> None:
for name in node.names:
self._check_name("const", name, node)
@utils.check_messages("non-ascii-name")
def visit_assignname(self, node: nodes.AssignName) -> None:
"""Check module level assigned names."""
# The NameChecker from which this Checker originates knows a lot of different
# versions of variables, i.e. constants, inline variables etc.
# To simplify we use only `variable` here, as we don't need to apply different
# rules to different types of variables.
frame = node.frame()
if isinstance(frame, nodes.FunctionDef):
if node.parent in frame.body:
# Only perform the check if the assignment was done in within the body
# of the function (and not the function parameter definition
# (will be handled in visit_functiondef)
# or within a decorator (handled in visit_call)
self._check_name("variable", node.name, node)
elif isinstance(frame, nodes.ClassDef):
self._check_name("attr", node.name, node)
else:
# Possibilities here:
# - isinstance(node.assign_type(), nodes.Comprehension) == inlinevar
# - isinstance(frame, nodes.Module) == variable (constant?)
# - some other kind of assigment missed but still most likely a variable
self._check_name("variable", node.name, node)
@utils.check_messages("non-ascii-name")
def visit_classdef(self, node: nodes.ClassDef) -> None:
self._check_name("class", node.name, node)
for attr, anodes in node.instance_attrs.items():
if not any(node.instance_attr_ancestors(attr)):
self._check_name("attr", attr, anodes[0])
def _check_module_import(self, node: Union[nodes.ImportFrom, nodes.Import]) -> None:
for module_name, alias in node.names:
name = alias or module_name
self._check_name("module", name, node)
@utils.check_messages("non-ascii-name")
def visit_import(self, node: nodes.Import) -> None:
self._check_module_import(node)
@utils.check_messages("non-ascii-name")
def visit_importfrom(self, node: nodes.ImportFrom) -> None:
self._check_module_import(node)
@utils.check_messages("non-ascii-name")
def visit_call(self, node: nodes.Call) -> None:
"""Check if the used keyword args are correct."""
for keyword in node.keywords:
self._check_name("argument", keyword.arg, keyword)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 114 | _check_name | ref | function | self._check_name("file", node.name.split(".")[-1], node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 116 | check_messages | ref | function | @utils.check_messages("non-ascii-name")
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 117 | visit_functiondef | def | function | def visit_functiondef(
self, node: Union[nodes.FunctionDef, nodes.AsyncFunctionDef]
) -> None:
self._check_name("function", node.name, node)
# Check argument names
arguments = node.args
# Check position only arguments
if arguments.posonlyargs:
for pos_only_arg in arguments.posonlyargs:
self._check_name("argument", pos_only_arg.name, pos_only_arg)
# Check "normal" arguments
if arguments.args:
for arg in arguments.args:
self._check_name("argument", arg.name, arg)
# Check key word only arguments
if arguments.kwonlyargs:
for kwarg in arguments.kwonlyargs:
self._check_name("argument", kwarg.name, kwarg)
visit_asyncfunctiondef = visit_functiondef
@utils.check_messages("non-ascii-name")
def visit_global(self, node: nodes.Global) -> None:
for name in node.names:
self._check_name("const", name, node)
@utils.check_messages("non-ascii-name")
def visit_assignname(self, node: nodes.AssignName) -> None:
"""Check module level assigned names."""
# The NameChecker from which this Checker originates knows a lot of different
# versions of variables, i.e. constants, inline variables etc.
# To simplify we use only `variable` here, as we don't need to apply different
# rules to different types of variables.
frame = node.frame()
if isinstance(frame, nodes.FunctionDef):
if node.parent in frame.body:
# Only perform the check if the assignment was done in within the body
# of the function (and not the function parameter definition
# (will be handled in visit_functiondef)
# or within a decorator (handled in visit_call)
self._check_name("variable", node.name, node)
elif isinstance(frame, nodes.ClassDef):
self._check_name("attr", node.name, node)
else:
# Possibilities here:
# - isinstance(node.assign_type(), nodes.Comprehension) == inlinevar
# - isinstance(frame, nodes.Module) == variable (constant?)
# - some other kind of assigment missed but still most likely a variable
self._check_name("variable", node.name, node)
@utils.check_messages("non-ascii-name")
def visit_classdef(self, node: nodes.ClassDef) -> None:
self._check_name("class", node.name, node)
for attr, anodes in node.instance_attrs.items():
if not any(node.instance_attr_ancestors(attr)):
self._check_name("attr", attr, anodes[0])
def _check_module_import(self, node: Union[nodes.ImportFrom, nodes.Import]) -> None:
for module_name, alias in node.names:
name = alias or module_name
self._check_name("module", name, node)
@utils.check_messages("non-ascii-name")
def visit_import(self, node: nodes.Import) -> None:
self._check_module_import(node)
@utils.check_messages("non-ascii-name")
def visit_importfrom(self, node: nodes.ImportFrom) -> None:
self._check_module_import(node)
@utils.check_messages("non-ascii-name")
def visit_call(self, node: nodes.Call) -> None:
"""Check if the used keyword args are correct."""
for keyword in node.keywords:
self._check_name("argument", keyword.arg, keyword)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 120 | _check_name | ref | function | self._check_name("function", node.name, node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 128 | _check_name | ref | function | self._check_name("argument", pos_only_arg.name, pos_only_arg)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 133 | _check_name | ref | function | self._check_name("argument", arg.name, arg)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 138 | _check_name | ref | function | self._check_name("argument", kwarg.name, kwarg)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 142 | check_messages | ref | function | @utils.check_messages("non-ascii-name")
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 143 | visit_global | def | function | def visit_global(self, node: nodes.Global) -> None:
for name in node.names:
self._check_name("const", name, node)
@utils.check_messages("non-ascii-name")
def visit_assignname(self, node: nodes.AssignName) -> None:
"""Check module level assigned names."""
# The NameChecker from which this Checker originates knows a lot of different
# versions of variables, i.e. constants, inline variables etc.
# To simplify we use only `variable` here, as we don't need to apply different
# rules to different types of variables.
frame = node.frame()
if isinstance(frame, nodes.FunctionDef):
if node.parent in frame.body:
# Only perform the check if the assignment was done in within the body
# of the function (and not the function parameter definition
# (will be handled in visit_functiondef)
# or within a decorator (handled in visit_call)
self._check_name("variable", node.name, node)
elif isinstance(frame, nodes.ClassDef):
self._check_name("attr", node.name, node)
else:
# Possibilities here:
# - isinstance(node.assign_type(), nodes.Comprehension) == inlinevar
# - isinstance(frame, nodes.Module) == variable (constant?)
# - some other kind of assigment missed but still most likely a variable
self._check_name("variable", node.name, node)
@utils.check_messages("non-ascii-name")
def visit_classdef(self, node: nodes.ClassDef) -> None:
self._check_name("class", node.name, node)
for attr, anodes in node.instance_attrs.items():
if not any(node.instance_attr_ancestors(attr)):
self._check_name("attr", attr, anodes[0])
def _check_module_import(self, node: Union[nodes.ImportFrom, nodes.Import]) -> None:
for module_name, alias in node.names:
name = alias or module_name
self._check_name("module", name, node)
@utils.check_messages("non-ascii-name")
def visit_import(self, node: nodes.Import) -> None:
self._check_module_import(node)
@utils.check_messages("non-ascii-name")
def visit_importfrom(self, node: nodes.ImportFrom) -> None:
self._check_module_import(node)
@utils.check_messages("non-ascii-name")
def visit_call(self, node: nodes.Call) -> None:
"""Check if the used keyword args are correct."""
for keyword in node.keywords:
self._check_name("argument", keyword.arg, keyword)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 145 | _check_name | ref | function | self._check_name("const", name, node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 147 | check_messages | ref | function | @utils.check_messages("non-ascii-name")
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 148 | visit_assignname | def | function | def visit_assignname(self, node: nodes.AssignName) -> None:
"""Check module level assigned names."""
# The NameChecker from which this Checker originates knows a lot of different
# versions of variables, i.e. constants, inline variables etc.
# To simplify we use only `variable` here, as we don't need to apply different
# rules to different types of variables.
frame = node.frame()
if isinstance(frame, nodes.FunctionDef):
if node.parent in frame.body:
# Only perform the check if the assignment was done in within the body
# of the function (and not the function parameter definition
# (will be handled in visit_functiondef)
# or within a decorator (handled in visit_call)
self._check_name("variable", node.name, node)
elif isinstance(frame, nodes.ClassDef):
self._check_name("attr", node.name, node)
else:
# Possibilities here:
# - isinstance(node.assign_type(), nodes.Comprehension) == inlinevar
# - isinstance(frame, nodes.Module) == variable (constant?)
# - some other kind of assigment missed but still most likely a variable
self._check_name("variable", node.name, node)
@utils.check_messages("non-ascii-name")
def visit_classdef(self, node: nodes.ClassDef) -> None:
self._check_name("class", node.name, node)
for attr, anodes in node.instance_attrs.items():
if not any(node.instance_attr_ancestors(attr)):
self._check_name("attr", attr, anodes[0])
def _check_module_import(self, node: Union[nodes.ImportFrom, nodes.Import]) -> None:
for module_name, alias in node.names:
name = alias or module_name
self._check_name("module", name, node)
@utils.check_messages("non-ascii-name")
def visit_import(self, node: nodes.Import) -> None:
self._check_module_import(node)
@utils.check_messages("non-ascii-name")
def visit_importfrom(self, node: nodes.ImportFrom) -> None:
self._check_module_import(node)
@utils.check_messages("non-ascii-name")
def visit_call(self, node: nodes.Call) -> None:
"""Check if the used keyword args are correct."""
for keyword in node.keywords:
self._check_name("argument", keyword.arg, keyword)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 154 | frame | ref | function | frame = node.frame()
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 162 | _check_name | ref | function | self._check_name("variable", node.name, node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 164 | _check_name | ref | function | self._check_name("attr", node.name, node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 170 | _check_name | ref | function | self._check_name("variable", node.name, node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 172 | check_messages | ref | function | @utils.check_messages("non-ascii-name")
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 173 | visit_classdef | def | class | |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 174 | _check_name | ref | class | |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 176 | instance_attr_ancestors | ref | function | if not any(node.instance_attr_ancestors(attr)):
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 177 | _check_name | ref | function | self._check_name("attr", attr, anodes[0])
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 179 | _check_module_import | def | function | def _check_module_import(self, node: Union[nodes.ImportFrom, nodes.Import]) -> None:
for module_name, alias in node.names:
name = alias or module_name
self._check_name("module", name, node)
@utils.check_messages("non-ascii-name")
def visit_import(self, node: nodes.Import) -> None:
self._check_module_import(node)
@utils.check_messages("non-ascii-name")
def visit_importfrom(self, node: nodes.ImportFrom) -> None:
self._check_module_import(node)
@utils.check_messages("non-ascii-name")
def visit_call(self, node: nodes.Call) -> None:
"""Check if the used keyword args are correct."""
for keyword in node.keywords:
self._check_name("argument", keyword.arg, keyword)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 182 | _check_name | ref | function | self._check_name("module", name, node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 184 | check_messages | ref | function | @utils.check_messages("non-ascii-name")
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 185 | visit_import | def | function | def visit_import(self, node: nodes.Import) -> None:
self._check_module_import(node)
@utils.check_messages("non-ascii-name")
def visit_importfrom(self, node: nodes.ImportFrom) -> None:
self._check_module_import(node)
@utils.check_messages("non-ascii-name")
def visit_call(self, node: nodes.Call) -> None:
"""Check if the used keyword args are correct."""
for keyword in node.keywords:
self._check_name("argument", keyword.arg, keyword)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 186 | _check_module_import | ref | function | self._check_module_import(node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 188 | check_messages | ref | function | @utils.check_messages("non-ascii-name")
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 189 | visit_importfrom | def | function | def visit_importfrom(self, node: nodes.ImportFrom) -> None:
self._check_module_import(node)
@utils.check_messages("non-ascii-name")
def visit_call(self, node: nodes.Call) -> None:
"""Check if the used keyword args are correct."""
for keyword in node.keywords:
self._check_name("argument", keyword.arg, keyword)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 190 | _check_module_import | ref | function | self._check_module_import(node)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 192 | check_messages | ref | function | @utils.check_messages("non-ascii-name")
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 193 | visit_call | def | function | def visit_call(self, node: nodes.Call) -> None:
"""Check if the used keyword args are correct."""
for keyword in node.keywords:
self._check_name("argument", keyword.arg, keyword)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 196 | _check_name | ref | function | self._check_name("argument", keyword.arg, keyword)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 199 | register | def | function | def register(linter: lint.PyLinter) -> None:
linter.register_checker(NonAsciiNameChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 200 | register_checker | ref | function | linter.register_checker(NonAsciiNameChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/non_ascii_names.py | pylint/checkers/non_ascii_names.py | 200 | NonAsciiNameChecker | ref | function | linter.register_checker(NonAsciiNameChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/raw_metrics.py | pylint/checkers/raw_metrics.py | 36 | report_raw_stats | def | function | def report_raw_stats(
sect,
stats: LinterStats,
old_stats: Optional[LinterStats],
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/raw_metrics.py | pylint/checkers/raw_metrics.py | 50 | diff_string | ref | function | diff_str = diff_string(old, total) if old else None
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/raw_metrics.py | pylint/checkers/raw_metrics.py | 58 | Table | ref | function | sect.append(Table(children=lines, cols=5, rheaders=1))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/raw_metrics.py | pylint/checkers/raw_metrics.py | 61 | RawMetricsChecker | def | class | __init__ open process_tokens |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/raw_metrics.py | pylint/checkers/raw_metrics.py | 86 | reset_code_count | ref | function | self.linter.stats.reset_code_count()
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/raw_metrics.py | pylint/checkers/raw_metrics.py | 88 | process_tokens | def | function | def process_tokens(self, tokens):
"""Update stats."""
i = 0
tokens = list(tokens)
while i < len(tokens):
i, lines_number, line_type = get_type(tokens, i)
self.linter.stats.code_type_count["total"] += lines_number
self.linter.stats.code_type_count[line_type] += lines_number
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/raw_metrics.py | pylint/checkers/raw_metrics.py | 93 | get_type | ref | function | i, lines_number, line_type = get_type(tokens, i)
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/raw_metrics.py | pylint/checkers/raw_metrics.py | 101 | get_type | def | function | def get_type(tokens, start_index):
"""Return the line type : docstring, comment, code, empty."""
i = start_index
start = tokens[i][2]
pos = start
line_type = None
while i < len(tokens) and tokens[i][2][0] == start[0]:
tok_type = tokens[i][0]
pos = tokens[i][3]
if line_type is None:
if tok_type == tokenize.STRING:
line_type = "docstring"
elif tok_type == tokenize.COMMENT:
line_type = "comment"
elif tok_type in JUNK:
pass
else:
line_type = "code"
i += 1
if line_type is None:
line_type = "empty"
elif i < len(tokens) and tokens[i][0] == tokenize.NEWLINE:
i += 1
return i, pos[0] - start[0] + 1, line_type
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/raw_metrics.py | pylint/checkers/raw_metrics.py | 127 | register | def | function | def register(linter: "PyLinter") -> None:
linter.register_checker(RawMetricsChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/raw_metrics.py | pylint/checkers/raw_metrics.py | 128 | register_checker | ref | function | linter.register_checker(RawMetricsChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/raw_metrics.py | pylint/checkers/raw_metrics.py | 128 | RawMetricsChecker | ref | function | linter.register_checker(RawMetricsChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/__init__.py | pylint/checkers/refactoring/__init__.py | 60 | register | def | function | def register(linter: "PyLinter") -> None:
linter.register_checker(RefactoringChecker(linter))
linter.register_checker(NotChecker(linter))
linter.register_checker(RecommendationChecker(linter))
linter.register_checker(ImplicitBooleanessChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/__init__.py | pylint/checkers/refactoring/__init__.py | 61 | register_checker | ref | function | linter.register_checker(RefactoringChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/__init__.py | pylint/checkers/refactoring/__init__.py | 61 | RefactoringChecker | ref | function | linter.register_checker(RefactoringChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/__init__.py | pylint/checkers/refactoring/__init__.py | 62 | register_checker | ref | function | linter.register_checker(NotChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/__init__.py | pylint/checkers/refactoring/__init__.py | 62 | NotChecker | ref | function | linter.register_checker(NotChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/__init__.py | pylint/checkers/refactoring/__init__.py | 63 | register_checker | ref | function | linter.register_checker(RecommendationChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/__init__.py | pylint/checkers/refactoring/__init__.py | 63 | RecommendationChecker | ref | function | linter.register_checker(RecommendationChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/__init__.py | pylint/checkers/refactoring/__init__.py | 64 | register_checker | ref | function | linter.register_checker(ImplicitBooleanessChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/__init__.py | pylint/checkers/refactoring/__init__.py | 64 | ImplicitBooleanessChecker | ref | function | linter.register_checker(ImplicitBooleanessChecker(linter))
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py | pylint/checkers/refactoring/implicit_booleaness_checker.py | 11 | ImplicitBooleanessChecker | def | class | visit_call instance_has_bool visit_unaryop visit_compare _check_use_implicit_booleaness_not_comparison base_names_of_instance |
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py | pylint/checkers/refactoring/implicit_booleaness_checker.py | 77 | check_messages | ref | function | @utils.check_messages("use-implicit-booleaness-not-len")
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py | pylint/checkers/refactoring/implicit_booleaness_checker.py | 78 | visit_call | def | function | def visit_call(self, node: nodes.Call) -> None:
# a len(S) call is used inside a test condition
# could be if, while, assert or if expression statement
# e.g. `if len(S):`
if not utils.is_call_of_name(node, "len"):
return
# the len() call could also be nested together with other
# boolean operations, e.g. `if z or len(x):`
parent = node.parent
while isinstance(parent, nodes.BoolOp):
parent = parent.parent
# we're finally out of any nested boolean operations so check if
# this len() call is part of a test condition
if not utils.is_test_condition(node, parent):
return
len_arg = node.args[0]
generator_or_comprehension = (
nodes.ListComp,
nodes.SetComp,
nodes.DictComp,
nodes.GeneratorExp,
)
if isinstance(len_arg, generator_or_comprehension):
# The node is a generator or comprehension as in len([x for x in ...])
self.add_message("use-implicit-booleaness-not-len", node=node)
return
try:
instance = next(len_arg.infer())
except astroid.InferenceError:
# Probably undefined-variable, abort check
return
mother_classes = self.base_names_of_instance(instance)
affected_by_pep8 = any(
t in mother_classes for t in ("str", "tuple", "list", "set")
)
if "range" in mother_classes or (
affected_by_pep8 and not self.instance_has_bool(instance)
):
self.add_message("use-implicit-booleaness-not-len", node=node)
@staticmethod
def instance_has_bool(class_def: nodes.ClassDef) -> bool:
try:
class_def.getattr("__bool__")
return _True
except astroid.AttributeInferenceError:
...
return _False
@utils.check_messages("use-implicit-booleaness-not-len")
def visit_unaryop(self, node: nodes.UnaryOp) -> None:
"""`not len(S)` must become `not S` regardless if the parent block
is a test condition or something else (boolean expression)
e.g. `if not len(S):`
"""
if (
isinstance(node, nodes.UnaryOp)
and node.op == "not"
and utils.is_call_of_name(node.operand, "len")
):
self.add_message("use-implicit-booleaness-not-len", node=node)
@utils.check_messages("use-implicit-booleaness-not-comparison")
def visit_compare(self, node: nodes.Compare) -> None:
self._check_use_implicit_booleaness_not_comparison(node)
def _check_use_implicit_booleaness_not_comparison(
self, node: nodes.Compare
) -> None:
"""Check for left side and right side of the node for empty literals."""
is_left_empty_literal = utils.is_base_container(
node.left
) or utils.is_empty_dict_literal(node.left)
# Check both left-hand side and right-hand side for literals
for operator, comparator in node.ops:
is_right_empty_literal = utils.is_base_container(
comparator
) or utils.is_empty_dict_literal(comparator)
# Using Exclusive OR (XOR) to compare between two side.
# If two sides are both literal, it should be different error.
if is_right_empty_literal ^ is_left_empty_literal:
# set target_node to opposite side of literal
target_node = node.left if is_right_empty_literal else comparator
literal_node = comparator if is_right_empty_literal else node.left
# Infer node to check
target_instance = utils.safe_infer(target_node)
if target_instance is None:
continue
mother_classes = self.base_names_of_instance(target_instance)
is_base_comprehension_type = any(
t in mother_classes for t in ("tuple", "list", "dict", "set")
)
# Only time we bypass check is when target_node is not inherited by
# collection literals and have its own __bool__ implementation.
if not is_base_comprehension_type and self.instance_has_bool(
target_instance
):
continue
# No need to check for operator when visiting compare node
if operator in {"==", "!=", ">=", ">", "<=", "<"}:
collection_literal = "{}"
if isinstance(literal_node, nodes.List):
collection_literal = "[]"
if isinstance(literal_node, nodes.Tuple):
collection_literal = "()"
instance_name = "x"
if isinstance(target_node, nodes.Call) and target_node.func:
instance_name = f"{target_node.func.as_string()}(...)"
elif isinstance(target_node, (nodes.Attribute, nodes.Name)):
instance_name = target_node.as_string()
original_comparison = (
f"{instance_name} {operator} {collection_literal}"
)
suggestion = (
f"{instance_name}"
if operator == "!="
else f"not {instance_name}"
)
self.add_message(
"use-implicit-booleaness-not-comparison",
args=(
original_comparison,
suggestion,
),
node=node,
)
@staticmethod
def base_names_of_instance(
node: Union[bases.Uninferable, bases.Instance]
) -> List[str]:
"""Return all names inherited by a class instance or those returned by a function.
The inherited names include 'object'.
"""
if isinstance(node, bases.Instance):
return [node.name] + [x.name for x in node.ancestors()]
return []
|
playground/e9b22a58-260b-483f-88d7-7a5fe9f8b1d4/pylint/pylint/checkers/refactoring/implicit_booleaness_checker.py | pylint/checkers/refactoring/implicit_booleaness_checker.py | 82 | is_call_of_name | ref | function | if not utils.is_call_of_name(node, "len"):
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.