Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

[Python-ideas] Make dataclass aware that it might be used with Enum

35 views
Skip to first unread message

Steve Jorgensen

unread,
Jul 6, 2022, 8:03:54 PM7/6/22
to python...@python.org
Perhaps, this has already been addressed in a newer release (?) but in Python 3.9, making `@dataclass` work with `Enum` is a bit awkward.

Currently, it order to make it work, I have to:
1. Pass `init=False` to `@dataclass` and hand-write the `__init__` method
2. Pass `repr=False` to `@dataclass` and use `Enum`'s representation or write a custom __repr__

Example:
In [72]: @dataclass(frozen=True, init=False, repr=False)
...: class Creature(Enum):
...: legs: int
...: size: str
...: Beetle = (6, 'small')
...: Dog = (4, 'medium')
...: def __init__(self, legs, size):
...: self.legs = legs
...: self.size = size
...:

In [73]: Creature.Dog
Out[73]: <Creature.Dog: (4, 'medium')>
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/EXPSE4KQYM5SWPFCWH4QPOTS6UCP5FNL/
Code of Conduct: http://python.org/psf/codeofconduct/

Steve Jorgensen

unread,
Jul 7, 2022, 12:03:47 PM7/7/22
to python...@python.org
Steve Jorgensen wrote:
> Perhaps, this has already been addressed in a newer release (?) but in Python 3.9, making `@dataclass` work with `Enum` is a bit awkward.
> Currently, it order to make it work, I have to:
> 1. Pass `init=False` to `@dataclass` and hand-write the `__init__` method
> 2. Pass `repr=False` to `@dataclass` and use `Enum`'s representation or write a custom __repr__
> Example:
> In [72]: @dataclass(frozen=True, init=False, repr=False)
> ...: class Creature(Enum):
> ...: legs: int
> ...: size: str
> ...: Beetle = (6, 'small')
> ...: Dog = (4, 'medium')
> ...: def __init__(self, legs, size):
> ...: self.legs = legs
> ...: self.size = size
> ...:
> In [73]: Creature.Dog
> Out[73]: <Creature.Dog: (4, 'medium')>

Actually, maybe these are fundamentally incompatible? `@dataclass` is a decorator, so it acts on the class after it was already defined, but `Enum` acts before that when `@dataclass` cannot have not generated the `__init__` yet. Right?
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/T775WMOLR6TNOXDAU37ZA2FKQB3SMJT6/

Steve Jorgensen

unread,
Jul 7, 2022, 9:24:41 PM7/7/22
to python...@python.org
After some playing around, I figured out a pattern that works without any changes to the implementations of `dataclass` or `Enum`, and I like this because it keeps the 2 kinds of concern separate. Maybe I'll try submitting an MR to add an example like this to the documentation for `Enum`.

In [1]: from dataclasses import dataclass

In [2]: from enum import Enum

In [3]: @dataclass(frozen=True)
...: class CreatureDataMixin:
...: size: str
...: legs: int
...:

In [4]: class Creature(CreatureDataMixin, Enum):
...: BEETLE = ('small', 6)
...: DOG = ('medium', 4)
...:

In [5]: Creature.DOG
Out[5]: Creature(size='medium', legs=4)
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/G2VALQ4RIVFKIOKVW4XZAHZMLSZWL2XS/

Barry Scott

unread,
Jul 8, 2022, 11:27:37 AM7/8/22
to Steve Jorgensen, python...@python.org


> On 8 Jul 2022, at 02:22, Steve Jorgensen <stev...@gmail.com> wrote:
>
> After some playing around, I figured out a pattern that works without any changes to the implementations of `dataclass` or `Enum`, and I like this because it keeps the 2 kinds of concern separate. Maybe I'll try submitting an MR to add an example like this to the documentation for `Enum`.
>
> In [1]: from dataclasses import dataclass
>
> In [2]: from enum import Enum
>
> In [3]: @dataclass(frozen=True)
> ...: class CreatureDataMixin:
> ...: size: str
> ...: legs: int
> ...:
>
> In [4]: class Creature(CreatureDataMixin, Enum):
> ...: BEETLE = ('small', 6)
> ...: DOG = ('medium', 4)
> ...:
>
> In [5]: Creature.DOG
> Out[5]: Creature(size='medium', legs=4)

Can't you define the type of size as an enum?
Using multiple inheritance seems like the wrong way to go.
What if you are 10 fields in the dataclass that are all enums?
That could get messy.

Disclaimer I have not used dataclass. Just thinking from OOD point of view.

Barry

> _______________________________________________
> Python-ideas mailing list -- python...@python.org
> To unsubscribe send an email to python-id...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at https://mail.python.org/archives/list/python...@python.org/message/G2VALQ4RIVFKIOKVW4XZAHZMLSZWL2XS/
> Code of Conduct: http://python.org/psf/codeofconduct/
>

_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/CBH24S2XG5EF2CQWER6BWLBSZEGK4Q7O/

Ethan Furman

unread,
Jul 8, 2022, 11:06:01 PM7/8/22
to python...@python.org
On 7/7/22 09:01, Steve Jorgensen wrote:

> Actually, maybe these are fundamentally incompatible?

Their intended use seems fundamentally incompatible:

- dataclass was designed for making many mutable records (hundreds, thousands, or more)
- enum was designed to make a handful of named constants (I haven't yet seen one with even a hundred elements)

The repr from a combined dataclass/enum looks like a dataclass, giving no clue that the object is an enum, and omitting
any information about which enum member it is and which enum it is from.

Given these conflicts of interest, I don't see any dataclass examples making it into the enum documentation.

--
~Ethan~
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/6CIPPTP2Z42GVUIVOUXZ7BW46DB5HWEZ/

Steve Jorgensen

unread,
Jul 9, 2022, 3:22:01 PM7/9/22
to python...@python.org
Ethan Furman wrote:
> On 7/7/22 09:01, Steve Jorgensen wrote:
> > Actually, maybe these are fundamentally incompatible?
> > Their intended use seems fundamentally incompatible:
> - dataclass was designed for making many mutable records (hundreds, thousands, or more)
> - enum was designed to make a handful of named constants (I haven't yet seen one with even a hundred elements)
> The repr from a combined dataclass/enum looks like a dataclass, giving no clue that the object is an enum, and omitting
> any information about which enum member it is and which enum it is from.
> Given these conflicts of interest, I don't see any dataclass examples making it into the enum documentation.
> --
> ~Ethan~

Per my subsequent self-reply, they are only incompatible when trying to do them at the same time in the same class definition. It works great to combine them by defining the dataclass as a mixin for the Enum class. Why would it not be good to include that as an example in the official docs, assuming (as I believe) that it is a particularly useful combination?
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/VFGXT4QOWYF3UJVWYOR54GNTKEG2XT7D/

Steve Jorgensen

unread,
Jul 9, 2022, 5:56:17 PM7/9/22
to python...@python.org
I don't think that dataclasses have the limited set of intended uses that you are interpreting them as having. To me, the fact that they can be frozen makes them a good fit with Enum.
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/V6U7UMQRTLDZ2W6SWREL472L6ZH7MHB5/

Barry Scott

unread,
Jul 10, 2022, 7:44:55 AM7/10/22
to Steve Jorgensen, python...@python.org


> On 9 Jul 2022, at 22:53, Steve Jorgensen <stev...@gmail.com> wrote:
>
> I don't think that dataclasses have the limited set of intended uses that you are interpreting them as having. To me, the fact that they can be frozen makes them a good fit with Enum.

Please quote the email that you are replying to.

It is usually considered a code smell to have a class that is two or more things.
This seems to be what you are trying to do.

How can one class be a set of fields and also the enum for one of its own fields?
I do not understand why this is resonable.

Barry



> _______________________________________________
> Python-ideas mailing list -- python...@python.org
> To unsubscribe send an email to python-id...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at https://mail.python.org/archives/list/python...@python.org/message/V6U7UMQRTLDZ2W6SWREL472L6ZH7MHB5/
> Code of Conduct: http://python.org/psf/codeofconduct/
>

_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/HZFZE3YMCMT6CF5VZHNM6ZJVDAX6LZXK/

David Mertz, Ph.D.

unread,
Jul 10, 2022, 10:11:32 AM7/10/22
to Barry Scott, Steve Jorgensen, python-ideas
I've seen this thread, and also wondered why anyone could EVER want a dataclass that is an enum.  Nothing I've seen in the thread gives me any hint about that, really.
--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.

Steve Jorgensen

unread,
Jul 10, 2022, 1:54:44 PM7/10/22
to python...@python.org
David Mertz, Ph.D. wrote:
> I've seen this thread, and also wondered why anyone could EVER want a
> dataclass that is an enum. Nothing I've seen in the thread gives me any
> hint about that, really.
> On Sun, Jul 10, 2022 at 7:44 AM Barry Scott ba...@barrys-emacs.org wrote:
> > On 9 Jul 2022, at 22:53, Steve Jorgensen stev...@gmail.com wrote:
> > I don't think that dataclasses have the limited set of intended uses
> > that you are interpreting them as having. To me, the fact that they can be
> > frozen makes them a good fit with Enum.
> > Please quote the email that you are replying to.
> > It is usually considered a code smell to have a class that is two or more
> > things.
> > This seems to be what you are trying to do.
> > How can one class be a set of fields and also the enum for one of its own
> > fields?
> > I do not understand why this is resonable.
> > Barry
> >
> > Python-ideas mailing list -- python...@python.org
> > To unsubscribe send an email to python-id...@python.org
> > https://mail.python.org/mailman3/lists/python-ideas.python.org/
> > Message archived at
> > https://mail.python.org/archives/list/python...@python.org/message/V6U7UM...
> > Code of Conduct: http://python.org/psf/codeofconduct/
> >
> > Python-ideas mailing list -- python...@python.org
> > To unsubscribe send an email to python-id...@python.org
> > https://mail.python.org/mailman3/lists/python-ideas.python.org/
> > Message archived at
> > https://mail.python.org/archives/list/python...@python.org/message/HZFZE3...
> > Code of Conduct: http://python.org/psf/codeofconduct/
> > --
> Keeping medicines from the bloodstreams of the sick; food
> from the bellies of the hungry; books from the hands of the
> uneducated; technology from the underdeveloped; and putting
> advocates of freedom in prisons. Intellectual property is
> to the 21st century what the slave trade was to the 16th.

Sorry, I don't know how I communicated that I was trying to have one class be a set of fields and also the enum for one of its own fields.

I'm really just wanting to have each member of the enum be an instance of a frozen dataclass. If an of the dataclass fields were of an enum type, then it would presumably not be for the same enum. In my example, none of the fields of the dataclass contains an enum. One contains a string, and the other contains an int.
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/KWL2FXQ2FKRMGBAB5PMR3GIRAQBC6CLR/

Chris Angelico

unread,
Jul 10, 2022, 7:33:56 PM7/10/22
to python...@python.org
Just throwing an idea out there, but would it work better to have an
enum-namedtuple instead?

ChrisA
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/YVF6OKL5UPCUWWU72R36WDIQ2KTWC5SF/

Michael Foord

unread,
Jul 12, 2022, 9:05:51 AM7/12/22
to Steve Jorgensen, Python-Ideas
On Fri, 8 Jul 2022 at 02:22, Steve Jorgensen <stev...@gmail.com> wrote:
After some playing around, I figured out a pattern that works without any changes to the implementations of `dataclass` or `Enum`, and I like this because it keeps the 2 kinds of concern separate. Maybe I'll try submitting an MR to add an example like this to the documentation for `Enum`.

In [1]: from dataclasses import dataclass

In [2]: from enum import Enum

In [3]: @dataclass(frozen=True)
   ...: class CreatureDataMixin:
   ...:     size: str
   ...:     legs: int
   ...:

In [4]: class Creature(CreatureDataMixin, Enum):
   ...:     BEETLE = ('small', 6)
   ...:     DOG = ('medium', 4)
   ...:

In [5]: Creature.DOG
Out[5]: Creature(size='medium', legs=4)

I really like this example. I love dataclasses and I love enums and it looks like they go together like peanut butter and chocolate.You get a free initialiser (__init__ method) and all the other goodness of dataclasses (which are really very good).

I tweeted your example and it got 29 likes :-)


Michael


--
Michael Foord
Python Consultant, Contractor and Trainer
https://agileabstractions.com/

Steve Jorgensen

unread,
Jul 12, 2022, 1:03:55 PM7/12/22
to python...@python.org
The only benefit I can think of for namedtuple vs a dataclass is compactness in memory, but the number of members of an enum is typically very small. I think the extra flexibility of a dataclass makes more desirable for this purpose.
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/F4YM66UAQ3GXXBIMPNX6MLEQA22K7UVL/

Michael Foord

unread,
Jul 12, 2022, 9:17:49 PM7/12/22
to Steve Jorgensen, Python-Ideas
The ability to unpack a namedtuple as an iterable is considered to be a great advantage the dataclass has over the named tuple because changing the number of members is a backwards incompatible change for namedtuple.

No reason in principle why a frozen dataclass should be less memory efficient than a namedtuple (?).

Michael

Ethan Furman

unread,
Jul 18, 2022, 1:38:43 AM7/18/22
to python...@python.org
On 7/6/22 17:01, Steve Jorgensen wrote:

> Perhaps, this has already been addressed in a newer release (?) but in Python 3.9, making
> `@dataclass` work with `Enum` is a bit awkward.
>
> Currently, it order to make it work, I have to:
> 1. Pass `init=False` to `@dataclass` and hand-write the `__init__` method
> 2. Pass `repr=False` to `@dataclass` and use `Enum`'s representation or write a custom __repr__
>
> Example:
> In [72]: @dataclass(frozen=True, init=False, repr=False)
> ...: class Creature(Enum):
> ...: legs: int
> ...: size: str
> ...: Beetle = (6, 'small')
> ...: Dog = (4, 'medium')
> ...: def __init__(self, legs, size):
> ...: self.legs = legs
> ...: self.size = size
> ...:
>
> In [73]: Creature.Dog
> Out[73]: <Creature.Dog: (4, 'medium')>

So why use dataclass then?

class Creature(Enum):
Beetle = (6, 'small')
Dog = (4, 'medium')
def __init__(self, legs, size):
self.legs = legs
self.size = size

and

>>> list(Creature)
[<Creature.Beetle: (6, 'small')>, <Creature.Dog: (4, 'medium')>]

>>> Creature.Beetle.size
'small'

>>> Creature.Beetle.legs
6

It looks like dataclass was just making you do a bunch of extra work.

--
~Ethan~
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/X5UCN42LFGSGISWD62SUP2BU4WDRGWDM/

Ethan Furman

unread,
Jul 18, 2022, 1:42:45 AM7/18/22
to python...@python.org
On 7/7/22 09:01, Steve Jorgensen wrote:

> Actually, maybe these are fundamentally incompatible? `@dataclass` is a decorator, so it
> acts on the class after it was already defined, but `Enum` acts before that when `@dataclass`
> cannot have not generated the `__init__` yet. Right?

Correct.

--
~Ethan~
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/TXBQEVYMFWDIKNXU6X6IASU22G42PWFG/

Ethan Furman

unread,
Jul 18, 2022, 1:54:12 AM7/18/22
to python...@python.org
On 7/7/22 18:22, Steve Jorgensen wrote:

> After some playing around, I figured out a pattern that works without any changes to the
> implementations of `dataclass` or `Enum`, and I like this because it keeps the 2 kinds of
> concern separate. Maybe I'll try submitting an MR to add an example like this to the
> documentation for `Enum`.
>
> In [1]: from dataclasses import dataclass
>
> In [2]: from enum import Enum
>
> In [3]: @dataclass(frozen=True)
> ...: class CreatureDataMixin:
> ...: size: str
> ...: legs: int
> ...:
>
> In [4]: class Creature(CreatureDataMixin, Enum):
> ...: BEETLE = ('small', 6)
> ...: DOG = ('medium', 4)
> ...:
>
> In [5]: Creature.DOG
> Out[5]: Creature(size='medium', legs=4)

I'm impressed that you found a way to make it work. Be aware that some of the bug-fixing in 3.11 has changed the
resulting repr() -- the above now looks like:

<Creature.DOG: CreatureDataMixin(size='medium', legs=4)>

It would be possible to have Enum check to see if the data type is a dataclass, and then see if the repr is set to be
automatically created:

>>> CreatureDataMixin.__dataclass_params__
_DataclassParams(init=True,repr=True,eq=True,order=False,unsafe_hash=False,frozen=True)


I'll have to look into that.

It does seem like a lot of extra work, or at least no less work, than just writing an `__init__` in the enum class directly.

--
~Ethan~
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/X2KBATIBRGAPSVYNAWX77WCRIPUMQKLX/

Ethan Furman

unread,
Jul 18, 2022, 1:57:52 AM7/18/22
to python...@python.org
On 7/8/22 19:50, Ethan Furman wrote:

> The repr from a combined dataclass/enum looks like a dataclass, giving no clue that the
> object is an enum, and omitting any information about which enum member it is and which
> enum it is from.

Fixed in 3.11: `<Creature.DOG: CreatureDataMixin(size='medium', legs=4)>`

--
~Ethan~
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/2CURYFJAFITUMBVTSJMVJSOBOVL2OXUA/

Ethan Furman

unread,
Jul 18, 2022, 2:04:20 AM7/18/22
to python...@python.org
On 7/9/22 12:19, Steve Jorgensen wrote:

> [...] It works great to combine them by defining the dataclass as a mixin for the Enum class. Why would
> it not be good to include that as an example in the official docs, assuming (as I believe) that it is a
> particularly useful combination?

Do you have some real-world examples that show this?

--
~Ethan~
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/OSGWU6QMAN34TNQHK2ZVN5K3EPKGRGZX/

Steve Jorgensen

unread,
Jul 20, 2022, 11:00:58 PM7/20/22
to python...@python.org
Ethan Furman wrote:
> On 7/9/22 12:19, Steve Jorgensen wrote:
> > [...] It works great to combine them by defining the dataclass as a mixin for the Enum class. Why would
> > it not be good to include that as an example in the official docs, assuming (as I believe) that it is a
> > particularly useful combination?
> > Do you have some real-world examples that show this?
> --
> ~Ethan~

I have only used it in 1 real-world case s far. It's a good use case but not a good example case. I'll keep using this pattern though, and I'll probably end up with a good example soonish.
_______________________________________________
Python-ideas mailing list -- python...@python.org
To unsubscribe send an email to python-id...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python...@python.org/message/FGK4R4ES3STAS2PZLYX5UOV5HZRIFSF2/
Reply all
Reply to author
Forward
0 new messages