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

[RFC] Add as_dict alias to to_dict for DynaBox for consistency between LazySettings and DynaBox objs #1073

Open
mitches-got-glitches opened this issue Mar 5, 2024 · 1 comment
Labels
Not a Bug Not a Problem, expected behavior RFC

Comments

@mitches-got-glitches
Copy link
Contributor

Is your feature request related to a problem? Please describe.
It's possible to convert a LazySettings object to a Python dictionary with LazySettings.as_dict(), and there is an alias to to_dict for backwards compatibility. The DynaBox class inherits from the Box class which comes from a vendor. There is the method Box.to_dict()
but not .as_dict(), so there is an inconsistency for the user if the user accesses a sub-box of settings and then tries to convert to a dictionary - see context.

Describe the solution you'd like
Add an alias to DynaBox:

class DynaBox(Box):
    ...
    as_dict = to_dict

The opposite of what is done here:

to_dict = as_dict # backwards compatibility

Describe alternatives you've considered

No Alternative Drawback
1 Using a combination of as_dict() and to_dict() confusing for the user
2 Using to_dict() for both LazySettings and DynaBox seems like this is the older method and the current guidance is to use as_dict()
3 Doing only LazySettings.as_dict() and then using keys to get to dict level we want takes away the flexibility of using dot notation to get to sub-levels of config

Additional context

nested_settings.toml

[hyperparameters]
max_depth = 10
n_estimators = 20

Examples

from dynaconf import Dynaconf
settings = Dynaconf(settings_files=['nested_settings.toml'])

print(settings.as_dict())
>>> {'HYPERPARAMETERS': {'max_depth': 10, 'n_estimators': 20}}

print(settings.hyperparameters.as_dict())
>>> dynaconf.vendor.box.exceptions.BoxKeyError: "'DynaBox' object has no attribute 'as_dict'"

print(settings.hyperparameters.to_dict())
>>> {'max_depth': 10, 'n_estimators': 20}
@mitches-got-glitches mitches-got-glitches added Not a Bug Not a Problem, expected behavior RFC labels Mar 5, 2024
@pedro-psb
Copy link
Member

This looks reasonable to me.
Cant see why not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Bug Not a Problem, expected behavior RFC
Projects
None yet
Development

No branches or pull requests

2 participants