From a36b9e7815c0cd9455757a2042287f34a209233d Mon Sep 17 00:00:00 2001 From: Dinesh Trivedi Date: Fri, 21 Jan 2022 15:02:16 +0200 Subject: [PATCH 1/7] 276 - Converted local function using closure to be callable class --- pylint_django/augmentations/__init__.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pylint_django/augmentations/__init__.py b/pylint_django/augmentations/__init__.py index 6bc99eff..6d051c1b 100644 --- a/pylint_django/augmentations/__init__.py +++ b/pylint_django/augmentations/__init__.py @@ -645,13 +645,18 @@ def is_model_test_case_subclass(node): return node_is_subclass(node, "django.test.testcases.TestCase") -def generic_is_view_attribute(parents, attrs): - """Generates is_X_attribute function for given parents and attrs.""" +class IsAttribute: + def __init__(self, parents, attrs): + self.parents = parents + self.attrs = attrs + + def __call__(self, node): + return _attribute_is_magic(node, self.attrs, self.parents) - def is_attribute(node): - return _attribute_is_magic(node, attrs, parents) - return is_attribute +def generic_is_view_attribute(parents, attrs) -> IsAttribute: + """Generates is_X_attribute function for given parents and attrs.""" + return IsAttribute(parents, attrs) def is_model_view_subclass_method_shouldnt_be_function(node): From 9033c72744ae2183f61e9e23030d2ffd75d09864 Mon Sep 17 00:00:00 2001 From: Dinesh Trivedi Date: Fri, 21 Jan 2022 16:14:32 +0200 Subject: [PATCH 2/7] 276 - Converted is_class local function using closure to be callable class --- pylint_django/augmentations/__init__.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pylint_django/augmentations/__init__.py b/pylint_django/augmentations/__init__.py index 6d051c1b..4e09e185 100644 --- a/pylint_django/augmentations/__init__.py +++ b/pylint_django/augmentations/__init__.py @@ -761,9 +761,17 @@ def allow_meta_protected_access(node): return False -def is_class(class_name): +class IsClass: + def __init__(self, class_name): + self.class_name = class_name + + def __call__(self, node): + return node_is_subclass(node, self.class_name) + + +def is_class(class_name) -> IsClass: """Shortcut for node_is_subclass.""" - return lambda node: node_is_subclass(node, class_name) + return IsClass(class_name) def wrap(orig_method, with_method): From fe95a123f3ed653255f5968f0f172eb5f06e80fc Mon Sep 17 00:00:00 2001 From: Dinesh Trivedi Date: Fri, 21 Jan 2022 16:19:35 +0200 Subject: [PATCH 3/7] 276 - Cleaning is_class method as now is unecessary --- pylint_django/augmentations/__init__.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/pylint_django/augmentations/__init__.py b/pylint_django/augmentations/__init__.py index 4e09e185..971e0ba6 100644 --- a/pylint_django/augmentations/__init__.py +++ b/pylint_django/augmentations/__init__.py @@ -769,11 +769,6 @@ def __call__(self, node): return node_is_subclass(node, self.class_name) -def is_class(class_name) -> IsClass: - """Shortcut for node_is_subclass.""" - return IsClass(class_name) - - def wrap(orig_method, with_method): @functools.wraps(orig_method) def wrap_func(*args, **kwargs): @@ -879,7 +874,7 @@ def apply_augmentations(linter): linter, MisdesignChecker.visit_classdef, "too-many-ancestors", - is_class("django.views.generic.edit.FormView"), + IsClass("django.views.generic.edit.FormView"), ) # class-based generic views just have a longer inheritance chain @@ -887,13 +882,13 @@ def apply_augmentations(linter): linter, MisdesignChecker.visit_classdef, "too-many-ancestors", - is_class("django.views.generic.detail.BaseDetailView"), + IsClass("django.views.generic.detail.BaseDetailView"), ) suppress_message( linter, MisdesignChecker.visit_classdef, "too-many-ancestors", - is_class("django.views.generic.edit.ProcessFormView"), + IsClass("django.views.generic.edit.ProcessFormView"), ) # model forms have no __init__ method anywhere in their bases @@ -901,7 +896,7 @@ def apply_augmentations(linter): linter, ClassChecker.visit_classdef, "W0232", - is_class("django.forms.models.ModelForm"), + IsClass("django.forms.models.ModelForm"), ) # Meta From 3e56dd2db8b2633d277d65c9016d88ed6197c015 Mon Sep 17 00:00:00 2001 From: Dinesh Trivedi Date: Fri, 21 Jan 2022 16:28:00 +0200 Subject: [PATCH 4/7] 276 - Added my user to contributors.md --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 276798e7..30f0b92e 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -19,3 +19,4 @@ * [michael-k](https://github.com/michael-k) * [naquiroz](https://github.com/naquiroz) * [john-sandall](https://github.com/john-sandall) +* [dineshtrivedi](https://github.com/dineshtrivedi) \ No newline at end of file From 200bee9c3d6cd4068c8c1b0801cc81f72ae3590c Mon Sep 17 00:00:00 2001 From: Dinesh Trivedi Date: Fri, 21 Jan 2022 16:29:58 +0200 Subject: [PATCH 5/7] 276 - Replaced unecessary generic_is_view_attribute function --- pylint_django/augmentations/__init__.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pylint_django/augmentations/__init__.py b/pylint_django/augmentations/__init__.py index 971e0ba6..68157d1a 100644 --- a/pylint_django/augmentations/__init__.py +++ b/pylint_django/augmentations/__init__.py @@ -654,11 +654,6 @@ def __call__(self, node): return _attribute_is_magic(node, self.attrs, self.parents) -def generic_is_view_attribute(parents, attrs) -> IsAttribute: - """Generates is_X_attribute function for given parents and attrs.""" - return IsAttribute(parents, attrs) - - def is_model_view_subclass_method_shouldnt_be_function(node): """Checks that node is a default http method (i.e get, post, put, and more) of the View class.""" if node.name not in View.http_method_names: @@ -866,7 +861,7 @@ def apply_augmentations(linter): linter, TypeChecker.visit_attribute, "no-member", - generic_is_view_attribute(parents, attrs), + IsAttribute(parents, attrs), ) # formviews have too many ancestors, there's nothing the user of the library can do about that From 50e1fff8002e9f1c50583321bdb696e8619c829c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 21 Jan 2022 14:31:04 +0000 Subject: [PATCH 6/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- CONTRIBUTORS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 30f0b92e..07160217 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -19,4 +19,4 @@ * [michael-k](https://github.com/michael-k) * [naquiroz](https://github.com/naquiroz) * [john-sandall](https://github.com/john-sandall) -* [dineshtrivedi](https://github.com/dineshtrivedi) \ No newline at end of file +* [dineshtrivedi](https://github.com/dineshtrivedi) From 96de0baed85581de71686957676ed46dd921f643 Mon Sep 17 00:00:00 2001 From: Dinesh Trivedi Date: Tue, 25 Jan 2022 12:13:32 +0200 Subject: [PATCH 7/7] 276 - Adding test to make sure the Pylint plugin is picklable --- pylint_django/tests/test_func.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pylint_django/tests/test_func.py b/pylint_django/tests/test_func.py index 4df4181a..185d707f 100644 --- a/pylint_django/tests/test_func.py +++ b/pylint_django/tests/test_func.py @@ -1,5 +1,6 @@ import csv import os +import pickle import sys import pylint @@ -112,5 +113,15 @@ def test_migrations_plugin(test_file): LintTest._runTest() +@pytest.mark.parametrize("test_file", MIGRATIONS_TESTS[:1], ids=MIGRATIONS_TESTS_NAMES[:1]) +def test_linter_should_be_pickleable_with_pylint_djang_plugin_installed(test_file): + LintTest = PylintDjangoMigrationsTest(test_file) + LintTest.setUp() + + # LintModuleTest sets reporter to instance of FunctionalTestReporter that is not picklable + LintTest._linter.reporter = None + pickle.dumps(LintTest._linter) + + if __name__ == "__main__": sys.exit(pytest.main(sys.argv))