Skip to content

Configuration class breaks MRO #329

@RJPercival

Description

@RJPercival

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions