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

Instance of 'tuple' has no 'label' member (no-member) #263

Open
thibaut-pro opened this issue May 1, 2020 · 3 comments
Open

Instance of 'tuple' has no 'label' member (no-member) #263

thibaut-pro opened this issue May 1, 2020 · 3 comments

Comments

@thibaut-pro
Copy link

Accessing the .label member for a models.TextChoices enumeration type https://docs.djangoproject.com/en/3.0/ref/models/fields/#enumeration-types raises this pylint error: Instance of 'tuple' has no 'label' member (no-member). Is this something that would make sense to fix in pylint-django?

Repro:

class Student(models.Model):

    class YearInSchool(models.TextChoices):
        FRESHMAN = 'FR', _('Freshman')
        SOPHOMORE = 'SO', _('Sophomore')
        JUNIOR = 'JR', _('Junior')
        SENIOR = 'SR', _('Senior')
        GRADUATE = 'GR', _('Graduate')

    YearInSchool.FRESHMAN.label
@carlio
Copy link
Collaborator

carlio commented May 1, 2020

It could be done in pylint-django, the best thing to do I would say is to add a transform to convert properties of models.TextChoices classes to some faked class. Though I don't know what that class does, I'd never heard of it and always used django-model-utils for that kind of thing :-)

@atodorov
Copy link
Contributor

atodorov commented May 2, 2020

From the docs:

A .label property is added on values, to return the human-readable name.

A number of custom properties are added to the enumeration classes – .choices, .labels, .values, and .names – to make it easier to access lists of those separate parts of the enumeration. Use .choices as a suitable value to pass to choices in a field definition.

I guess that we'll have to apply the same transformation like Django does. FTR I also haven't used these fields before. Looks easy but will probably take a day of work. PRs are welcome.

moritz89 added a commit to moritz89/pylint-django that referenced this issue Dec 6, 2020
Why:

- Test that the fields label and value are not marked as missing

This change addresses the need by:

- Adding a test to ensure that pylint does not mark these as errors
@moritz89
Copy link

moritz89 commented Dec 6, 2020

I've created a test case. Coule you give me a pointer where I would add the attirbute to the TextChoices tuples?

moritz89 added a commit to moritz89/pylint-django that referenced this issue Jan 14, 2021
Why:

- Transform tuples to include value and label properties

This change addresses the need by:

- Adding the text_choices transform file
- Integrating in the transforms init file
- Focusing on the predicate for the rule to trigger the transform
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

4 participants