-
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
Unable to bind Annotated to instances of origin #174
Comments
Ah yeah, I haven't considered handling It's probably worth exploring what possible uses of |
One can, however, use import pathlib
import injector
ConfigFile = injector.Annotated[pathlib.WindowsPath, 'config']
cfg_file = pathlib.Path.home() / 'settings.cfg'
class AppModule(injector.Module):
@singleton
@prodiver
def provides_config_file(self) -> ConfigFile:
return cfg_file
class App:
@injector.inject
def __init__(self, cfg: ConfigFile):
self.config = cfg
app = injector.Injector(AppModule).get(App)
print(app.config)
# C:\Users\<me>\settings.cfg However, for simply annotating multiple instances of the same type (e.g. a file interface [app config, user config, network config, etc.]), using the |
Oh, really? That's interesting, those should have the same effect. Also I have a feeling that due to Annotated-stripping in the Injector internals this won't distinguish between % cat asd.py
import pathlib
import injector
ConfigFile = injector.Annotated[pathlib.Path, 'config']
OtherConfigFile = injector.Annotated[pathlib.Path, 'other_config']
cfg_file = pathlib.Path.home() / 'settings.cfg'
other_cfg_file = pathlib.Path.home() / 'settings.cfg' / 'other'
class AppModule(injector.Module):
@injector.singleton
@injector.provider
def provides_config_file(self) -> ConfigFile:
return cfg_file
@injector.singleton
@injector.provider
def provides_other_config_file(self) -> OtherConfigFile:
return other_cfg_file
class App:
@injector.inject
def __init__(self, cfg: ConfigFile, other_cfg: OtherConfigFile) -> None:
self.cfg = cfg
self.other_cfg = other_cfg
app = injector.Injector(AppModule).get(App)
print(app.cfg)
print(app.other_cfg)
With this in mind I'm tempted to add a big warning to the documentation to inform people about this for the time being. Bonus: (edit: originally both provided methods returned |
Correct. Using import typing
import pathlib
import injector
UserFile = typing.NewType('UserFile', pathlib.WindowsPath)
ConfigFile = typing.NewType('ConfigFile', pathlib.WindowsPath)
usr_file = pathlib.Path.home() / 'user.dat'
cfg_file = pathlib.Path.home() / 'settings.cfg'
class AppModule(injector.Module):
def configure(self, binder):
binder.bind(UserFile, to=usr_file)
binder.bind(ConfigFile, to=cfg_file)
class App:
@injector.inject
def __init__(self, cfg: ConfigFile, usr: UserFile):
self.user = usr
self.config = cfg
app = injector.Injector(AppModule).get(App)
print(app.config)
print(app.user)
# C:\Users\<me>\settings.cfg
# C:\Users\<me>\user.dat I believe such integration is achievable still in the |
Yes, absolutely. I'd like to consider other options before allowing arbitrary, opaque |
FYI, calling out a technically nit that I mentioned in #133 about For instance, in the example above (I know it was just meant as a motivating example) the |
Originally stemming from interest in #133 , I too thought to replicate Guice's
@Named
convention. In doing so, I originally used thetyping.NewType
(prior to having even seen the aforementioned issue) and succeeded. However, instantiating a type for each simple alias, I thoughttyping.Annotated
(albeit, usingtyping_extensions.Annotated
) would be more idiomatic.Given a simple example:
...throws...
My belief is that
provider_for
does not adequately check to see if the interface is an_AnnotatedAlias
or not. Theorigin
variable resolves to the base interface correctly, in this casepathlib.WindowsPath
, but sincebase_type
istyping_extenstions.Annotated
, not check is made given that case.The text was updated successfully, but these errors were encountered: