{"id":15961394,"url":"https://github.com/niklasrosenstein/python-typer-builder","last_synced_at":"2025-04-04T12:13:13.416Z","repository":{"id":196678706,"uuid":"628554813","full_name":"NiklasRosenstein/python-typer-builder","owner":"NiklasRosenstein","description":"A framework for simplifying the development of Typer based CLIs supporting modern type hints and hierarchical dependency injection.","archived":false,"fork":false,"pushed_at":"2023-12-15T13:50:50.000Z","size":33,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-03T20:48:58.360Z","etag":null,"topics":["library","python","typer-cli"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NiklasRosenstein.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-04-16T10:14:34.000Z","updated_at":"2025-01-28T18:39:28.000Z","dependencies_parsed_at":null,"dependency_job_id":"3e8c832c-7808-43a8-977e-58ace87e617f","html_url":"https://github.com/NiklasRosenstein/python-typer-builder","commit_stats":null,"previous_names":["niklasrosenstein/python-typer-builder","niklasrosenstein/typer-builder"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NiklasRosenstein%2Fpython-typer-builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NiklasRosenstein%2Fpython-typer-builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NiklasRosenstein%2Fpython-typer-builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NiklasRosenstein%2Fpython-typer-builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NiklasRosenstein","download_url":"https://codeload.github.com/NiklasRosenstein/python-typer-builder/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247174468,"owners_count":20896078,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["library","python","typer-cli"],"created_at":"2024-10-07T15:42:04.490Z","updated_at":"2025-04-04T12:13:13.396Z","avatar_url":"https://github.com/NiklasRosenstein.png","language":"Python","readme":"# typer-builder\n\n  [Typer]: https://typer.tiangolo.com/\n  [pep585]: https://www.python.org/dev/peps/pep-0585/\n  [pep604]: https://www.python.org/dev/peps/pep-0604/\n  [typeapi]: https://github.com/NiklasRosenstein/python-typeapi\n\nA framework for simplifying the development of [Typer][] based CLIs supporting modern type hints and hierarchical\ndependency injection. \n\n__Table of Contents__\n\n* [Introduction](#introduction)\n  * [Example](#example)\n* [Documentation](#documentation)\n  * [New-style type hint support](#new-style-type-hint-support)\n  * [Dependency injection](#dependency-injection)\n\n\n## Introduction\n\nThe `build_app_from_module()` inspect a hierarchy of Python modules to build a Typer command and group structure.\nPackages are treated as command groups and may define a `callback()` member. Modules are treated commands and must\ndefine a `main()` member. Modules and packages prefixed with an underscore ( `_` ) are ignored. Help text is extracted\nfrom the `main()` docstring or the module docstring.\n\nIn addition, we provide support for new-style type hints ([PEP 585 - Type Hinting Generics in Standard Collections][pep585]\nand [PEP 604 - Union Operators][pep604]) in older versions of Python as well as adapt it for Typer (e.g. `list[str]`\nand `str | None`), as well as a method of injecting dependencies to functions that are not sourced from the command-line.\n\n\n### Example\n\n```\n$ tree src/mypackage/\nsrc/mypackage/\n├── __init__.py\n├── __main__.py\n└── commands\n    ├── __init__.py\n    ├── hello.py\n    └── bye.py\n```\n\n```py\n# src/mypackage/commands/hello.py\ndef main(name: str) -\u003e None:\n    print(\"Hello,\", name)\n```\n\n```py\n# src/mypackage/__main__.py\nfrom typer_builder import build_app_from_module\n\nif __name__ == \"__main__\":\n    app = build_app_from_module(\"mypackage.commands\")\n    app()\n```\n\n\n## Documentation\n\n### New-style type hint support\n\nThrough [`typeapi`][typeapi], we can convert new-tyle type hints such as `str | None` or `list[int]` to their corresponding\nrepresentation using `typing` before the function signature is parsed by [Typer][].\n\n```py\n# src/mypackage/commands/create_user.py\nfrom ___future__ import annotations\n\ndef main(name: str | None = None, groups: list[str] | None = None) -\u003e None:\n    # ...\n```\n\n[`typeapi`][typeapi] also allows us to convert `list[str]` to `List[str]` and `dict[str, int]` to `Dict[str, int]` for\nPython versions prior to 3.9. This is necessary because [Typer][] does not support the new-style type hints.\n\n### Dependency injection\n\nThe `typer_builder.Dependencies` object is used to map types to concrete values or functions that provide them.\nFunctions wrapped with `Dependencies.bind()` will have their arguments resolved by the injector based on type\nannotations. Every `build_app_from_module()` call creates a new `Dependencies` instance. Dependencies can be\ninjected from the outside by passing a `Dependencies` instance to `build_app_from_module()` or by providing\nadditional dependencies via a `callback()` function on the command group.\n\nNote that the `Dependencies` does not understand generics with different type parameters. For example, it makes\nno distinction between `MyGeneric[int]` and `MyGeneric[str]`. This is a limitation of the current implementation as well\nas the Python type system.\n\nThe most common use case for dependency injection is to inject configuration managers or clients into subcommands. For\nan example, you should check out the [examples/dependency-injection](./examples/dependency-injection) directory.\n\n\n## License\n\nThis project is licensed under the terms of the MIT license.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniklasrosenstein%2Fpython-typer-builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fniklasrosenstein%2Fpython-typer-builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fniklasrosenstein%2Fpython-typer-builder/lists"}