X7ROOT File Manager
Current Path:
/opt/cloudlinux/venv/lib/python3.11/site-packages/pylint/extensions
opt
/
cloudlinux
/
venv
/
lib
/
python3.11
/
site-packages
/
pylint
/
extensions
/
📁
..
📄
__init__.py
(575 B)
📁
__pycache__
📄
_check_docs_utils.py
(25.74 KB)
📄
bad_builtin.py
(2.22 KB)
📄
broad_try_clause.py
(2.25 KB)
📄
check_elif.py
(2.09 KB)
📄
code_style.py
(12.51 KB)
📄
comparetozero.py
(3.09 KB)
📄
comparison_placement.py
(2.3 KB)
📄
confusing_elif.py
(1.99 KB)
📄
consider_refactoring_into_while_condition.py
(3.23 KB)
📄
consider_ternary_expression.py
(1.66 KB)
📄
dict_init_mutate.py
(2.06 KB)
📄
docparams.py
(25.3 KB)
📄
docstyle.py
(2.87 KB)
📄
dunder.py
(2.33 KB)
📄
empty_comment.py
(1.91 KB)
📄
emptystring.py
(2.9 KB)
📄
eq_without_hash.py
(1.42 KB)
📄
for_any_all.py
(5.69 KB)
📄
magic_value.py
(4.14 KB)
📄
mccabe.py
(6.89 KB)
📄
no_self_use.py
(3.62 KB)
📄
overlapping_exceptions.py
(3.26 KB)
📄
private_import.py
(10.97 KB)
📄
redefined_loop_name.py
(3.14 KB)
📄
redefined_variable_type.py
(4 KB)
📄
set_membership.py
(1.75 KB)
📄
typing.py
(19.91 KB)
📄
while_used.py
(1.07 KB)
Editing: redefined_variable_type.py
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE # Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt from __future__ import annotations from typing import TYPE_CHECKING from astroid import nodes from pylint.checkers import BaseChecker from pylint.checkers.utils import is_none, node_type, only_required_for_messages if TYPE_CHECKING: from pylint.lint import PyLinter class MultipleTypesChecker(BaseChecker): """Checks for variable type redefinition (NoneType excepted). At a function, method, class or module scope This rule could be improved: - Currently, if an attribute is set to different types in 2 methods of a same class, it won't be detected (see functional test) - One could improve the support for inference on assignment with tuples, ifexpr, etc. Also, it would be great to have support for inference on str.split() """ name = "multiple_types" msgs = { "R0204": ( "Redefinition of %s type from %s to %s", "redefined-variable-type", "Used when the type of a variable changes inside a " "method or a function.", ) } def visit_classdef(self, _: nodes.ClassDef) -> None: self._assigns.append({}) @only_required_for_messages("redefined-variable-type") def leave_classdef(self, _: nodes.ClassDef) -> None: self._check_and_add_messages() visit_functiondef = visit_asyncfunctiondef = visit_classdef leave_functiondef = leave_asyncfunctiondef = leave_module = leave_classdef def visit_module(self, _: nodes.Module) -> None: self._assigns: list[dict[str, list[tuple[nodes.Assign, str]]]] = [{}] def _check_and_add_messages(self) -> None: assigns = self._assigns.pop() for name, args in assigns.items(): if len(args) <= 1: continue orig_node, orig_type = args[0] # Check if there is a type in the following nodes that would be # different from orig_type. for redef_node, redef_type in args[1:]: if redef_type == orig_type: continue # if a variable is defined to several types in an if node, # this is not actually redefining. orig_parent = orig_node.parent redef_parent = redef_node.parent if isinstance(orig_parent, nodes.If): if orig_parent == redef_parent: if ( redef_node in orig_parent.orelse and orig_node not in orig_parent.orelse ): orig_node, orig_type = redef_node, redef_type continue elif isinstance( redef_parent, nodes.If ) and redef_parent in orig_parent.nodes_of_class(nodes.If): orig_node, orig_type = redef_node, redef_type continue orig_type = orig_type.replace("builtins.", "") redef_type = redef_type.replace("builtins.", "") self.add_message( "redefined-variable-type", node=redef_node, args=(name, orig_type, redef_type), ) break def visit_assign(self, node: nodes.Assign) -> None: # we don't handle multiple assignment nor slice assignment target = node.targets[0] if isinstance(target, (nodes.Tuple, nodes.Subscript)): return # ignore NoneType if is_none(node): return _type = node_type(node.value) if _type: self._assigns[-1].setdefault(target.as_string(), []).append( (node, _type.pytype()) ) def register(linter: PyLinter) -> None: linter.register_checker(MultipleTypesChecker(linter))
Upload File
Create Folder