You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found some problem in attr getter. It lies in the fact that it does not know how to work with nested attributes. Just look at some tests below and you'll understand what we're talking about.
fromdataclassesimportdataclass, fieldimportpytestfromthat_dependsimportproviders@dataclassclassNested2:
some_const=144@dataclassclassNested1:
nested2_attr: Nested2=field(default_factory=Nested2)
@dataclassclassSettings:
some_str_value: str='some_string_value'some_int_value: int=3453621nested1_attr: Nested1=field(default_factory=Nested1)
@pytest.fixturedefsome_settings_provider() ->providers.Singleton:
provider=providers.Singleton(Settings)
returnprovider# WORKSdeftest_attr_getter_with_zero_attribute_depth(some_settings_provider):
attr_getter=some_settings_provider.some_str_valueassertattr_getter.sync_resolve() ==Settings().some_str_value# DOESNT WORKS BECAUSE Settings HAS NESTED OBJECTS AND ATTRIBUTESdeftest_attr_getter_with_more_than_zero_attribute_depth(some_settings_provider):
attr_getter=some_settings_provider.nested1_attr.nested2_attr.some_constassertattr_getter.sync_resolve() ==Nested2().some_const
I think I can fix this if you want these cases to work
@nightblure will it be hard? I'm not sure, that this feature will be popular, but I don't mind if you want to do it
I don’t know about popularity, but I think it’s necessary for reasons of convenience
Let's imagine that your service actually has a lot of environment variables and other configuration data. This is usually stored in the form of dataclasses or identity models obtained as a result of parsing environment variables and various configuration files (for example, yaml configs, etc...). And in order not to store a bunch of fields in one class, you can often find division into subclasses for reasons of convenience and logical grouping of variables. Based on this, in some cases this functionality would be necessary. Otherwise, we would have to make a bunch of small providers instead of one or another kind of refactoring
Activity
lesnik512 commentedon Jul 18, 2024
@nightblure thank you! interesting case. Will think about it
lesnik512 commentedon Jul 18, 2024
@nightblure will it be hard? I'm not sure, that this feature will be popular, but I don't mind if you want to do it
nightblure commentedon Jul 18, 2024
I don’t know about popularity, but I think it’s necessary for reasons of convenience
Let's imagine that your service actually has a lot of environment variables and other configuration data. This is usually stored in the form of dataclasses or identity models obtained as a result of parsing environment variables and various configuration files (for example, yaml configs, etc...). And in order not to store a bunch of fields in one class, you can often find division into subclasses for reasons of convenience and logical grouping of variables. Based on this, in some cases this functionality would be necessary. Otherwise, we would have to make a bunch of small providers instead of one or another kind of refactoring
nightblure commentedon Jul 18, 2024
btw dependency injector can do this: https://python-dependency-injector.ets-labs.org/providers/provided_instance.html
lesnik512 commentedon Jul 18, 2024
@nightblure valid points