-
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] How to provide custom error messages when a type is not available? #167
Comments
After reading the first sentence I was gonna say |
class TickTickModule(Module):
@provider
def credentials(self, binder: Binder) -> TickTickCredentials:
has_been_overridden_by_user: bool = bLaCkBoX(binder, TickTickCredentials)
if not has_been_overridden_by_user:
raise UnsatisfiedRequirement(
"In order to work with this API, you should provide an instance of " +
f"{TickTickCredentials.__name__} to injector!"
)
return binder.get_binding(TickTickCredentials) # ok, they did their job Here's a somewhat condensed version of my eventual solution. Bear in mind that above is more of a toy example, as I did after all go with the module initializer approach - but I've had some use cases where I wanted to check "ok has someone else been interfering with the library-provided dependencies here or are we clean?" Do you you hear how I feel? |
Just wanted to note that WOW does it loosen up your architecture and drop you into that goldilock pit of success when you don't have to worry about all the wiring anymore.. Top-down containers are nice! (when compared to the global container approach of other libs) |
Yes, sorry, it's clear now. There's no good way to get this now. I can't think of anything but an ugly hack like import injector
class TickTickCredentials:
def __init__(self, username: str = '', password: str = '') -> None:
if not username and not password:
raise RuntimeError('In order to use this library you need to bind this')
injector.Injector().get(TickTickCredentials) which gives you a custom exception if nobody constructs |
I would like to expose a
FooCredentials
dataclass to users as part of a library.Ignoring the fact that this could probably be done nicely by asking them to pass it to me via the
Module
initializer, how could I check whether a specific binding has been overriden by the user at time of first access (not at time of binding as user code might not have run yet)? The reason behind it is that I would like to show improved error messages when they're "using the library wrong".I'm assuming some combination of
CallableProvider
and try/except on some binder or injector method, but I'm not sure which one is the right place.The text was updated successfully, but these errors were encountered: