Skip to content

Importing a file with relative path and the same file with path from interpreter root creates Enums with different ids. #129335

@feature-engineer

Description

@feature-engineer

Bug report

Bug description:

The type of path used to import an Enum affects whether it equals itself or not.

Assume we have a tests directory in our project, and in it are enums1.py and test_enums.py

enums1.py:

import enum


class Color(enum.Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

tests_enums.py:

import enums1
import tests.enums1

d1 = {c: c.name for c in enums1.Color}
print(id(enums1.Color.RED))
print(id(tests.enums1.Color.RED))
print(enums1.Color.RED == tests.enums1.Color.RED)
print(d1[tests.enums1.Color.RED])

running test_enums.py results in two distrinct ids for Color.Red, it says enums1.Color.RED does not equal tests.enums1.Color.RED, and there's a KeyError on the final line.

Expected behavior:
The import path should not affect whether the same enum is equal to itself. there should have been two identical ids, Color.RED should equal Color.RED and there should not be a KeyError.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions