Message358615
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 from str and implement __fspath__ for your enum class to be used as per fspath protocol.
> If the object is str or bytes, then allow it to pass through with
an incremented refcount. If the object defines __fspath__(), then
return the result of that method. All other types raise a TypeError.
# bpo39081.py
import os
import pathlib
import enum
class MyEnum(enum.Enum):
RED = 'red'
def __fspath__(self):
return self.name
print(os.fspath(MyEnum.RED))
print(pathlib.Path.home() / MyEnum.RED)
$ python3.8 bpo39081.py
RED
/Users/kasingar/RED |
|
Date |
User |
Action |
Args |
2019-12-18 07:40:18 | xtreak | set | recipients:
+ xtreak, ethan.furman, andrewni |
2019-12-18 07:40:18 | xtreak | set | messageid: <1576654818.41.0.0678326938839.issue39081@roundup.psfhosted.org> |
2019-12-18 07:40:18 | xtreak | link | issue39081 messages |
2019-12-18 07:40:18 | xtreak | create | |
|