Skip to main content

An IOC Container for Python 3.10+

Project description

Punq

Release Build status Tests Commit activity License

An unintrusive library for dependency injection in modern Python. Inspired by Funq, Punq is a dependency injection library you can understand.

  • No global state
  • No decorators
  • No weird syntax applied to arguments
  • Supports type checking with strict modes of mypy, pyright, and pyrefly
  • Small and simple code base with 100% test coverage and developer-friendly comments.

Installation

Punq is available on the cheese shop

pip install punq

Documentation is available on GitHub pages.

Quick Start

Punq avoids global state, so you must explicitly create a container in the entrypoint of your application:

import punq

container = punq.Container()

Once you have a container, you can register your application's dependencies. In the simplest case, we can register any arbitrary object with some key:

container.register("connection_string", instance="postgresql://...")

We can then request that object back from the container:

conn_str = container.resolve("connection_string")

Usually, though, we want to register some object that implements a useful service.:

class ConfigReader:
    def get_config(self) -> dict[str, str]:
        pass

class EnvironmentConfigReader(ConfigReader):
    def get_config(self) -> dict[str, str]:
        return {
            "logging": {
                "level": os.env.get("LOGGING_LEVEL", "debug"),
            },
            "greeting": os.env.get("GREETING", "Hello world"),
        }

container.register(ConfigReader, EnvironmentConfigReader)

Now we can resolve the ConfigReader service, and receive a concrete implementation:

config = container.resolve(ConfigReader).get_config()

If our application's dependencies have their own dependencies, Punq will inject those, too:

class Greeter:
    def greet(self) -> None:
        pass


class ConsoleGreeter(Greeter):
    def __init__(self, config_reader: ConfigReader) -> None:
        self.config = config_reader.get_config()

    def greet(self) -> None:
        print(self.config['greeting'])


container.register(Greeter, ConsoleGreeter)
container.resolve(Greeter).greet()

If you just want to resolve an object without having any base class, that's okay:

class Greeter:
    def __init__(self, config_reader: ConfigReader) -> None:
        self.config = config_reader.get_config()

    def greet(self) -> None:
        print(self.config['greeting'])

container.register(Greeter)
container.resolve(Greeter).greet()

And if you need to have a singleton object for some reason, we can tell punq to register a specific instance of an object:

class FileWritingGreeter:
    def __init__(self, path: str, greeting: str) -> None:
        self.path = path
        self.message = greeting
        self.file = open(self.path, 'w')

    def greet(self) -> None:
        self.file.write(self.message)


one_true_greeter = FileWritingGreeter("/tmp/greetings", "Hello world")
container.register(Greeter, instance=one_true_greeter)

You might not know all of your arguments at registration time, but you can provide them later:

container.register(Greeter, FileWritingGreeter)
greeter = container.resolve(Greeter, path="/tmp/foo", greeting="Hello world")

Conversely, you might want to provide arguments at registration time, without adding them to the container:

container.register(Greeter, FileWritingGreeter, path="/tmp/foo", greeting="Hello world")

Fuller documentation is available on GitHub pages

GitHub workflows, nox configuration, and linting gratefully stolen from CookieCutter UV

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

punq-0.8.0.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

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

punq-0.8.0-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file punq-0.8.0.tar.gz.

File metadata

  • Download URL: punq-0.8.0.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for punq-0.8.0.tar.gz
Algorithm Hash digest
SHA256 efc5308e5b027a466449389ea5b1e9fcfd5eb65495bcb766e3f725a9d6db9998
MD5 12afead64a1f72f8b4c3395bc521cdba
BLAKE2b-256 0331be5732aaa2b7cf57225ff25bbd88e4b304890a1aa24f675658f026e12a43

See more details on using hashes here.

File details

Details for the file punq-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: punq-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 19.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for punq-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1f34dee67f26bd5ea7f4dfeee11b22aa1a19c634421262a07f2b51a9dc8e2c41
MD5 ca11d89dc4f8133d710d786b10bf9a03
BLAKE2b-256 4f92b78a5a31180f5af151ea0b28ee6c0c7964fb79cfea394fbba6f028423c11

See more details on using hashes here.

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