-
Notifications
You must be signed in to change notification settings - Fork 82
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
[question] string-based annotations? equivalent to guice Names.named() and @Named #133
Comments
No, you're right, something like this is not supported at the moment. The closest you can get right now is with type aliases I think: Mine = NewType('Mine', str)
SomethingElse = NewType('SomethingElse', str)
@inject
@dataclass
class MyClass:
myval: Mine
myotherval: SomethingElse
def conf(binder: Binder):
binder.bind(Mine, to="test1")
binder.bind(SomethingElse, to="test2")
# ... Granted, the types/aliases/names still need to be declared statically up front. If you want to be completely dynamic in config reading I think your other best choice is to have provider methods that receive configuration and construct your classes:
In both of those cases you don't lose type safety if you use any linters that test it, so there's that. :) Injector has initial support for PEP 593 -- Flexible function and variable annotations so something like Guice's |
See #174 for a potential (not supported at the moment) way to use |
@jstasiak, just a nit-pick. technically The following are roughly equivalent (including how it sets the parent class pointer)
A type alias would be defined as |
I know it's generally better to use static annotations (eg #69), but I found
@Named
annotations very useful in guice for configuration injection. I'm able to reproduce this feature usingtype
but did I miss an existing implementation in the package? didn't want to reinvent the wheel, even if its only two lines of code.From here I'd write a module that parses a config file and does
binder.bind(named(key),to=value)
to make it easy for classes to get values from the config to an injected class.The text was updated successfully, but these errors were encountered: