Skip to main content

No project description provided

Project description

typer-builder

A framework for simplifying the development of Typer based CLIs supporting modern type hints and hierarchical dependency injection.

Table of Contents

Introduction

The build_app_from_module() inspect a hierarchy of Python modules to build a Typer command and group structure. Packages are treated as command groups and may define a callback() member. Modules are treated commands and must define a main() member. Modules and packages prefixed with an underscore ( _ ) are ignored. Help text is extracted from the main() docstring or the module docstring.

In addition, we provide support for new-style type hints (PEP 585 - Type Hinting Generics in Standard Collections and PEP 604 - Union Operators) in older versions of Python as well as adapt it for Typer (e.g. list[str] and str | None), as well as a method of injecting dependencies to functions that are not sourced from the command-line.

Example

$ tree src/mypackage/
src/mypackage/
├── __init__.py
├── __main__.py
└── commands
    ├── __init__.py
    ├── hello.py
    └── bye.py
# src/mypackage/commands/hello.py
def main(name: str) -> None:
    print("Hello,", name)
# src/mypackage/__main__.py
from typer_builder import build_app_from_module

if __name__ == "__main__":
    app = build_app_from_module("mypackage.commands")
    app()

Documentation

New-style type hint support

Through typeapi, we can convert new-tyle type hints such as str | None or list[int] to their corresponding representation using typing before the function signature is parsed by Typer.

# src/mypackage/commands/create_user.py
from ___future__ import annotations

def main(name: str | None = None, groups: list[str] | None = None) -> None:
    # ...

typeapi also allows us to convert list[str] to List[str] and dict[str, int] to Dict[str, int] for Python versions prior to 3.9. This is necessary because Typer does not support the new-style type hints.

Dependency injection

The typer_builder.Dependencies object is used to map types to concrete values or functions that provide them. Functions wrapped with Dependencies.bind() will have their arguments resolved by the injector based on type annotations. Every build_app_from_module() call creates a new Dependencies instance. Dependencies can be injected from the outside by passing a Dependencies instance to build_app_from_module() or by providing additional dependencies via a callback() function on the command group.

Note that the Dependencies does not understand generics with different type parameters. For example, it makes no distinction between MyGeneric[int] and MyGeneric[str]. This is a limitation of the current implementation as well as the Python type system.

The most common use case for dependency injection is to inject configuration managers or clients into subcommands. For an example, you should check out the examples/dependency-injection directory.

License

This project is licensed 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_builder-0.0.10.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

typer_builder-0.0.10-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file typer_builder-0.0.10.tar.gz.

File metadata

  • Download URL: typer_builder-0.0.10.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.30.0 requests-toolbelt/1.0.0 urllib3/2.0.2 tqdm/4.65.0 importlib-metadata/6.6.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.2

File hashes

Hashes for typer_builder-0.0.10.tar.gz
Algorithm Hash digest
SHA256 b5f63df0fe2e630b1ba16d1c6571b679065fb2e6570a951e08f40048e392451e
MD5 83c1f4a39a0ff49388fd9be6b81c73ba
BLAKE2b-256 b03316f0fdcf263266393cfc1e13dc8b972ebf75795fe319151f970a6a5421d2

See more details on using hashes here.

File details

Details for the file typer_builder-0.0.10-py3-none-any.whl.

File metadata

  • Download URL: typer_builder-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.30.0 requests-toolbelt/1.0.0 urllib3/2.0.2 tqdm/4.65.0 importlib-metadata/6.6.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.2

File hashes

Hashes for typer_builder-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 cef407474b923b021af297a4ea8077b0530f7accd77da8ae1f1f14eab5f11843
MD5 5904b5e7a6c9b3d3d8c9452227d59893
BLAKE2b-256 e46b9e4cede4cd03d2874de378111a2a5e9463b501a52b118ba332d1db3eccb7

See more details on using hashes here.

Supported by

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