Inject[Greeter] vs. inject.me() #59
Replies: 2 comments 1 reply
-
What kind of error did you have with flake8? I never had any with The main difference is the one you've seen in #60. It just works better with static type checking. It also has better syntax to retrieve implementations for a given interface currently: def f(a: A = inject.me(qualified_by=X)):
...
def g(a: Annotated[A, Get(ImplementationsOf(A).single(qualified_by=X))]):
... The V2 I'm working on removes the latter difference though, objects used for annotation and default are the same and treated in the same way. Moreover, they're open for extension. Here's a bit simplified extract: # implemented by inject.get, lazy functions, constants
class Dependency(Generic[Out]):
def __antidote_dependency__(self) -> object:
return self
# implemented by inject.me() and used in Inject annotation
class ParameterDependency:
def __antidote_parameter_dependency__(
self, *, name: str, type_hint: object, type_hint_with_extras: object
) -> Dependency[Any]:
raise NotImplementedError() To be honest, the best way to use antidote is definitely using default parameters as it's the only one providing proper static type checking. Now that said, previous injection styles have been there before and the purpose of the core part is to be flexible. So I intend to keep them as I don't see a good reason to remove them. I probably should remove them from the README and provide an explanation of their shortcomings though. |
Beta Was this translation helpful? Give feedback.
-
Agreed, changing the docs to show these as kind of legacy is a good answer. |
Beta Was this translation helpful? Give feedback.
-
I'm working again on examples. I'm covering different spelling for injector.
In the injection example you cover the different spellings for injection. I'm curious the difference between
greeter: Greeting = inject.me()
andgreeting: Inject[Greeter]
.I know the latter can't be used with other markers such as
Optional
. Are there any other differences?Inject[Greeter]
is shorter and might be more normal to look at. For example, flake8 out-of-the-box complains aboutinject.me()
.I'm considering changing my examples to
Inject[Greeter]
but that differs from official docs.Beta Was this translation helpful? Give feedback.
All reactions