Skip to main content

Dependency injection for typer

Project description

typer-injector

PyPI - Version PyPI - Python Version


typer-injector adds FastAPI-style dependency injection to Typer.

Table of Contents

Installation

pip install typer-injector

Usage

Example:

import socket
from typing import Annotated, TypeAlias

import typer
from typer_injector import Depends, InjectingTyper


app = InjectingTyper()


def address_dependency(
    host: Annotated[str, typer.Option()],
    port: Annotated[int, typer.Option()],
) -> tuple[str, int]:
    return host, port


Address: TypeAlias = Annotated[tuple[str, int], Depends(address_dependency)]


@app.command()
def send_message(message: str, address: Address) -> None:
    """Send a message to the specified address."""
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.connect(address)
        sock.send(message.encode())


@app.command()
def receive_message(address: Address) -> None:
    """Listen for a message at the specified address."""
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.bind(address)
        sock.listen(1)
        conn, _ = sock.accept()
        with conn:
            data = conn.recv(1024)
            print(data.decode())


if __name__ == '__main__':
    app()

License

typer-injector is distributed under the terms of the MIT license.

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

typer_injector-0.2.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

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

typer_injector-0.2.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file typer_injector-0.2.0.tar.gz.

File metadata

  • Download URL: typer_injector-0.2.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for typer_injector-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9101774e4a7402143e33eb60cabf21f6f95fa0f9c2c5eb99d9da720b40b00aaf
MD5 bed24a704b2858c881b04ce1136f3a4f
BLAKE2b-256 918e267d1666ed6dd1d07db05a7e331586d4a3924975d89d86c9d52a14de169a

See more details on using hashes here.

Provenance

The following attestation bundles were made for typer_injector-0.2.0.tar.gz:

Publisher: release.yml on BenjyWiener/typer-injector

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

File details

Details for the file typer_injector-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: typer_injector-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for typer_injector-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1469b8a942f9fc68f60c076c49da4a8f903887e2c9108c11c8316b9eb41be66e
MD5 42928227c7a8fec637bf1698d187bd13
BLAKE2b-256 2a38c1926b8eff4f23ff0b0406670a7d9980c23b295131d0d90dcca7267615ae

See more details on using hashes here.

Provenance

The following attestation bundles were made for typer_injector-0.2.0-py3-none-any.whl:

Publisher: release.yml on BenjyWiener/typer-injector

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