Skip to main content

No project description provided

Project description

terracomp-typer

This package allows you to easily build a Typer CLI application from a Python module hierarchy.

Quickstart

$ tree src/mypackage/commands/
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 terracomp_typer import build_app_from_module

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

Features

  • Packages are treated as command groups and may define a def callback(): ... (see Typer.add_callback()).
  • Modules are treated as commands and must define a def main(): ... (see Typer.command()).
  • Underscores in package or module names are normalized to hyphens (e.g my_command -> my-command).
  • Command(-group) help text is extracted from the package or module docstring, or from the main() docstring.
  • [WIP] Improved and dynamic dependency injection.
  • Support for new-style type hints in older versions of Python and Typer (e.g. str | None).

Dependency Injection

The terracomp_typer.DependencyInjector is essentially a mapping of types to a corresponding implementation. It allows you to bind any function to the given dependencies based on the function signature.

The build_app_from_module() takes a dependencies argument which populates a DependencyInjector. All callback() and main() functions encountered and added to a typer.Typer object are first bound to the dependencies that can be served by the injector.

The types for which injection can take place must be known in advance. If the implementation is not known in advance, a callback() can accept the DependencyInjector as an argument and inform about the dependencies that will be provided by the callback, allowing any of its subcommands to resolve it.

# src/mypackages/commands/__init__.py
"""
This is a cool CLI that uses terracomp-typer.
"""

from mypackage.config import CliConfig
from pathlib import Path
from terracomp_typer import DependencyInjector, DelayedBinding
from typer import Option

def callback(
    config_file: Path = Option(Path("~/.config/mypackage.ini"), help="Path to the configuration file."),
    dependencies: DependencyInjector = DelayedBinding(CliConfig),
) -> None:
    dependencies.register_supplier(CliConfig, lambda: CliConfig.load(config_file))
# src/mypackage/commands/hello.py
from mypackage.config import CliConfig

def main(name: str, config: CliConfig) -> None:
    # ...

In the above example, the config parameter is not passed by Typer, but instead by the DependencyInjector per the implementation in the previous callback() snippet.

Known caveats

  • Only concrete types are supported (no Optional[CliConfig] or vice versa).

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. Usually, ty

# src/mypackage/commands/hello.py
from mypackage.config import CliConfig

def main(name: str | None = None) -> None:
    # ...

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

terracomp_typer-0.0.4.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

terracomp_typer-0.0.4-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file terracomp_typer-0.0.4.tar.gz.

File metadata

  • Download URL: terracomp_typer-0.0.4.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.28.2 requests-toolbelt/0.10.1 urllib3/1.26.14 tqdm/4.64.1 importlib-metadata/6.0.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.9

File hashes

Hashes for terracomp_typer-0.0.4.tar.gz
Algorithm Hash digest
SHA256 48f278ea331715f47aca25b300101c9b57a9f71cd0a4e15223c11cf6ed3cc1e2
MD5 a126ae9295420c1e93606ca145fd14af
BLAKE2b-256 5c13c2c8bef5e5d44ec0dc0c187d66d4d1a702b8f5844ebe884443b4216daa7e

See more details on using hashes here.

File details

Details for the file terracomp_typer-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: terracomp_typer-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 8.0 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.28.2 requests-toolbelt/0.10.1 urllib3/1.26.14 tqdm/4.64.1 importlib-metadata/6.0.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.9

File hashes

Hashes for terracomp_typer-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6eae42cb85679d335da3c28da9a281dea30ac94307cef29ddc4fe19c19693b33
MD5 5f6faa0309bfc9df9b1ea7bedd748760
BLAKE2b-256 7553c7834bcd1c4efc89a4b499bf3ec21577ce50b38853001ee301686ac6c0f5

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