Ping dependencies
Add separate decorator to provide ping functionality. It has no scope during declaration and can be used to run specific code declated in providers:
Declaration:
class Provider:
@ping
def foo(self) -> None:
print("PING")
@ping
def bar(self, session: Session) -> None:
session.execute(select(1))
print("PING SESSION")
Usage:
container.ping()
That call will find all pings declared with current and parent scopes and request them
Discuss: should we unify this technic?
Discuss: auto calculation of scope based on dependencies vs explicit scope
Discuss: auto calculation of scope based on dependencies vs explicit scope
Good discussion point, there is a boundary between code readability and developer experience. Auto calculation can be good convenient option (NestJS DI System has it), but it can be difficult to read dependency source from a lot of nested providers (modules)
The description proposes API where you explicitly declare each ping. That's the most compatible way, certainly.
It could be nice if there was some introspection in play, too. Like if a dependency defines ping (or alike) method with certain arguments/annotations then it would be automatically registered/called.
class SomeClass:
def ping(self):
...
Then you wouldn't have to explicitly define the ping each time.
Or @provide(ping=True) or @provide(ping="custom_method_name") though that would be bad for type safety.
Though, I don't think many dependencies need to define pings. Most likely just IO (database, cache, connections to other HTTP servers, etc). And in those cases you normally need to manually define/customize how the ping is done, anyway.
So, the proposed API would most likely suffice.