Skip to content
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

pathlib '/' operator does not resolve Enums with str mixin as expected #83262

Closed
andrew-ni mannequin opened this issue Dec 17, 2019 · 4 comments
Closed

pathlib '/' operator does not resolve Enums with str mixin as expected #83262

andrew-ni mannequin opened this issue Dec 17, 2019 · 4 comments
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@andrew-ni
Copy link
Mannequin

andrew-ni mannequin commented Dec 17, 2019

BPO 39081
Nosy @brettcannon, @ethanfurman, @tirkarthi, @andrew-ni

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2019-12-18.17:31:06.241>
created_at = <Date 2019-12-17.23:37:20.537>
labels = ['3.7', 'invalid', 'type-bug', 'library']
title = "pathlib '/' operator does not resolve Enums with str mixin as expected"
updated_at = <Date 2019-12-18.17:48:52.115>
user = 'https://github.com/andrew-ni'

bugs.python.org fields:

activity = <Date 2019-12-18.17:48:52.115>
actor = 'ethan.furman'
assignee = 'none'
closed = True
closed_date = <Date 2019-12-18.17:31:06.241>
closer = 'brett.cannon'
components = ['Library (Lib)']
creation = <Date 2019-12-17.23:37:20.537>
creator = 'andrewni'
dependencies = []
files = []
hgrepos = []
issue_num = 39081
keywords = []
message_count = 4.0
messages = ['358598', '358615', '358633', '358637']
nosy_count = 4.0
nosy_names = ['brett.cannon', 'ethan.furman', 'xtreak', 'andrewni']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue39081'
versions = ['Python 3.7']

@andrew-ni
Copy link
Mannequin Author

andrew-ni mannequin commented Dec 17, 2019

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'
os.path.join(pathlib.Path.home(), MyEnum.RED)

@andrew-ni andrew-ni mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Dec 17, 2019
@tirkarthi
Copy link
Member

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.

# bpo-39081.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

@brettcannon
Copy link
Member

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.

@ethanfurman
Copy link
Member

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

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants