Inconsistency in Enum with class mixing string formatting in Python 3.11

99 Views Asked by At

I have been following the back and forth on Enum formatting in Python 3.11, see https://github.com/python/cpython/issues/100458.

However I have a few mixins with classes that implement __str__ and have been seeing inconsistent behavior among the minor versions of 3.11, specifically 3.11.0 - 3.11.3.

With the below script, run on a few different late 3.10 to early 3.12:

import enum


class A:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def __str__(self):
        return f'{self.a}|{self.b}'


class Foo(A, enum.Enum):
    BAR = 'a', 'b'

import sys

print(sys.version)
print(Foo.BAR)
print(str(Foo.BAR))
print("%s" % Foo.BAR)
print(f"{Foo.BAR}")
print("{}".format(Foo.BAR))

Output:

3.10.13 (main, Oct 25 2023, 19:27:56) [Clang 15.0.0 (clang-1500.0.40.1)]
a|b
a|b
a|b
a|b
a|b

3.11.0 (main, Oct 25 2023, 19:22:31) [Clang 15.0.0 (clang-1500.0.40.1)]
Foo.BAR
Foo.BAR
Foo.BAR
Foo.BAR
Foo.BAR

3.11.3 (main, Oct 25 2023, 12:37:31) [Clang 15.0.0 (clang-1500.0.40.1)]
Foo.BAR
Foo.BAR
Foo.BAR
Foo.BAR
Foo.BAR

3.11.4 (main, Oct 25 2023, 00:36:48) [Clang 15.0.0 (clang-1500.0.40.1)]
a|b
a|b
a|b
a|b
a|b

3.11.6 (main, Oct 25 2023, 12:41:05) [Clang 15.0.0 (clang-1500.0.40.1)]
a|b
a|b
a|b
a|b
a|b

3.12.0 (main, Oct 25 2023, 19:30:35) [Clang 15.0.0 (clang-1500.0.40.1)]
a|b
a|b
a|b
a|b
a|b

I am just looking to confirm that the 3.11.0 - 3.11.3 versions are some kind of regression that was fixed and that the intended behavior is the 3.11.4+ behavior going forward.

1

There are 1 best solutions below

3
On BEST ANSWER

The 3.11.4+ behavior is correct.


Disclosure: I am the author of the Python stdlib Enum, the enum34 backport, and the Advanced Enumeration (aenum) library.