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

[Question] How to provide custom error messages when a type is not available? #167

Open
JosXa opened this issue Dec 7, 2020 · 4 comments

Comments

@JosXa
Copy link

JosXa commented Dec 7, 2020

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.

@jstasiak
Copy link
Collaborator

jstasiak commented Dec 7, 2020

After reading the first sentence I was gonna say binder.bind(FooCredentials, to=FooCredentials(loging='hello', password='world')) but I can't understand the rest of your question at all. Can you provide some pseudocode of what you're trying to achieve?

@JosXa
Copy link
Author

JosXa commented Dec 7, 2020

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?

@JosXa
Copy link
Author

JosXa commented Dec 7, 2020

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)

@JosXa JosXa changed the title [Questions] How to provide custom error messages when a type is not available? [Question] How to provide custom error messages when a type is not available? Dec 7, 2020
@jstasiak
Copy link
Collaborator

jstasiak commented Dec 8, 2020

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 TickTickCredentials with non-empty username and password.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants