-
Notifications
You must be signed in to change notification settings - Fork 19
Wireup vNext API Modernization #100
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
Open
maldoinc
wants to merge
31
commits into
master
Choose a base branch
from
wireup-next
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* Initial support for protocols via binds * binds -> as_type; Replace registration instead of aliasing * Add as_type tests * Fix import * Handle optional factories * Update tests * Rm @abstract from docs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces a modernized Wireup API focused on clearer naming, explicit abstraction binding, and first-class support for protocols.
This will be a minor backwards-compatible release with the previous API continuing to work until v3, but being deprecated.
1.
@service->@injectableThe
@servicedecorator will be renamed to@injectable. The name service carries architectural baggage and is misleading for many injected values. Even in Wireup's own docs the usage of@serviceis confusing.AuthenticatedUsername = NewType("AuthenticatedUsername", str) -@service(lifetime="scoped") +@injectable(lifetime="scoped") def authenticated_username_factory(auth: SomeAuthService) -> AuthenticatedUsername: return AuthenticatedUsername(...)Many injected values such as
AuthenticatedUsernamefrom the example, are not really "services".The name injectable is strictly about capability: A thing that can be injected. It is agnostic to the produced object's role in the system architecture making it overall less confusing.
Note: This is a pure rename and the behavior is unchanged.
2.
Inject(param=...)->Inject(config=)Same as "service", injecting "parameters" is somewhat ambiguous for users. The name itself is also very overloaded especially in the context of a web application: You have query parameters, path parameters, function parameters in the signature and now Wireup parameters.
The feature remains to enable co-located definitions, but it is renamed to
configto make it explicit that values come from Wireup’s configuration rather than some runtime or function parameters.Similarly
container.paramsis deprecated in favor ofcontainer.config.Note: Same as above, this is a pure rename and the behavior is unchanged.
3. Better support for abstractions
Currently Wireup requires tagging classes to be used as "interfaces" with
@abstract. The linking between the two is indirect, wireup scans bases to see if any of them was marked with@abstractto do the wiring. This also has the side-effect that the container knows both about the implementation and the abstraction.This is now deprecated in favor of explicit binding via
as_type.@abstract,as_typereplaces the registration rather than creating an alias. The concrete type will no longer be visible to the container unless explicitly exposed (see example below).FooImpl | None,as_type=Fooautomatically registers the key asFoo | Noneto ensure runtime safety.For factories, you can control the registration by setting the return type or using
as_type. You may choose to keep the concrete type visible (e.g. for tests) while exposing only the abstraction to the container.With functions since you can control the return type (unlike
@injectableon a class), instead of usingas_type=Cache, you can have the function returnCachefor the same effect.If you need to keep both the implementation and the abstraction visible to the container you can explicitly expose both by writing a small adapter.
Both
FooImplandFooProtocolare visible to the container and will reuse the same instance.Improved container creation signature
Given the "service" rename, the signatures must be updated as well. The new api exposes a new
injectablesparameter where you can place either injectables themselves or modules for wireup to scan for injectables rather than scattering them in two parameters (servicesandservice_modules).4. Improved handling of optional values
Wireup supports optional values as first-class citizens, with one caveat:
This dependency can be injected into function signatures as
Redis | None.However, when using the container as a service locator, users previously had to write
container.get(Redis)This meant the type checker was unaware that the value could be absent.This is now fixed:
container.get(Redis | None)is supported and correctly typed.Calling
container.get(T)for a dependency registered asT | Nonewill continue to work, but will emit a deprecation warning.