This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Consider adding __slots__ to enums?
Type: enhancement Stage: resolved
Components: Versions: Python 3.11
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: AlexWaygood, christian.heimes, ethan.furman, rhettinger
Priority: normal Keywords:

Created on 2021-12-19 13:47 by AlexWaygood, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg408898 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2021-12-19 13:47
Attempting to create an enum with __slots__ silently fails. No error is raised if __slots__ are specified, but the usual behaviour of __slots__ does not work as expected. Attributes that are not specified in __slots__ can be freely set:


>>> from enum import Enum
>>> class Color(Enum):
...     __slots__ = ()
...     RED = 0
...     BLUE = 1
...
>>> Color.RED.foo = 'bar'
>>>


Given that enums are rather special, I didn't exactly *expect* this to work -- but it might be good to raise some kind of error if a user attempts to specify __slots__, instead of having it fail silently.
msg408935 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2021-12-19 22:57
The primary purpose of __slots__ is not to limit attribute assignment. Slots are useful to define types that have a smaller memory footprint than types.

You are getting the expected behavior. Enum parent class does not have __slots__ and therefore automatically has a __dict__. You could make an argument to add __slots__ to Enum, but that would be a new feature for 3.11.
msg408936 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2021-12-19 23:14
Ah, of course, I'm an idiot — I forgot that a class could not prevent the creation of __dict__ unless the parent class also had __slots__. Thanks, Christian.

In which case: consider this a feature request to consider adding __slots__ to enum.Enum: for faster attribute access, lower memory footprint, and the ability to define __slots__ in custom Enum classes.
msg408937 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-12-20 00:58
> In which case: consider this a feature request to 
> consider adding __slots__ ...

A few thoughts:
* Enumerations tend to be small, so a space savings likely isn't relevant.
* In Python 3.11, the speed advantage of slots is now much smaller.
* The code for Enum is already complex and has a lot of magic.  Adding slots to the equation may make it even harder to reason about the internals.
* It not even clear that __slots__ would or could play nice with existing code.  Even if it does, it may tie our hands for other avenues of development such as a custom a __getattribute__ or use of @cached_property.

Please consider a more use case driven development approach to working on the core.  We have no known problems with Enum at this point that need to be solved.  A general notion that __slots__ should be used in more places is a reasonable topic for python-ideas.  The tracker is more suitable for targeted proposals like, "my company needs enums to work with tool x but it won't unless slot support is added", "my real-world app must support enormous enumerations that lead to memory problems unless slots are added", "i tried adding slots to Enum and found it was easy, didn't cause problems, and sped-up common cases by 22%".

I mostly agree with Christian except that limiting attribute access is a legitimate reason to use slots.  However, in the case of Enums that isn't a known issue.  Also, it is something that would need to done when a tool is first released.  Generally, we can't add it afterwards because that would be a breaking change (some applications may be legitimately adding additional attributes).

For now, I'll close this.  If Ethan thinks there is some fruit on this tree, he can reopen it.  Also if a specific real world use case arises, it can be reopened.
msg408944 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2021-12-20 05:33
Some testing reveals that `__slots__` is not a good option for Enum -- it makes it impossible to mix in in other types such as `int`.
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90290
2021-12-20 05:33:36ethan.furmansetmessages: + msg408944
2021-12-20 00:58:31rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg408937

resolution: rejected
stage: resolved
2021-12-19 23:14:50AlexWaygoodsettype: behavior -> enhancement
title: Attempting to create an enum with slots silently fails -> Consider adding __slots__ to enums?
messages: + msg408936
versions: - Python 3.9, Python 3.10
2021-12-19 22:57:17christian.heimessetnosy: + christian.heimes
messages: + msg408935
2021-12-19 17:39:21ethan.furmansetassignee: ethan.furman
2021-12-19 13:47:18AlexWaygoodcreate