From fa5c813c78baecb8684eb9acabab411762e76c87 Mon Sep 17 00:00:00 2001 From: Xavier Francisco <98830734+XF-FW@users.noreply.github.com> Date: Tue, 29 Mar 2022 13:16:22 +0000 Subject: [PATCH 1/2] Add "no-member" suppression in WSGIRequest inside Django Test Cases Fixes #332 Supresses "no-member" for attributes of a WSGIRequest, that is inside a class that inherits from django.test.testcases.SimpleTestCase. Let me know if you need me to change anything. Thank you. --- pylint_django/augmentations/__init__.py | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/pylint_django/augmentations/__init__.py b/pylint_django/augmentations/__init__.py index e067709d..6a2ddc0e 100644 --- a/pylint_django/augmentations/__init__.py +++ b/pylint_django/augmentations/__init__.py @@ -797,6 +797,39 @@ def is_wsgi_application(node): ) +def is_wsgi_request(node): + def is_inside_test_case(): + test_case_parent = 'django.test.testcases.SimpleTestCase' + + for ancestor in node.node_ancestors(): + if getattr(ancestor, "type", None) == 'class': + for class_ancestor in ancestor.mro(): + if class_ancestor.qname() == test_case_parent: + return True + + return False + + + wsgi_request_parent = "django.core.handlers.wsgi.WSGIRequest" + + try: + parent_classes = node.expr.inferred() + except: # noqa: E722, pylint: disable=bare-except + return False + + for parent_class in parent_classes: + try: + if parent_class.qname() == wsgi_request_parent: + return True and is_inside_test_case() + + if node_is_subclass(parent_class, wsgi_request_parent): + return True and is_inside_test_case() + except AttributeError: + continue + + return False + + # Compat helpers def pylint_newstyle_classdef_compat(linter, warning_name, augment): if not hasattr(NewStyleConflictChecker, "visit_classdef"): @@ -991,5 +1024,6 @@ def apply_augmentations(linter): # wsgi.py suppress_message(linter, NameChecker.visit_assignname, "invalid-name", is_wsgi_application) + suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_wsgi_request) apply_wrapped_augmentations() From ef6608b843807cd88caf8b0444af584daee727f4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 29 Mar 2022 13:16:54 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pylint_django/augmentations/__init__.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pylint_django/augmentations/__init__.py b/pylint_django/augmentations/__init__.py index 6a2ddc0e..f56c6281 100644 --- a/pylint_django/augmentations/__init__.py +++ b/pylint_django/augmentations/__init__.py @@ -799,17 +799,16 @@ def is_wsgi_application(node): def is_wsgi_request(node): def is_inside_test_case(): - test_case_parent = 'django.test.testcases.SimpleTestCase' + test_case_parent = "django.test.testcases.SimpleTestCase" for ancestor in node.node_ancestors(): - if getattr(ancestor, "type", None) == 'class': + if getattr(ancestor, "type", None) == "class": for class_ancestor in ancestor.mro(): if class_ancestor.qname() == test_case_parent: return True return False - wsgi_request_parent = "django.core.handlers.wsgi.WSGIRequest" try: