Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supress some protected-access warnings when django-simple-history is installed. #423

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions pylint_django/augmentations/__init__.py
Expand Up @@ -9,6 +9,7 @@
from astroid.nodes.scoped_nodes import Module
from astroid.objects import Super
from django import VERSION as django_version
from django.conf import settings
from django.utils import termcolors
from django.views.generic.base import ContextMixin, RedirectView, View
from django.views.generic.dates import DateMixin, DayMixin, MonthMixin, WeekMixin, YearMixin
Expand Down Expand Up @@ -752,6 +753,23 @@ def allow_meta_protected_access(node):
return False


def allow_simple_history_protected_access(assign_node):
# NOTE: Only consider the first assignment target, mirroring current pylint behavior.
# See pylint ClassChecker::visit_assign().
# Because the type of assign_target typically cannot be inferred, this will suppress
# the warning even for assignments on objects not related to simple_history.
assign_target = assign_node.targets[0]
assign_target_attrname = getattr(assign_target, "attrname", None)

if assign_target_attrname is None or assign_target_attrname not in ("_change_reason",):
return False

if "simple_history" not in settings.INSTALLED_APPS:
return False

return True


class IsClass: # pylint: disable=too-few-public-methods
def __init__(self, class_name):
self.class_name = class_name
Expand Down Expand Up @@ -968,6 +986,14 @@ def apply_augmentations(linter):
is_model_mpttmeta_subclass,
)

# django-simple-history
suppress_message(
linter,
ClassChecker.visit_assign,
"protected-access",
allow_simple_history_protected_access,
)

# factory_boy's DjangoModelFactory
suppress_message(linter, TypeChecker.visit_attribute, "no-member", is_model_factory)
suppress_message(
Expand Down