Skip to content

Inconsistent behaviour of Enums which are also strings in/before 3.10 #135370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zahlman opened this issue Jun 10, 2025 · 1 comment
Closed

Inconsistent behaviour of Enums which are also strings in/before 3.10 #135370

zahlman opened this issue Jun 10, 2025 · 1 comment
Labels
type-bug An unexpected behavior, bug, or error

Comments

@zahlman
Copy link

zahlman commented Jun 10, 2025

Bug report

Bug description:

In 3.10, since StrEnum is not yet provided, I thought to emulate it by simply using str as a mixin:

>>> from enum import Enum
>>> class x(str, Enum):
...     KEY = 'value'
... 

The result seems mostly correct, but there is an inconsistency between various ways of converting the instance to an ordinary string:

>>> isinstance(x.KEY, str)
True
>>> '%s' % x.KEY
'x.KEY'
>>> f'{x.KEY}'
'value'
>>> '{}'.format(x.KEY)
'value'
>>> str(x.KEY)
'x.KEY'

Compare in 3.11, which is consistent when using this mixin approach:

>>> from enum import Enum
>>> class x(str, Enum):
...     KEY = 'value'
... 
>>> isinstance(x.KEY, str)
True
>>> '%s' % x.KEY
'x.KEY'
>>> f'{x.KEY}'
'x.KEY'
>>> '{}'.format(x.KEY)
'x.KEY'
>>> str(x.KEY)
'x.KEY'

And consistent the other way when using the new StrEnum:

>>> from enum import StrEnum
>>> class x(StrEnum):
...     KEY = 'value'
... 
>>> isinstance(x.KEY, str)
True
>>> '%s' % x.KEY
'value'
>>> f'{x.KEY}'
'value'
>>> '{}'.format(x.KEY)
'value'
>>> str(x.KEY)
'value'

I don't have a firm opinion on which is correct, but I don't think it should depend on the conversion method.

CPython versions tested on:

3.10

Operating systems tested on:

No response

@zahlman zahlman added the type-bug An unexpected behavior, bug, or error label Jun 10, 2025
@ZeroIntensity
Copy link
Member

3.10 and 3.11 are security-only versions, so we couldn't change anything there even if we wanted to.

@ZeroIntensity ZeroIntensity added the pending The issue will be closed if no feedback is provided label Jun 11, 2025
@serhiy-storchaka serhiy-storchaka closed this as not planned Won't fix, can't repro, duplicate, stale Jun 11, 2025
@ZeroIntensity ZeroIntensity removed the pending The issue will be closed if no feedback is provided label Jun 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants