diff options
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | pylint/checkers/design_analysis.py | 17 | ||||
-rw-r--r-- | pylint/test/functional/too_few_public_methods_37.py | 5 |
3 files changed, 26 insertions, 4 deletions
@@ -7,6 +7,10 @@ What's New in Pylint 2.2? Release date: TBA + * ``too-few-public-methods`` is not reported for ``typing.NamedTuple`` + + Close #2459 + * ```too-few-public-methods`` is not reported for dataclasses created with options. Close #2488 @@ -83,7 +87,7 @@ Release date: TBA * `unnecessary-pass` is now also emitted when a function or class contains only docstring and pass statement. - In Python, stubbed functions often have a body that contains just a single `pass` statement, + In Python, stubbed functions often have a body that contains just a single `pass` statement, indicating that the function doesn't do anything. However, a stubbed function can also have just a docstring, and function with a docstring and no body also does nothing. @@ -100,7 +104,7 @@ Release date: TBA * Consider tuples in exception handler for ``try-except-raise``. Close #2389 - * Fix astroid.ClassDef check in checkers.utils.is_subclass_of + * Fix astroid.ClassDef check in checkers.utils.is_subclass_of * Fix wildcard imports being ignored by the import checker diff --git a/pylint/checkers/design_analysis.py b/pylint/checkers/design_analysis.py index a762e269d..0c1a21cb9 100644 --- a/pylint/checkers/design_analysis.py +++ b/pylint/checkers/design_analysis.py @@ -26,7 +26,6 @@ from astroid import decorators from pylint.interfaces import IAstroidChecker from pylint.checkers import BaseChecker -from pylint.checkers import utils as checker_utils from pylint.checkers.utils import check_messages from pylint import utils @@ -93,6 +92,15 @@ MSGS = { SPECIAL_OBJ = re.compile("^_{2}[a-z]+_{2}$") DATACLASS_DECORATOR = "dataclass" 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: @@ -351,7 +359,12 @@ 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(node): + if ( + node.type != "class" + or _is_enum_class(node) + or _is_dataclass(node) + or _is_typing_namedtuple(node) + ): return # Does the class contain more than n public methods ? diff --git a/pylint/test/functional/too_few_public_methods_37.py b/pylint/test/functional/too_few_public_methods_37.py index 56b275f73..0bb3a3c16 100644 --- a/pylint/test/functional/too_few_public_methods_37.py +++ b/pylint/test/functional/too_few_public_methods_37.py @@ -1,4 +1,5 @@ # pylint: disable=missing-docstring +import typing import dataclasses from dataclasses import dataclass @@ -17,3 +18,7 @@ class ScheduledTxSearchModelOne: @dataclass(frozen=True) class Test: some_integer: int + + +class Example(typing.NamedTuple): + some_int: int |