Example code:
{{{
from django.forms.fields import DateField
print(repr(DateField().input_formats))
print(DateField().input_formats)
}}}
This will result in the following error:
{{{
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: __str__ returned non-string (type list)
}}}
I would have expected the string representation to be available as well
instead of failing with an internal Python error.
--
Ticket URL: <https://code.djangoproject.com/ticket/34445>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by David Sanders):
Hi, thanks for the report!
Interesting:
{{{
> lazy_val = lazy(get_format, str, list, tuple)("DATE_INPUT_FORMATS")
> lazy_val.__str__()
['%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', '%b %d %Y', '%b %d, %Y', '%d %b %Y',
'%d %b, %Y', '%B %d %Y', '%B %d, %Y', '%d %B %Y', '%d %B, %Y']
> str(lazy_val)
*** TypeError: __str__ returned non-string (type list)
}}}
The `__str__()` on the proxy object doesn't seem to be called when
evaluated through `str()` 🤔
--
Ticket URL: <https://code.djangoproject.com/ticket/34445#comment:1>
* type: Uncategorized => Bug
* component: Uncategorized => Utilities
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/34445#comment:2>
* owner: nobody => David Sanders
* status: new => assigned
Comment:
PR: https://github.com/django/django/pull/16701
--
Ticket URL: <https://code.djangoproject.com/ticket/34445#comment:3>
* has_patch: 0 => 1
* stage: Accepted => Ready for checkin
Comment:
[https://github.com/django/django/pull/16707 Alternative PR].
--
Ticket URL: <https://code.djangoproject.com/ticket/34445#comment:4>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"066aabcb77579cf8d549119c860d11cd15e3eef1" 066aabc]:
{{{
#!CommitTicketReference repository=""
revision="066aabcb77579cf8d549119c860d11cd15e3eef1"
Fixed #34445 -- Fixed string-casting of non-string lazy objects.
This removes __text_cast() as it's the same as __cast().
_delegate_bytes and __delegate_text are mutually exclusive so the
`if self._delegate_bytes` branch in __cast() is unreachable.
Co-Authored-By: David Sanders <shang.xia...@gmail.com>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34445#comment:5>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"f5817c24f44aa90188757ddca1e5e63ba6f5df75" f5817c2]:
{{{
#!CommitTicketReference repository=""
revision="f5817c24f44aa90188757ddca1e5e63ba6f5df75"
Refs #34445 -- Fixed string-casting of non-string lazy objects when value
may be bytes.
If the result type is bytes, then calling bytes() on it does nothing.
If the result type is not bytes, we should not cast to bytes, just
because the return value may be bytes.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34445#comment:6>