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

RelatedManager passing as a ManyToManyField #163

Closed
franbeep opened this issue Jun 3, 2018 · 4 comments
Closed

RelatedManager passing as a ManyToManyField #163

franbeep opened this issue Jun 3, 2018 · 4 comments

Comments

@franbeep
Copy link

franbeep commented Jun 3, 2018

First time using a linter (and issue) here. I've noticed that when using user_permissions from AbstractUser (that is supposed to be a ManyToMany, as stated by the linter) it is indeed a RelatedManager by some gimmick that I'm unawre of, so when I use user_permissions.clear() the linter states a error, when it is valid code.

python=3.6.5
pylint=1.91
pylint-django=0.11.1

ss

The class in which the method is defined just inherents user_permissions from AbstractUser.

So what should have been done? I really don't want to whitelist this error, neither just ignore.

@atodorov
Copy link
Contributor

atodorov commented Jun 6, 2018

Will inspect in the next few days when I have the time.

Next time please paste a plain text of your code/error and not a picture.

@franbeep
Copy link
Author

franbeep commented Jun 6, 2018

Next time please paste a plain text of your code/error and not a picture.

This code isn't entirely mine neither I have the authority to share it for the public.
I will try to reproduce the same error in a different project then I'll share with you.

@franbeep
Copy link
Author

franbeep commented Jun 9, 2018

I've created a repository to show you what I meant.

The error resides in users.models.CustomUser. The linter is confunsing user_permissions from PermissionsMixin (which is inhereted by AbstractUser) with the same named property from AbstractUser.

Here is the models.py:

from django.contrib.auth.models import AbstractUser, Permission

# Custom permissions for CustomUser
USER_PERMS = ['change_customuser', 'add_customuser']

class CustomUser(AbstractUser):
    class Meta:
        verbose_name = 'CustomUser'
        verbose_name_plural = 'CustomUsers'
        app_label = "users"

    def grant_permissions(self):
        ''' Example adding permissions to User '''
        self.user_permissions.clear() 
        for perm in USER_PERMS:
            perm = Permission.objects.get(codename=perm)
            self.user_permissions.add(perm)
        return self.user_permissions

    def save(self, *args, **kwargs):
        ''' Saving while granting new permissions '''
        self.is_staff = True
        super(CustomUser, self).save()
        self.grant_permissions()

@atodorov
Copy link
Contributor

The linter is confunsing user_permissions from PermissionsMixin (which is inhereted by AbstractUser) with the same named property from AbstractUser.

this doesn't sound right. From what I can see PermissionsMixin.user_permissions is a ManyToManyField. This property is not redefined inside the AbstractUser class again.

So from what I can tell pylint-django is doing the correct thing by recognizing that user_permissions is a ManyToManyField.

atodorov added a commit that referenced this issue Aug 2, 2018
Disable model-no-explicit-unicode for the test class because Django 1.11
defines the AbstractBaseUser with @python_2_unicode_compatible and
we don't care about this issue for this particular test.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants