pylint-django icon indicating copy to clipboard operation
pylint-django copied to clipboard

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

Open thibaut-pro opened this issue 5 years ago • 3 comments

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

thibaut-pro avatar May 01 '20 22:05 thibaut-pro

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 :-)

carlio avatar May 01 '20 23:05 carlio

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.

atodorov avatar May 02 '20 18:05 atodorov

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

moritz89 avatar Dec 06 '20 20:12 moritz89