Issue 33976: Enums don't support nested classes

Created on 2018-06-27 04:00 by edwardw, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 7950 closed edwardw, 2018-06-27 04:02
Messages (4)
msg320541 - (view) Author: Edward Wang (edwardw) * Date: 2018-06-27 04:00
Methods defined in Enums behave 'normally' but classes defined in Enums get mistaken for regular values and can't be used as classes out of the box.

```python
class Outer(Enum):
    a = 1
    b = 2
    class Inner(Enum):
        foo = 10
        bar = 11
```
msg320548 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2018-06-27 07:06
Edward, thank you for taking the time to submit a patch, complete with tests!

Do you have a real-world example of why Enum should work this way?
msg320550 - (view) Author: Edward Wang (edwardw) * Date: 2018-06-27 07:15
Ethan - thank you for your speedy response!

For an example you can see https://github.com/ucb-bar/hammer/blob/97021bc7e1c819747f8b8b6d4b8c76cdc6a488e3/src/hammer-vlsi/hammer_vlsi_impl.py#L195 - the ObstructionType enum is really only used inside PlacementConstraintType, so it was thought to be logical to nest ObstructionType inside PlacementConstraintType and reference it as PlacementConstraintType.ObstructionType.

It just seemed weird that inline class definitions inside an Enum get treated as an enum value while functions defined inside Enums are treated as methods - as a user it would make more sense to have them both treated the same way (not as enum values), I think.
msg348073 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2019-07-17 17:25
Edward,

I'm inclined to agree with you.  It will take a couple versions for deprecation, etc., to push this through (assuming nobody comes up with a good counter-argument).

In the mean-time, this change has landed in aenum [1] so you can use it now.


[1] https://pypi.org/project/aenum/
History
Date User Action Args
2022-04-11 14:59:02adminsetgithub: 78157
2019-07-17 17:25:12ethan.furmansetmessages: + msg348073
2018-06-27 07:15:40edwardwsetmessages: + msg320550
2018-06-27 07:06:49ethan.furmansetassignee: ethan.furman

messages: + msg320548
nosy: + barry, eli.bendersky, ethan.furman

2018-06-27 04:02:23edwardwsetkeywords: + patch
stage: patch review
pull_requests: + pull_request7557
2018-06-27 04:00:50edwardwcreate