[New-bugs-announce] [issue18989] reuse of enum names in class creation inconsistent

Ethan Furman report at bugs.python.org
Mon Sep 9 20:58:50 CEST 2013


New submission from Ethan Furman:

Consider:

==================================================================================
-->from enum import Enum

-->class Color(Enum):
...   red = 1
...   green = 2
...   blue = 3
...   red = 4
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in Color
  File "/home/ethan/source/python/issue18924/Lib/enum.py", line 87, in __setitem__
    raise TypeError('Attempted to reuse key: %r' % key)
TypeError: Attempted to reuse key: 'red'
==================================================================================

versus

==================================================================================
-->class Color(Enum):
...   red = 1
...   green = 2
...   blue = 3
...   def red(self):
...     return 'fooled ya!'
... 

# no error
==================================================================================

or

==================================================================================
-->class Color(Enum):
...   red = 1
...   green = 2
...   blue = 3
...   @property
...   def red(self):
...     return 'fooled ya!'
... 

# no error
==================================================================================

In both of the latter two cases the redefinition of 'red' is allowed because the new definition is not an enum member.

This is inconsistent as well as confusing.

I know normal class creation semantics don't place any such limitations on names and allow redefining at will, but Enum is not a regular class.

----------
assignee: ethan.furman
files: no_reuse_of_enum_names.stoneleaf.01.patch
keywords: patch
messages: 197375
nosy: barry, eli.bendersky, eric.snow, ethan.furman, ncoghlan, pitrou
priority: normal
severity: normal
status: open
title: reuse of enum names in class creation inconsistent
type: behavior
versions: Python 3.4
Added file: http://bugs.python.org/file31699/no_reuse_of_enum_names.stoneleaf.01.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18989>
_______________________________________


More information about the New-bugs-announce mailing list