Opened 21 months ago

Closed 21 months ago

Last modified 19 months ago

#34445 closed Bug (fixed)

DateField.input_formats cannot be printed

Reported by: stefan6419846 Owned by: David Sanders
Component: Utilities Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I am trying to display the input formats for django.forms.fields.DateField. This seems to fail at the moment when using __str__, while __repr__ works.

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.

Change History (6)

comment:1 by David Sanders, 21 months ago

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)
> type(lazy_val.__str__())
<class 'list'>

Confirmed __str__() is returning a list.

Last edited 21 months ago by David Sanders (previous) (diff)

comment:2 by David Sanders, 21 months ago

Component: UncategorizedUtilities
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:3 by David Sanders, 21 months ago

Owner: changed from nobody to David Sanders
Status: newassigned

comment:4 by Mariusz Felisiak, 21 months ago

Has patch: set
Triage Stage: AcceptedReady for checkin

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 21 months ago

Resolution: fixed
Status: assignedclosed

In 066aabc:

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.xiao.sanders@…>

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 19 months ago

In f5817c2:

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.

Note: See TracTickets for help on using tickets.
Back to Top