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

[BUG] get_settings() does not return the inherited settings #644

Closed
piercsi opened this issue Aug 7, 2023 · 4 comments · Fixed by #960
Closed

[BUG] get_settings() does not return the inherited settings #644

piercsi opened this issue Aug 7, 2023 · 4 comments · Fixed by #960

Comments

@piercsi
Copy link

piercsi commented Aug 7, 2023

Describe the bug

When inheriting settings from a common class, get_settings() does not return the inherited settings.

To Reproduce

from beanie import Document, init_beanie
from motor.motor_asyncio import AsyncIOMotorClient
import asyncio
import datetime


class GlobalSettings(Document):
    class Settings:
        bson_encoders = {
            datetime.datetime: lambda x: x,
            datetime.date: lambda x: datetime.datetime.combine(x, datetime.time.min),
        }


class MyDates(GlobalSettings):
    d: datetime.date
    dt: datetime.datetime

    class Settings(GlobalSettings.Settings):
        name = 'mydates'


async def example():
    client = AsyncIOMotorClient("mongodb://localhost:27019/my_database")
    await init_beanie(database=client.get_database(), document_models=[MyDates])
    doc = MyDates(d=datetime.date.today(), dt=datetime.datetime.now())
    print(doc.Settings.bson_encoders)
    print(doc.get_settings().bson_encoders)


if __name__ == "__main__":
    asyncio.run(example())

Expected behavior

doc.get_settings().bson_encoders (in this case) should return the same as doc.Settings.bson_encoder, i.e. the values from the inherited class.

Additional context
If

    class Settings(GlobalSettings.Settings):
        name = 'mydates'

is removed, get_settings() works as expected.

@piercsi piercsi changed the title [BUG] [BUG] get_settings() does not return the inherited settings Aug 7, 2023
@roman-right
Copy link
Member

Hi! Thank you for the catch. I'll check it out in the next bug-fixing session. This or next week.

@roman-right roman-right added the bug Something isn't working label Aug 10, 2023
@roman-right
Copy link
Member

Hi! It looks like I cannot solve this. Overriding this behavior is possible using metaclasses. However, Pydantic already uses its metaclass there. Python doesn't allow metaclass conflicts.

If you have any ideas, please share.

@roman-right roman-right removed the bug Something isn't working label Aug 21, 2023
@piercsi
Copy link
Author

piercsi commented Aug 22, 2023

Thanks for taking a look. I guess all I am trying to achieve is to have some common settings to prevent duplicate code on each class as I have over 100 collections. Is there an alternative approach?

@piercsi
Copy link
Author

piercsi commented Aug 22, 2023

Ok so something simple like this seems to work:

bson_encoders = {
    datetime.datetime: lambda x: x,
    datetime.date: lambda x: datetime.datetime.combine(x, datetime.time.min),
}


class MyDates(Document):
    d: datetime.date
    dt: datetime.datetime

    class Settings:
        name = 'mydates'
        bson_encoders = bson_encoders

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.

2 participants