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

Configuration class breaks MRO #329

Open
RJPercival opened this issue Feb 8, 2022 · 0 comments · May be fixed by #330
Open

Configuration class breaks MRO #329

RJPercival opened this issue Feb 8, 2022 · 0 comments · May be fixed by #330

Comments

@RJPercival
Copy link

RJPercival commented Feb 8, 2022

Python's MRO (Method Resolution Order), a.k.a. the C3 algorithm, should control how an attribute is looked up in a class hierarchy. However, if those classes inherit from configurations.Configuration, the attribute that is found by C3 changes. This appears to be a result of the ConfigurationBase metaclass, which copies attributes from parent classes onto the child class. See the examples below, which differ only by whether BaseConfig inherits from Configuration.

import configurations

class BaseConfig(configurations.Configuration):
    DEBUG = False

class TestConfig(BaseConfig):
    DEBUG = True

class ServiceConfig(BaseConfig):
    ...

class TestServiceConfig(ServiceConfig, TestConfig):
    ...

assert TestServiceConfig.DEBUG == True  # assertion fails
class BaseConfig:
    DEBUG = False

class TestConfig(BaseConfig):
    DEBUG = True

class ServiceConfig(BaseConfig):
    ...

class TestServiceConfig(ServiceConfig, TestConfig):
    ...

assert TestServiceConfig.DEBUG == True  # assertion passes

I find it surprising that the value of the DEBUG attribute is not what it should be, as defined by the C3 algorithm. I don't think the ConfigurationBase class should be copying attributes onto child classes. If it didn't, then the C3 algorithm would find the correct value for the DEBUG attribute.

@RJPercival RJPercival linked a pull request Feb 8, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant