Opened 3 years ago

Closed 3 years ago

#25571 closed Bug (fixed)

When number argument is not an integer ungettext_lazy's return value evaluates to False

Reported by: Michał Przybyś Owned by: nobody
Component: Internationalization Version: 1.8
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


When I tried to pass ungettext_lazy as the message argument for MinLengthValidator:

MinLengthValidator(4, ungettext_lazy(
    'Name has to be at least %(limit_value)d character long.',
    'Name has to be at least %(limit_value)d characters long.', 'limit_value'

the default message was still used. After fiddling around I found out, that it's because ungettext_lazy's return value's __len__ returns 0 which makes it evaluate to False and fail a check in BaseValidator's constructor.

>>> len(ungettext_lazy('%(number)d object', '%(number)d objects', 'number'))
>>> len(ungettext_lazy('%(number)d object', '%(number)d objects', 1))
>>> bool(ungettext_lazy('%(number)d object', '%(number)d objects', 'number'))
>>> bool(ungettext_lazy('%(number)d object', '%(number)d objects', 1))

I'm not sure if this behaviour is a bug, or is it by design and it's a bug in BaseValidator's constructor, but I assume it's the former.

Using django 1.8.5, checked on python 2.7.9 and 3.4.3.

Change History (3)

comment:1 Changed 3 years ago by Claude Paroz

Component: UtilitiesInternationalization
Has patch: set
Triage Stage: UnreviewedAccepted

Yes, that's a bug. Possible resolution: PR

comment:2 Changed 3 years ago by Tim Graham

Triage Stage: AcceptedReady for checkin

comment:3 Changed 3 years ago by Claude Paroz <claude@…>

Resolution: fixed
Status: newclosed

In 8b5acda:

Fixed #25571 -- Fixed boolean evaluation of ungettext_lazy

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