Shared task
Fixed #30902 -- Added __str__() for model choice enums.
PR #11964 ↗ · django/django · · merged Oct 25, 2019 · +13 −1 · base fc2b1cc926e3
what a new run launched now would send
The value of a TextChoices/IntegerChoices field has a differing type
Description
If we create an instance of a model having a CharField or IntegerField with the keyword choices pointing to IntegerChoices or TextChoices, the value returned by the getter of the field will be of the same type as the one created by enum.Enum (enum value).
For example, this model:
from django.db import models
from django.utils.translation import gettext_lazy as _
class MyChoice(models.TextChoices):
FIRST_CHOICE = "first", _("The first choice, it is")
SECOND_CHOICE = "second", _("The second choice, it is")
class MyObject(models.Model):
my_str_value = models.CharField(max_length=10, choices=MyChoice.choices)
Then this test:
from django.test import TestCase
from testing.pkg.models import MyObject, MyChoice
class EnumTest(TestCase):
def setUp(self) -> None:
self.my_object = MyObject.objects.create(my_str_value=MyChoice.FIRST_CHOICE)
def test_created_object_is_str(self):
my_object = self.my_object
self.assertIsInstance(my_object.my_str_value, str)
self.assertEqual(str(my_object.my_str_value), "first")
def test_retrieved_object_is_str(self):
my_object = MyObject.objects.last()
self.assertIsInstance(my_object.my_str_value, str)
self.assertEqual(str(my_object.my_str_value), "first")
And then the results:
(django30-venv) ➜ django30 ./manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F.
======================================================================
FAIL: test_created_object_is_str (testing.tests.EnumTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/mikailkocak/Development/django30/testing/tests.py", line 14, in test_created_object_is_str
self.assertEqual(str(my_object.my_str_value), "first")
AssertionError: 'MyChoice.FIRST_CHOICE' != 'first'
- MyChoice.FIRST_CHOICE
+ first
----------------------------------------------------------------------
Ran 2 tests in 0.002s
FAILED (failures=1)
We notice when invoking __str__(...) we don't actually get the value property of the enum value which can lead to some unexpected issues, especially when communicating to an external API with a freshly created instance that will send MyEnum.MyValue, and the one that was retrieved would send my_value.
Work only inside this repository checkout. Make the code change the task
describes, keeping the diff focused — no drive-by refactors.
When you are done, leave your changes committed or in the working tree;
they are collected automatically.
Stay on this snapshot checkout (`task/ycb_django_pr11964`). Never checkout, pull, or rebase onto `main`. That branch is a README-only orphan.
Stay on this HEAD. Do not fetch another default branch. Push only on the Cursor-created `crazy-cursor/…` side branch from this HEAD.Some past runs of this task were launched with a different prompt (the prompt template changed since, or those runs predate this benchmark's stored prompt). Each run persists the exact prompt it sent at launch — that per-launch record is the audit trail; this page shows only the current one.
| Run | Model | Verdict |
|---|---|---|
| Aug 21, 12:31 UTC · completed | composer-2.5 | PASS |
| Aug 21, 12:31 UTC · completed | grok-4.6 | PASS |
| Aug 21, 12:31 UTC · completed | grok-4.6-low |
Powered by YourCodingBench — benchmark coding models on your own repo's commits. sign in
| Aug 21, 12:31 UTC · completed | grok-4.6-medium | PASS |
| Aug 21, 12:31 UTC · completed | grok-4.6-xhigh | PASS |
Binary verdicts from the pinned judge (D27). Full attempt detail — candidate diff, transcript, timings — lives on each run page's score matrix.