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

Field order not preserved during redefinition. #1321

Open
amcgregor opened this issue Jun 23, 2016 · 0 comments
Open

Field order not preserved during redefinition. #1321

amcgregor opened this issue Jun 23, 2016 · 0 comments

Comments

@amcgregor
Copy link
Contributor

amcgregor commented Jun 23, 2016

There's a use case where subclassing is used for specialization and overriding of default values.

Take this example:

from mongoengine import Document, StringField

class Asset(Document):
    meta = dict(allow_inheritance=True)
    handler = StringField(default="web.component.asset:AssetController")
    path = StringField()

class Page(Asset):
    handler = StringField(default="web.component.page:PageController")

However, there is a bit of an issue that crops up when you have defined multiple fields, then override one this way.

print(Page._fields_ordered)
# ('id', 'path', 'handler', '_cls')

My own declarative schema system uses this chunk of metaclass to avoid this issue: https://github.com/marrow/schema/blob/develop/marrow/schema/meta.py#L60-L95 (apologies for the metaclass bit being exceptionally difficult to grok Python code… nature of the beast.)

Very specifically, this explicitly preserves declaration order for redefined fields:

from marrow.schema import Container, Attribute

class Foo(Container):
    handler = Attribute()
    path = Attribute()

class Bar(Foo):
    handler = Attribute()

print(Bar.__attributes__)
# handler, path - order preserved despite the redefinition getting a later sequence ID

This metaclass example may provide an approach to resolve 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

No branches or pull requests

1 participant