django-extended-choices icon indicating copy to clipboard operation
django-extended-choices copied to clipboard

If constant starts with numeral, CHOICE.<CONSTANT> fails

Open lachlansimpson opened this issue 5 years ago • 1 comments

If the constant starts with a numeral (potentially other non-alpha too), the CHOICES.<CONSTANT> construct doesn't work.

Steps to reproduce.

>>> STATES = Choices(('ONLINE','1-2', 'Online'),('OFFLINE','2-1', 'Offline'),)
>>> STATES.constants
{'ONLINE': ('ONLINE', '1-2', 'Online'), 'OFFLINE': ('OFFLINE', '2-1', 'Offline')}
>>> STATES.ONLINE
'1-2'
>>> STATES = Choices(('1ONLINE','1-2', 'Online'),('2OFFLINE','2-1', 'Offline'),)
>>> STATES.constants
{'1ONLINE': ('1ONLINE', '1-2', 'Online'), '2OFFLINE': ('2OFFLINE', '2-1', 'Offline')}
>>> STATES.1ONLINE
  File "<console>", line 1
    STATES.1ONLINE
           ^
SyntaxError: invalid syntax

lachlansimpson avatar Jul 08 '19 03:07 lachlansimpson

It's logic because the goal of the constant is to create an attribute. So it will never be possible, but it should raise an error at definition time.

Thanks for the report

twidi avatar Jul 09 '19 16:07 twidi