summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2019-09-24 08:45:26 +0200
committerClaudiu Popa <pcmanticore@gmail.com>2019-09-24 09:10:33 +0200
commit3e1f61d0df4ded6479c5b109d495b8a1cd2cde31 (patch)
treeb26632147ba3ef9a2841339eb4777f95bc6db887
parentab79ac0b53658fff9474d601baac5d6b831e5e5e (diff)
downloadpylint-git-3e1f61d0df4ded6479c5b109d495b8a1cd2cde31.tar.gz
Squash multiple checks for special classes into a single function
-rw-r--r--pylint/checkers/design_analysis.py61
1 files changed, 10 insertions, 51 deletions
diff --git a/pylint/checkers/design_analysis.py b/pylint/checkers/design_analysis.py
index 0cc5e5dd6..50d8eaa3e 100644
--- a/pylint/checkers/design_analysis.py
+++ b/pylint/checkers/design_analysis.py
@@ -93,53 +93,17 @@ DATACLASS_IMPORT = "dataclasses"
TYPING_NAMEDTUPLE = "typing.NamedTuple"
-def _is_typing_namedtuple(node: astroid.ClassDef) -> bool:
- """Check if a class node is a typing.NamedTuple class"""
- for base in node.ancestors():
- if base.qname() == TYPING_NAMEDTUPLE:
- return True
- return False
-
-
-def _is_enum_class(node: astroid.ClassDef) -> bool:
- """Check if a class definition defines an Enum class.
-
- :param node: The class node to check.
- :type node: astroid.ClassDef
-
- :returns: True if the given node represents an Enum class. False otherwise.
- :rtype: bool
- """
- for base in node.bases:
- try:
- inferred_bases = base.inferred()
- except astroid.InferenceError:
- continue
-
- for ancestor in inferred_bases:
- if not isinstance(ancestor, astroid.ClassDef):
- continue
-
- if ancestor.name == "Enum" and ancestor.root().name == "enum":
- return True
+def _is_exempt_from_public_methods(node: astroid.ClassDef) -> bool:
+ """Check if a class is exempt from too-few-public-methods"""
- return False
-
-
-def _is_dataclass_like(node: astroid.ClassDef) -> bool:
- """Check if a class definition defines a Python data class
-
- A list of decorator names are introspected, such as the builtin
- `dataclass` decorator, as well as the popular `attrs` one from
- the `attrs` library.
-
- :param node: The class node to check.
- :type node: astroid.ClassDef
+ # If it's a typing.Namedtuple or an Enum
+ for ancestor in node.ancestors():
+ if ancestor.name == "Enum" and ancestor.root().name == "enum":
+ return True
+ if ancestor.qname() == TYPING_NAMEDTUPLE:
+ return True
- :returns:
- `True` if the given node represents a dataclass class, `False` otherwise.
- :rtype: bool
- """
+ # Or if it's a dataclass
if not node.decorators:
return False
@@ -366,12 +330,7 @@ class MisdesignChecker(BaseChecker):
# Stop here for exception, metaclass, interface classes and other
# classes for which we don't need to count the methods.
- if (
- node.type != "class"
- or _is_enum_class(node)
- or _is_dataclass_like(node)
- or _is_typing_namedtuple(node)
- ):
+ if node.type != "class" or _is_exempt_from_public_methods(node):
return
# Does the class contain more than n public methods ?