A Flexible Service Locator
Project description
A Flexible Service Locator for Python.
svcs (pronounced services) is a dependency container for Python. It gives you a central place to register factories for types/interfaces and then imperatively acquire instances of those types with automatic cleanup and health checks.
It's suitable for implementing Inversion of Control using either dependency injection or service location while not requiring global state, decorators, or mangling of function signatures.
Benefits:
- Eliminates tons of repetitive boilerplate code,
- unifies acquisition and cleanups of services,
- provides full static type safety for them,
- simplifies testing through loose coupling,
- improves live introspection and monitoring with health checks.
The goal is to minimize the code for acquiring pluggable services to:
from svcs.your_framework import svcs_from
def view(request):
db, api, cache = svcs_from(request).get(Database, WebAPIClient, Cache)
... or less!
To a type checker, db has the type Database, api has the type WebAPIClient, and cache has the type Cache.
db, api, and cache will be automatically cleaned up when the request ends -- it's context managers all the way down.
svcs comes with seamless integration for AIOHTTP, FastAPI, Flask, Pyramid, and Starlette.
While svcs also has first-class support for static typing, it is strictly optional and will always remain so. svcs also doesn't check your types at runtime. It only forwards the type you have asked for to the type checker. If you don't use a type checker, that information is ignored without any runtime overhead.
Read on in Why? or watch this short video if you're intrigued:
Project links
Release Information
Deprecated
svcs.get_abstract()and all its*_abstract()siblings. Thanks to PEP 747 they are no longer necessary. No deprecation warnings or plans to remove them for now.
Added
-
Python 3.14 and 3.15 support.
-
PEP 747 support, aka
typing.TypeForm. This means that it's now possible use abstract types likeProtocols or abstract base classes for registered services, removing an important typing caveat. This change introduces a dependency ontyping-extensionsfor Python 3.14 and earlier.Note: On Mypy versions older than 2.2, users that want to take advantage of this must pass the
--enable-incomplete-feature=TypeFormargument. -
New
svcs.autowire()andsvcs.aautowire()that can be used to automatically resolve dependencies based on type annotations. #167 -
New suppress_context_exit argument to
svcs.register_(factory|value)(). If set toFalse, errors in the container context will be passed into the factory cleanup context manager and allow you to act on them there.You can't stop the exception from bubbling out of the container context, though. #129
Removed
-
Debug logs don't contain stack information anymore since it leads to excessive output. If you miss them, please open an issue and we make it an option on
svcs.Registry. #135 #139 -
Python 3.9 support. #152
Changed
Container.get_pings()now includes registry-local services. Locally defined services overwrite global ones if they are registered for the same type. This includes that a local service without a ping disables a global service's ping. #83
Fixed
-
Factories now can return
MagicMocks without crashing with a TypeError. #137 -
AIOHTTP: The container is now stored using
aiohttp.web.RequestKeys on the application. This is an implementation detail and shouldn't matter, but it fixes a warning on AIOHTTP 3.14 and later.
Credits
svcs is written by Hynek Schlawack and distributed under the terms of the MIT license.
The development is kindly supported by my employer Variomedia AG and all my fabulous GitHub Sponsors.
The Bestagon radar logo is made by Lynn Root, based on a Font Awesome icon. svcs has started out as a wrapper around wired by Michael Merickel and has been heavily influenced by it.
svcs for Enterprise
Available as part of the Tidelift Subscription.
The maintainers of svcs and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file svcs-26.1.0.tar.gz.
File metadata
- Download URL: svcs-26.1.0.tar.gz
- Upload date:
- Size: 906.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71550e3b228d529448136d78013b2d65528eec4632b8779f8f89c1d8585eed99
|
|
| MD5 |
b6763efc4d5b3479fbbb3ec22c31fbbe
|
|
| BLAKE2b-256 |
50ea10957367bf49b401a8100ff434f453a340cc30c9c2fc3b2367e6c282f03d
|
Provenance
The following attestation bundles were made for svcs-26.1.0.tar.gz:
Publisher:
pypi-package.yml on hynek/svcs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
svcs-26.1.0.tar.gz -
Subject digest:
71550e3b228d529448136d78013b2d65528eec4632b8779f8f89c1d8585eed99 - Sigstore transparency entry: 2162477902
- Sigstore integration time:
-
Permalink:
hynek/svcs@32b0c2f5f4efb9fcc6c95bd39d88beca00da2309 -
Branch / Tag:
refs/tags/26.1.0 - Owner: https://github.com/hynek
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-package.yml@32b0c2f5f4efb9fcc6c95bd39d88beca00da2309 -
Trigger Event:
release
-
Statement type:
File details
Details for the file svcs-26.1.0-py3-none-any.whl.
File metadata
- Download URL: svcs-26.1.0-py3-none-any.whl
- Upload date:
- Size: 23.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
87428c6371af6ae5d0936193613ba4e463118f36b43e15a708f417a8e0b85520
|
|
| MD5 |
de3c897919914d60f941780c2e3fbdb7
|
|
| BLAKE2b-256 |
5e8c5f9441100851bea8f4735cf54a2980d2ad9300a5933c1d5e416448fd789a
|
Provenance
The following attestation bundles were made for svcs-26.1.0-py3-none-any.whl:
Publisher:
pypi-package.yml on hynek/svcs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
svcs-26.1.0-py3-none-any.whl -
Subject digest:
87428c6371af6ae5d0936193613ba4e463118f36b43e15a708f417a8e0b85520 - Sigstore transparency entry: 2162478473
- Sigstore integration time:
-
Permalink:
hynek/svcs@32b0c2f5f4efb9fcc6c95bd39d88beca00da2309 -
Branch / Tag:
refs/tags/26.1.0 - Owner: https://github.com/hynek
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-package.yml@32b0c2f5f4efb9fcc6c95bd39d88beca00da2309 -
Trigger Event:
release
-
Statement type: