Typer

1 minute read

Updated:

Typer is python package for building python CLI. It’s built on top of Click, which is also a library for parsing CLI arguments.

Comparison

argparse

argparse is in the standard library and many are familiar with it. It’s very configurable and flexible but requires a lot of code. It feels low-level.

It feels like writing python application. Everything is an object, you’re setting properties, calling methods.

Click

Click was created to improve on the develop experience with argparse. It uses decorators on functions, instead of creating python objects. This keeps the configuration and documentation close to the function that uses it. You don’t have to rely on a code convention for your argument parser and ensure that the subcommand that needs it will be hooked up correctly.

Typer

Typer is built on top of Click. It makes use of python type annotations, which were introduced in 3.6. The type annotations remove the need to configure the parameter types for validation purposes. The annotations also allow for IDE to use autocompletion, as static typing makes for a nicer development environment and tooling.

Over time, I would not be surprised if it were eventually integrated back into Click. The strict requirement for type annotations is the only thing that might be a blocker, as Click would need to drop support for older python versions. However, this requirements could quickly go away:

  • Python 2.7 was sunsetted 9 months ago
  • Python 3 has had great backwards compatibility for upgrading the latest (3.8)

Summary

I can’t wait to give Typer a try for my next CLI application and see how much better the experience is. I’ve mostly used argparse and it’s been tolerable, once you’ve put in the time and developed mastery.

Click is a new paradigm so that was a barrier to entry. But now with the added benefits of type annotations, Typer is a clear win for improved experience.