Skip to content

Commit

Permalink
Merge branch 'main' into dataclass-typecheck-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
david-yz-liu authored Aug 19, 2021
2 parents f9019c7 + 441ec27 commit 2c93bed
Show file tree
Hide file tree
Showing 32 changed files with 377 additions and 398 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ Release date: TBA

Closes #4653

* Improve performance when inferring ``Call`` nodes, by utilizing caching.

* Improve error message for invalid-metaclass when the node is an Instance.


Expand Down
13 changes: 5 additions & 8 deletions pylint/checkers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@
from pylint import utils as lint_utils
from pylint.checkers import utils
from pylint.checkers.utils import (
infer_all,
is_overload_stub,
is_property_deleter,
is_property_setter,
)
from pylint.constants import BUILTINS
from pylint.reporters.ureports import nodes as reporter_nodes


Expand Down Expand Up @@ -179,7 +179,7 @@ class AnyStyle(NamingStyle):
TYPECHECK_COMPARISON_OPERATORS = frozenset(("is", "is not", "==", "!="))
LITERAL_NODE_TYPES = (nodes.Const, nodes.Dict, nodes.List, nodes.Set)
UNITTEST_CASE = "unittest.case"
TYPE_QNAME = "%s.type" % BUILTINS
TYPE_QNAME = "builtins.type"
ABC_METACLASSES = {"_py_abc.ABCMeta", "abc.ABCMeta"} # Python 3.7+,

# Name categories that are always consistent with all naming conventions.
Expand All @@ -189,7 +189,7 @@ class AnyStyle(NamingStyle):
# about dangerous default values as arguments
DEFAULT_ARGUMENT_SYMBOLS = dict(
zip(
[".".join([BUILTINS, x]) for x in ("set", "dict", "list")],
[".".join(["builtins", x]) for x in ("set", "dict", "list")],
["set()", "{}", "[]"],
),
**{
Expand Down Expand Up @@ -783,11 +783,8 @@ def visit_call(self, node):
"""Check instantiating abstract class with
abc.ABCMeta as metaclass.
"""
try:
for inferred in node.func.infer():
self._check_inferred_class_is_abstract(inferred, node)
except astroid.InferenceError:
return
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):
Expand Down
6 changes: 3 additions & 3 deletions pylint/checkers/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def _is_attribute_property(name, klass):
attributes = klass.getattr(name)
except astroid.NotFoundError:
return False
property_name = f"{astroid.bases.BUILTINS}.property"
property_name = "builtins.property"
for attr in attributes:
if attr is astroid.Uninferable:
continue
Expand Down Expand Up @@ -880,7 +880,7 @@ def _check_proper_bases(self, node):
if not ancestor:
continue
if isinstance(ancestor, astroid.Instance) and ancestor.is_subtype_of(
f"{astroid.bases.BUILTINS}.type"
"builtins.type"
):
continue

Expand Down Expand Up @@ -2175,7 +2175,7 @@ def _check_unexpected_method_signature(self, node):
# by no-method-argument.
return

if decorated_with(node, [astroid.bases.BUILTINS + ".staticmethod"]):
if decorated_with(node, ["builtins.staticmethod"]):
# We expect to not take in consideration self.
all_args = node.args.args
else:
Expand Down
15 changes: 6 additions & 9 deletions pylint/checkers/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from astroid import nodes

from pylint.checkers import utils
from pylint.checkers.utils import get_import_name, safe_infer
from pylint.checkers.utils import get_import_name, infer_all, safe_infer

ACCEPTABLE_NODES = (
astroid.BoundMethod,
Expand Down Expand Up @@ -58,14 +58,11 @@ class DeprecatedMixin:
"deprecated-class",
)
def visit_call(self, node: nodes.Call) -> None:
"""Called when a :class:`.astroid.Call` node is visited."""
try:
self.check_deprecated_class_in_call(node)
for inferred in node.func.infer():
# Calling entry point for deprecation check logic.
self.check_deprecated_method(node, inferred)
except astroid.InferenceError:
pass
"""Called when a :class:`nodes.Call` node is visited."""
self.check_deprecated_class_in_call(node)
for inferred in infer_all(node.func):
# Calling entry point for deprecation check logic.
self.check_deprecated_method(node, inferred)

@utils.check_messages(
"deprecated-module",
Expand Down
27 changes: 12 additions & 15 deletions pylint/checkers/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from pylint import checkers, interfaces
from pylint.checkers import utils
from pylint.checkers.utils import check_messages
from pylint.checkers.utils import check_messages, infer_all

MSGS = { # pylint: disable=consider-using-namedtuple-or-dataclass
"W1201": (
Expand Down Expand Up @@ -205,20 +205,17 @@ def is_logging_name():
)

def is_logger_class():
try:
for inferred in node.func.infer():
if isinstance(inferred, astroid.BoundMethod):
parent = inferred._proxied.parent
if isinstance(parent, nodes.ClassDef) and (
parent.qname() == "logging.Logger"
or any(
ancestor.qname() == "logging.Logger"
for ancestor in parent.ancestors()
)
):
return True, inferred._proxied.name
except astroid.exceptions.InferenceError:
pass
for inferred in infer_all(node.func):
if isinstance(inferred, astroid.BoundMethod):
parent = inferred._proxied.parent
if isinstance(parent, nodes.ClassDef) and (
parent.qname() == "logging.Logger"
or any(
ancestor.qname() == "logging.Logger"
for ancestor in parent.ancestors()
)
):
return True, inferred._proxied.name
return False, None

if is_logging_name():
Expand Down
8 changes: 3 additions & 5 deletions pylint/checkers/python3.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,9 +979,7 @@ def visit_functiondef(self, node):
# classmethod 1 argument should cause a failure, if it is a
# staticmethod 0 arguments should cause a failure.
failing_arg_count = 1
if utils.decorated_with(
node, [astroid.bases.BUILTINS + ".staticmethod"]
):
if utils.decorated_with(node, ["builtins.staticmethod"]):
failing_arg_count = 0
if len(node.args.args) == failing_arg_count:
self.add_message("next-method-defined", node=node)
Expand Down Expand Up @@ -1119,7 +1117,7 @@ def _check_cmp_argument(self, node):
if not inferred:
return

builtins_list = f"{astroid.bases.BUILTINS}.list"
builtins_list = "builtins.list"
if isinstance(inferred, nodes.List) or inferred.qname() == builtins_list:
kwargs = node.keywords

Expand All @@ -1128,7 +1126,7 @@ def _check_cmp_argument(self, node):
if not inferred:
return

builtins_sorted = f"{astroid.bases.BUILTINS}.sorted"
builtins_sorted = "builtins.sorted"
if inferred.qname() == builtins_sorted:
kwargs = node.keywords

Expand Down
3 changes: 1 addition & 2 deletions pylint/checkers/refactoring/not_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from pylint import checkers, interfaces
from pylint.checkers import utils
from pylint.constants import BUILTINS


class NotChecker(checkers.BaseChecker):
Expand Down Expand Up @@ -40,7 +39,7 @@ class NotChecker(checkers.BaseChecker):
# not equivalent to "set(LEFT_VALS) > set(RIGHT_VALS)"
skipped_nodes = (nodes.Set,)
# 'builtins' py3, '__builtin__' py2
skipped_classnames = [f"{BUILTINS}.{qname}" for qname in ("set", "frozenset")]
skipped_classnames = [f"builtins.{qname}" for qname in ("set", "frozenset")]

@utils.check_messages("unneeded-not")
def visit_unaryop(self, node):
Expand Down
Loading

0 comments on commit 2c93bed

Please sign in to comment.