Skip to content
This repository was archived by the owner on Jul 18, 2024. It is now read-only.

"ValueError: too many values to unpack" with TextChoices #27

Closed
ievans3024 opened this issue Jun 3, 2021 · 8 comments
Closed

"ValueError: too many values to unpack" with TextChoices #27

ievans3024 opened this issue Jun 3, 2021 · 8 comments

Comments

@ievans3024
Copy link

ievans3024 commented Jun 3, 2021

django==3.0.5
pydantic==1.8.2
djantic=0.3.0

Attempting to create a djantic schema for a model that has a django.db.models.TextChoices subclass assigned to a field's choices will result in a ValueError

For example:

from django.db import models
from djantic import ModelSchema


class FoodChoices(models.TextChoices):
    BANANA = 'ba', 'A delicious yellow Banana'
    APPLE = 'ap', 'A delicious red Apple'


class A(models.Model):
    name = models.CharField(max_length=128)
    preferred_food = models.CharField(max_length=2, choices=FoodChoices.choices)


class ASchema(ModelSchema):
    class Config:
        model = A

the above results in:

...
/usr/local/lib/python3.8/site-packages/djantic/main.py in __new__(mcs, name, bases, namespace)
     95 
     96                     else:
---> 97                         python_type, pydantic_field = ModelSchemaField(field)
     98 
     99                     field_values[field_name] = (python_type, pydantic_field)

/usr/local/lib/python3.8/site-packages/djantic/fields.py in ModelSchemaField(field)
     85         if field.choices:
     86             enum_choices = {v: k for k, v in field.choices}
---> 87             python_type = Enum(  # type: ignore
     88                 f"{field.name.title().replace('_', '')}Enum",
     89                 enum_choices,

/usr/local/lib/python3.8/enum.py in __call__(cls, value, names, module, qualname, type, start)
    339             return cls.__new__(cls, value)
    340         # otherwise, functional API: we're creating a new Enum type
--> 341         return cls._create_(
    342                 value,
    343                 names,

/usr/local/lib/python3.8/enum.py in _create_(cls, class_name, names, module, qualname, type, start)
    461                 member_name, member_value = item, names[item]
    462             else:
--> 463                 member_name, member_value = item
    464             classdict[member_name] = member_value
    465         enum_class = metacls.__new__(metacls, class_name, bases, classdict)

ValueError: too many values to unpack (expected 2)
@jordaneremieff
Copy link
Owner

@ievans3024 I was unable to reproduce this issue using the sample you provided. I merged additional test coverage based on these values in #29, but the test suite is passing.

Can you re-confirm this issue using the main branch?

@kaletvintsev
Copy link

I try this, and it help me:

convert keys and values to str in ModelSchemaField function

enum_choices = {v: k for k, v in field.choices}

enum_choices = {str(v): str(k) for k, v in field.choices}

@jordaneremieff
Copy link
Owner

Hi @kaletvintsev, could you confirm if the issue is still occurring in the latest 0.3.1 release?

@jordaneremieff
Copy link
Owner

Closing this, please reopen if the issues are not resolved in the latest release.

@kaletvintsev
Copy link

Hi, @jordaneremieff, I think that it can still occur, since it occurs when using gettext_lazy from django.utils.translation

@jordaneremieff jordaneremieff reopened this Aug 4, 2021
@jordaneremieff
Copy link
Owner

@kaletvintsev can you post a minimal example of how I can recreate this issue?

@ievans3024
Copy link
Author

ievans3024 commented Aug 11, 2021

Given:

Python 3.9.6

Django==3.2.6
djantic==0.3.4
pydantic==1.8.2

Running this using python manage.py shell in an empty django project:

from django.db import models
from django.utils.translation import ugettext_lazy as _
from djantic import ModelSchema


class FoodChoices(models.TextChoices):
    BANANA = 'ba', _('A delicious yellow Banana')
    APPLE = 'ap', _('A delicious red Apple')


class A(models.Model):
    class Meta:
        app_label = 'djanticbugtest'
    name = models.CharField(max_length=128)
    preferred_food = models.CharField(max_length=2, choices=FoodChoices.choices)


class ASchema(ModelSchema):
    class Config:
        model = A

raised the error I reported:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/ian/PycharmProjects/djanticbugtest/venv/lib/python3.9/site-packages/djantic/main.py", line 100, in __new__
    python_type, pydantic_field = ModelSchemaField(field)
  File "/home/ian/PycharmProjects/djanticbugtest/venv/lib/python3.9/site-packages/djantic/fields.py", line 93, in ModelSchemaField
    python_type = Enum(  # type: ignore
  File "/usr/lib/python3.9/enum.py", line 386, in __call__
    return cls._create_(
  File "/usr/lib/python3.9/enum.py", line 508, in _create_
    member_name, member_value = item
ValueError: too many values to unpack (expected 2)

This is likely the missing component to my original report. I had experienced the issue in a project using lazy translation in the choices and did not think to test other scenarios.

Interestingly, django.utils.translation.gettext and django.utils.translation.ugettext work fine.

@jordaneremieff
Copy link
Owner

Released a fix in 0.3.5. Let me know if this does not resolve the issue @ievans3024.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants