Feature / Fix: Allow Settings to be inherited and extended (fixes #644) #960
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Before this would not work
The fastapi-users library actually claims in their documentation that this is how you should extend their
BeanieBaseUser
model to change the collection name: https://fastapi-users.github.io/fastapi-users/latest/configuration/databases/beanie/Indeed the functionality would be very useful e.g. to provide shared
bscon_encoders
via a kind of base document model that all your documents inherit from.This fixes #644 which was closed without resolution. In the issue @roman-right mentions that this is not fixable because it would have to be fixed via
metaclass
which is already occupied by pydantic. Evidently, by this PR, that is not the case :)The way the Settings attribute collection was implemented before (via
vars(class)
) would throw away the inheritedSettings
if aSettings
class was present in the inheriting class.I changed the
Settings
attribute collection to usedir
and included some filtering of dunder methods.✅ Tests passing
✅ Added test to cover new functionality
I don't know if this fix / feature needs a change in the Settings documentation. Arguably it now behaves the way you'd expect this to behave, as it mirrors the way these attributes access and inherit via normal Python.