Ethan Furman added the comment:
The other option is to continue to inherit from `str`, but override the
`__str__` method:
class MyEnum(str, enum.Enum):
#
def __str__(self):
return self.value
--
___
Python tracker
Brett Cannon added the comment:
Karthikeyan is right and this is working as expected. If you want the semantics
you're after you can either implement __fspath__ as was suggested or get the
'value' attribute of the enum when constructing your path.
--
nosy: +brett.cannon
resolution:
Karthikeyan Singaravelan added the comment:
As per the fspath PEP https://www.python.org/dev/peps/pep-0519/#c-api the below
precedence is set. So for the enum inheriting from str the object itself is
returned and MyEnum.RED.__str__ is used returning MyEnum.RED. You can remove
inheritance
New submission from Andrew Ni :
import os
import pathlib
import enum
class MyEnum(str, enum.Enum):
RED = 'red'
# this resolves to: '/Users/niandrew/MyEnum.RED'
# EXPECTED: '/Users/niandrew/red'
str(pathlib.Path.home() / MyEnum.RED)
# this resolves to: '/Users/niandrew/red'