Skip to main content

A Flexible Service Locator

Project description

svcs logo showing a hexagon-shaped radar

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:

Watch the video

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 like Protocols or abstract base classes for registered services, removing an important typing caveat. This change introduces a dependency on typing-extensions for 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=TypeForm argument.

  • New svcs.autowire() and svcs.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 to False, 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.


Full Changelog →

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

svcs-26.1.0.tar.gz (906.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

svcs-26.1.0-py3-none-any.whl (23.6 kB view details)

Uploaded Python 3

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

Hashes for svcs-26.1.0.tar.gz
Algorithm Hash digest
SHA256 71550e3b228d529448136d78013b2d65528eec4632b8779f8f89c1d8585eed99
MD5 b6763efc4d5b3479fbbb3ec22c31fbbe
BLAKE2b-256 50ea10957367bf49b401a8100ff434f453a340cc30c9c2fc3b2367e6c282f03d

See more details on using hashes here.

Provenance

The following attestation bundles were made for svcs-26.1.0.tar.gz:

Publisher: pypi-package.yml on hynek/svcs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for svcs-26.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 87428c6371af6ae5d0936193613ba4e463118f36b43e15a708f417a8e0b85520
MD5 de3c897919914d60f941780c2e3fbdb7
BLAKE2b-256 5e8c5f9441100851bea8f4735cf54a2980d2ad9300a5933c1d5e416448fd789a

See more details on using hashes here.

Provenance

The following attestation bundles were made for svcs-26.1.0-py3-none-any.whl:

Publisher: pypi-package.yml on hynek/svcs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page