Constants must be equal no matter how they are loaded #11

Closed
oblalex opened this Issue Aug 1, 2015 · 0 comments

Comments

Projects
None yet
1 participant
@oblalex
Owner

oblalex commented Aug 1, 2015

Assume you have the following package structure:

.
└── x
    ├── __init__.py
    └── y
        ├── __init__.py
        └── constants.py

where x.y.constants.py contains the following:

# -*- coding: utf-8 -*-

from candv import Constants, SimpleConstant


class FOO_TYPES(Constants):
    PRIMARY = SimpleConstant()
    SECONDARY = SimpleConstant()

If x is in Python's path, and you import constants from x.y and simply from y, then their module will differ and comparison of constants will fail:

>>> import sys
>>> sys.path.insert(0, './x')
>>> from x.y.constants import FOO_TYPES as X_TYPES
>>> from y.constants import FOO_TYPES as Y_TYPES
>>> X_TYPES.__module__
'x.y.constants'
>>> Y_TYPES.__module__
'y.constants'
>>> X_TYPES.PRIMARY
<constant 'FOO_TYPES.PRIMARY'>
>>> Y_TYPES.PRIMARY
<constant 'FOO_TYPES.PRIMARY'>
>>> X_TYPES.PRIMARY == Y_TYPES.PRIMARY
False

Solution: constants base must implement own __hash__ and __eq__ magic methods, whose result will be based on full name of constant.

@oblalex oblalex added the bug label Aug 1, 2015

@oblalex oblalex closed this in edc0cd5 Aug 1, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment