Closed takanuva15 closed 5 months ago
@takanuva15 We potentially could do that, but not everyone in python world is adhering to this convention. Additionally, when inspecting a class for their related class, how would you know which one is really meant to be used for aliasing without any kind of hint?
The other problem with this approach might be a namespace conflict. Imagine you have couple implementations of ISomething, which one noe should be the proper target?
I prefer to keep it explicit, I think this way it is simpler.
Hi, thanks for making this awesome library (I found an article on Google recommending this library).
My question was: The documentation mentions for service aliasing that:
I always explicitly define relationships between my "interfaces" and their implementations through base classes so that the IDE does static type-checking to verify that the subclass has implemented everything from the superclass. (It's also clear to others that the class is intended to implement all the methods of the interface). Simple example:
The official Python documentation also shows a similar example with that recommendation:
Since inheritance already implies by definition that
UserService
is an instance ofIUserService
, having to manually define thatIUserService
is an alias ofUserService
within the@inject
annotation is redundant. (ie it's already clear to a Python developer thatUserService
is an instance ofIUserService
, so when I want to get all the implementations ofIUserService
, I should expect to seeUserService
in that list by definition).(This is also the default behavior in other major di frameworks like SpringBoot in Java, which saves a lot of time).
Thus, I wanted to request that can we automatically register base class(es) as aliases when using
@inject
on a class?(Searching online briefly, it seems we can use
inspect.getmro(B)
orcls.__bases__
to get the base classes to auto-register as aliases at runtime)