Skip to content

Misleading documentation: Beanie does not support extending settings via inheritance #1405

@dotKokott

Description

@dotKokott

Describe the bug

https://fastapi-users.github.io/fastapi-users/latest/configuration/databases/beanie/

In the documentation for how to configure the User model for Beanie the documentation mentions that BeanieBaseUser is pre-configured to create a unique index on id and email but that the consumer of this library can extend the settings by

If you want to add your own custom settings to your User document model - like changing the collection name - don't forget to let your inner Settings class inherit the pre-defined settings from BeanieBaseUser like this: class Settings(BeanieBaseUser.Settings): # ...! See Beanie's documentation on Settings for details.

This is wrong. See BeanieODM/beanie#644.

Apart from being misleading, it actually creates an annoying issue: We cannot adjust the collection name without overriding BeanieBaseUsers setting. We have to copy the default indexing settings over to keep them.

To Reproduce

class SettingsA(Document):
    class Settings:
        name = "A"
        max_nesting_depth = 2
        bson_encoders = {IPv4Address: int}

class SettingsB(SettingsA):
    class Settings(SettingsA.Settings):
        name = "B"

async def test_can_inherit_settings():
    await init_beanie(database=db, document_models=[SettingsA, SettingsB]) 
    doc_settings_b = SettingsB.get_settings()

    assert doc_settings_b.max_nesting_depth == 2 # wrong, is set to default value 3
    assert doc_settings_b.bson_encoders == {IPv4Address: int} # wrong, is set to default value {}

Expected behavior

  • The documentation should mention that Settings are not inheritable
  • The documentation ideally shows an alternative way on how to configure the User model collection name without loosing the BeanieBaseUser default settings.

Configuration

  • Python version : 3.12
  • FastAPI version : 0.111.0
  • FastAPI Users version : 13.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions