2019-12-24 13:31:59 +01:00
What inspired **Typer** , how it compares to other alternatives and what it learned from them.
2020-03-11 12:13:50 +01:00
## Intro
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
**Typer** wouldn't exist if not for the previous work of others.
2019-12-24 13:31:59 +01:00
There have been many tools created before that have helped inspire its creation.
## Previous tools
2020-03-11 12:13:50 +01:00
### <a href="https://docs.python.org/3/library/argparse.html" class="external-link" target="_blank">`argparse`</a>
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
`argparse` is the Python standard library's module to write CLIs.
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
It provides a better alternative than reading the *CLI Parameters* as a `list` of `str` and parsing everything by hand.
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
!!! check "Inspired **Typer** to"
Provide a better development experience than just reading *CLI Parameters* by hand.
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
### <a href="https://www.hug.rest/" class="external-link" target="_blank">Hug</a>
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
Hug is a library to create APIs and CLIs, it uses parameters in functions to declare the required data.
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
It inspired a lot of the ideas in **FastAPI** and **Typer** .
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
!!! check "Inspired **Typer** to"
:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)
* :sparkles: Implement custom handling for callbacks with types
* :sparkles: Implement type-based handling for completion functions
* :sparkles: Add examples for custom completion
* :memo: Add docs for custom autocompletion
* :white_check_mark: Add tests for autocompletion
* :sparkles: Document using the context
* :white_check_mark: Add tests for using the context
* :white_check_mark: Add tests init for context
* :wrench: Update Mypy config
* :white_check_mark: Add extra tests for completion
* :pencil2: Fix format, typos, and minor texts
* :sparkles: Update docs for printing, include stdout, stderr, and stdin
* :pencil2: Add references to FastAPI, fix typos
* :pencil2: Fix typo in Typer CLI
* :memo: Update docs for version
* :memo: Update docs for callbacks
* :memo: Update references to stdout/stderr in autocompletion
* :memo: Add docs for password in CLI option
* :memo: Add docs and tests for Testing
* :memo: Add new sections to docs
* :art: Fix formatting
2020-03-18 21:17:47 +01:00
Use function parameters to declare *CLI arguments* and *CLI options* as it simplifies a lot the development experience.
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
### <a href="https://micheles.github.io/plac/" class="external-link" target="_blank">Plac</a>
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
Plac is another library to create CLIs using parameters in functions, similar to Hug.
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
!!! check "Inspired **Typer** to"
Provide a simple way to use a function as a command line app, without having to create a complete app, with `typer.run(some_function)` .
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
### <a href="https://pydantic-docs.helpmanual.io/" class="external-link" target="_blank">Pydantic</a>
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
Pydantic is a library to handle data validation using standard modern Python type annotations.
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
It powers **FastAPI** underneath.
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
It is not used by **Typer** , but it inspired a lot of the design (through **FastAPI** ).
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
!!! check "Inspired **Typer** to"
Use standard Python type annotations to declare types instead of library-specific types or classes and use them for data validation and documentation.
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
### <a href="https://click.palletsprojects.com" class="external-link" target="_blank">Click</a>
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
Click is one of the most widely used libraries to create CLIs in Python.
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
It's a very powerful tool and there are many CLIs built with it. It is what powers **Typer** underneath.
2019-12-24 13:31:59 +01:00
:sparkles: Add docs for testing, password, version, context. Extra tests, include callback and autocompletion PR (#68)
* :sparkles: Implement custom handling for callbacks with types
* :sparkles: Implement type-based handling for completion functions
* :sparkles: Add examples for custom completion
* :memo: Add docs for custom autocompletion
* :white_check_mark: Add tests for autocompletion
* :sparkles: Document using the context
* :white_check_mark: Add tests for using the context
* :white_check_mark: Add tests init for context
* :wrench: Update Mypy config
* :white_check_mark: Add extra tests for completion
* :pencil2: Fix format, typos, and minor texts
* :sparkles: Update docs for printing, include stdout, stderr, and stdin
* :pencil2: Add references to FastAPI, fix typos
* :pencil2: Fix typo in Typer CLI
* :memo: Update docs for version
* :memo: Update docs for callbacks
* :memo: Update references to stdout/stderr in autocompletion
* :memo: Add docs for password in CLI option
* :memo: Add docs and tests for Testing
* :memo: Add new sections to docs
* :art: Fix formatting
2020-03-18 21:17:47 +01:00
It also uses functions with parameters for *CLI arguments* and *CLI options* , but the declaration of the specific *CLI arguments* , *CLI options* , types, etc, is done in decorators on top of the function. This requires some code repetition (e.g. a *CLI Option* name `--verbose` and a variable name `verbose` ) and synchronization between two places related to the same information (the decorator and the parameter function).
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
It uses decorators on top of functions to modify the actual value of those functions, converting them to instances of a specific class. This is a clever trick, but code editors can't provide great support for autocompletion that way.
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
It was built with some great ideas and design using the features available in the language at the time (Python 2.x).
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
!!! check "**Typer** uses it for"
Everything. 🚀
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
**Typer** mainly adds a layer on top of Click, making the code simpler and easier to use, with autocompletion everywhere, etc, but providing all the powerful features of Click underneath.
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
As someone pointed out: < em > < a href = "https://twitter.com/fishnets88/status/1210126833745838080" class = "external-link" target = "_blank" > "Nice to see it is built on Click but adds the type stuff. Me gusta!"< / a > < / em >
2019-12-24 13:31:59 +01:00
2020-03-16 13:21:58 +01:00
### <a href="https://github.com/click-contrib/click-completion" class="external-link" target="_blank">`click-completion`</a>
`click-completion` is a plug-in for Click. It was created to extend completion support for shells when Click only had support for Bash completion.
Previous versions of **Typer** had deep integrations with `click-completion` and used it as an optional dependency. But now all the completion logic is implemented internally in **Typer** itself, the internal logic was heavily inspired and using some parts of `click-completion` .
And now **Typer** improved it to have new features, tests, some bug fixes (for issues in plain `click-completion` and Click), and better support for shells, including modern versions of PowerShell (e.g. the default versions that come with Windows 10).
!!! check "Inspired **Typer** to"
Provide auto completion for all the shells.
2020-03-11 12:13:50 +01:00
### <a href="https://fastapi.tiangolo.com/" class="external-link" target="_blank">FastAPI</a>
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
I created **FastAPI** to provide an easy way to build APIs with autocompletion for everything in the code (and some other < a href = "https://fastapi.tiangolo.com/features/" class = "external-link" target = "_blank" > features</ a > ).
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
**Typer** is the "FastAPI of CLIs".
2019-12-24 13:31:59 +01:00
2020-03-11 12:13:50 +01:00
It uses the same design and usage of FastAPI as much as possible. So, if you have used FastAPI, you know how to use Typer.