Skip to content
Open
26 changes: 5 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,34 +356,18 @@ For a more complete example including more features, see the <a href="https://ty

## Dependencies

**Typer** stands on the shoulders of a giant. Its only internal required dependency is <a href="https://click.palletsprojects.com/" class="external-link" target="_blank">Click</a>.

By default it also comes with extra standard dependencies:
**Typer** stands on the shoulders of giants. It has three required dependencies:

* <a href="https://click.palletsprojects.com/" class="external-link" target="_blank">Click</a>: a popular tool for building CLIs in Python. Typer is based on it.
* <a href="https://rich.readthedocs.io/en/stable/index.html" class="external-link" target="_blank"><code>rich</code></a>: to show nicely formatted errors automatically.
* <a href="https://github.com/sarugaku/shellingham" class="external-link" target="_blank"><code>shellingham</code></a>: to automatically detect the current shell when installing completion.
* With `shellingham` you can just use `--install-completion`.
* Without `shellingham`, you have to pass the name of the shell to install completion for, e.g. `--install-completion bash`.

### `typer-slim`

If you don't want the extra standard optional dependencies, install `typer-slim` instead.

When you install with:

```bash
pip install typer
```

...it includes the same code and dependencies as:

```bash
pip install "typer-slim[standard]"
```

The `standard` extra dependencies are `rich` and `shellingham`.
There used to be a slimmed-down version of Typer called `typer-slim`, which didn't include the dependencies `rich` and `shellingham`, nor the `typer` command.

**Note**: The `typer` command is only included in the `typer` package.
However, since version 0.22.0, we have stopped supporting this, and `typer-slim` now simply installs (all of) Typer.
If you want to disable Rich globally, you can set an environmental variable "TYPER_USE_RICH" to `False`/`0`.

## License

Expand Down
9 changes: 1 addition & 8 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,13 @@ The resulting CLI apps created with **Typer** have the nice features of many "pr

Auto completion works when you create a package (installable with `pip`). Or when using the `typer` command.

**Typer** uses `shellingham` to auto-detect the current shell when installing completion. If you don't want to include `shellingham`, install `typer-slim`.
**Typer** uses `shellingham` to auto-detect the current shell when installing completion.

**Typer** will automatically create 2 *CLI options*:

* `--install-completion`: Install completion for the current shell.
* `--show-completion`: Show completion for the current shell, to copy it or customize the installation.

If you didn't add `shellingham` (if you installed `pip install typer-slim`) those *CLI options* take a value with the name of the shell to install completion for, e.g.:

* `--install-completion bash`.
* `--show-completion powershell`.

Then you can tell the user to install completion after installing your CLI program and the rest will just work.

///

/// tip
Expand Down
26 changes: 5 additions & 21 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,34 +362,18 @@ For a more complete example including more features, see the <a href="https://ty

## Dependencies

**Typer** stands on the shoulders of a giant. Its only internal required dependency is <a href="https://click.palletsprojects.com/" class="external-link" target="_blank">Click</a>.

By default it also comes with extra standard dependencies:
**Typer** stands on the shoulders of giants. It has three required dependencies:

* <a href="https://click.palletsprojects.com/" class="external-link" target="_blank">Click</a>: a popular tool for building CLIs in Python. Typer is based on it.
* <a href="https://rich.readthedocs.io/en/stable/index.html" class="external-link" target="_blank"><code>rich</code></a>: to show nicely formatted errors automatically.
* <a href="https://github.com/sarugaku/shellingham" class="external-link" target="_blank"><code>shellingham</code></a>: to automatically detect the current shell when installing completion.
* With `shellingham` you can just use `--install-completion`.
* Without `shellingham`, you have to pass the name of the shell to install completion for, e.g. `--install-completion bash`.

### `typer-slim`

If you don't want the extra standard optional dependencies, install `typer-slim` instead.

When you install with:

```bash
pip install typer
```

...it includes the same code and dependencies as:

```bash
pip install "typer-slim[standard]"
```

The `standard` extra dependencies are `rich` and `shellingham`.
There used to be a slimmed-down version of Typer called `typer-slim`, which didn't include the dependencies `rich` and `shellingham`, nor the `typer` command.

**Note**: The `typer` command is only included in the `typer` package.
However, since version 0.22.0, we have stopped supporting this, and `typer-slim` now simply installs (all of) Typer.
If you want to disable Rich globally, you can set an environmental variable "TYPER_USE_RICH" to `False`/`0`.

## License

Expand Down
9 changes: 4 additions & 5 deletions docs/tutorial/commands/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,18 @@ If there are multiple close matches, Typer will suggest them all. This feature u

## Rich Markdown and Markup

If you have **Rich** installed as described in [Printing and Colors](../printing.md){.internal-link target=_blank}, you can configure your app to enable markup text with the parameter `rich_markup_mode`.

Then you can use more formatting in the docstrings and the `help` parameter for *CLI arguments* and *CLI options*. You will see more about it below. 👇
Typer installs **Rich** to allow for more formatting in the docstrings and the `help` parameter for *CLI arguments* and *CLI options*. You will see more about it below. 👇

/// info

By default, `rich_markup_mode` is `None` if Rich is not installed, and `"rich"` if it is installed. In the latter case, you can set `rich_markup_mode` to `None` to disable rich text formatting.
You can disable rich text formatting by setting `rich_markup_mode` to `None` for your specific app.
Alternatively, you can disable it globally using an environmental variable "TYPER_USE_RICH" set to `False`/`0`.

///

### Rich Markup

If you set `rich_markup_mode="rich"` when creating the `typer.Typer()` app, you will be able to use <a href="https://rich.readthedocs.io/en/stable/markup.html" class="external-link" target="_blank">Rich Console Markup</a> in the docstring, and even in the help for the *CLI arguments* and options:
If you set `rich_markup_mode="rich"` when creating the `typer.Typer()` app (which is the default), you will be able to use <a href="https://rich.readthedocs.io/en/stable/markup.html" class="external-link" target="_blank">Rich Console Markup</a> in the docstring, and even in the help for the *CLI arguments* and options:

{* docs_src/commands/help/tutorial004_an_py39.py hl[5,11,15:17,22,25,28] *}

Expand Down
5 changes: 3 additions & 2 deletions docs/tutorial/exceptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This code is broken because you can't sum a string and a number (`name + 3`).

## Exceptions with Rich

If you have **Rich** installed (for example if you installed `"typer[all]"`), **Typer** will use it to automatically show you nicely printed errors.
**Typer** will automatically use Rich to automatically show you nicely printed errors.

It will **omit** all the parts of the traceback (the chain of things that called your function) that come from the internal parts in Typer and Click.

Expand Down Expand Up @@ -47,7 +47,8 @@ $ python main.py

## Exceptions without Rich

If you don't have Rich installed, Typer will still do some tricks to show you the information **as clearly as possible**:
You can disable Rich globally using the environmental variable `TYPER_USE_RICH`.
In this case, Typer will still do some tricks to show you the information **as clearly as possible**:

<div class="termy">

Expand Down
10 changes: 0 additions & 10 deletions docs/tutorial/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,8 @@ Successfully installed typer click shellingham rich

By default, `typer` comes with `rich` and `shellingham`.

/// note

If you are an advanced user and want to opt out of these default extra dependencies, you can instead install `typer-slim`.

```bash
pip install typer
```

...includes the same optional dependencies as:

```bash
pip install "typer-slim[standard]"
```

///
2 changes: 1 addition & 1 deletion docs/tutorial/options-autocompletion.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ Using `stderr=True` tells **Rich** that the output should be shown in "standard

/// info

If you can't install and use Rich, you can also use `print(lastname, file=sys.stderr)` or `typer.echo("some text", err=True)` instead.
If you have disabled Rich, you can also use `print(lastname, file=sys.stderr)` or `typer.echo("some text", err=True)` instead.

///

Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial/options/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Options:

The same as with *CLI arguments*, you can put the help for some *CLI options* in different panels to be shown with the `--help` option.

If you have installed Rich as described in the docs for [Printing and Colors](../printing.md){.internal-link target=_blank}, you can set the `rich_help_panel` parameter to the name of the panel you want for each *CLI option*:
Using Rich, you can set the `rich_help_panel` parameter to the name of the panel you want for each *CLI option*:

{* docs_src/options/help/tutorial002_an_py39.py hl[15,21] *}

Expand Down
6 changes: 0 additions & 6 deletions docs/tutorial/package.md
Original file line number Diff line number Diff line change
Expand Up @@ -681,12 +681,6 @@ You just have to pass it the module to import (`rick_portal_gun.main`) and it wi

By specifying the `--name` of the program it will be able to use it while generating the docs.

/// tip

If you installed `typer-slim` and don't have the `typer` command, you can use `python -m typer` instead.

///

### Publish a new version with the docs

Now you can publish a new version with the updated docs.
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial/printing.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ In general, **Typer** tends to be the entry point to your program, taking the fi

**Rich** is useful for the parts that need to *display* information. Showing beautiful content on the screen.

The best results for your command line application would be achieved combining both **Typer** and **Rich**.
The best results for your command line application are achieved combining both **Typer** and **Rich**.

## "Standard Output" and "Standard Error"

Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial/progressbar.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ If you can use Rich, use the information above, the Rich docs, and skip the rest

///

But if you can't use Rich, Typer (actually Click) comes with a simple utility to show progress bars.
But if you can't use Rich and have it disabled, Typer (actually Click) comes with a simple utility to show progress bars.

/// info

Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial/prompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Aborted!

## Prompt with Rich

If you installed Rich as described in [Printing and Colors](printing.md){.internal-link target=_blank}, you can use Rich to prompt the user for input:
You can use Rich to prompt the user for input:

{* docs_src/prompt/tutorial004_py39.py hl[2,9] *}

Expand Down
10 changes: 2 additions & 8 deletions docs/tutorial/typer-command.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ pip install typer

...it includes the `typer` command.

If you don't want to have the `typer` command, you can install instead:

```bash
pip install typer-slim
```

You can still use it by calling the Typer library as a module with:
If you don't want to use the `typer` command, you can call the Typer library as a module with:

```bash
python -m typer
Expand Down Expand Up @@ -261,7 +255,7 @@ $ typer some_script.py utils docs

/// tip

If you installed only `typer-slim` and you don't have the `typer` command, you can still generate docs with:
If you don't want to use the `typer` command, you can still generate docs with:

```console
$ python -m typer some_script.py utils docs
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ sync-dependencies = [

[tool.tiangolo._internal-slim-build.packages.typer-slim.project]
name = "typer-slim"
dependencies = [
"typer",
]

[tool.tiangolo._internal-slim-build.packages.typer]
include-optional-dependencies = ["standard"]
Expand Down
20 changes: 7 additions & 13 deletions typer/_completion_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
from typing import Optional, Union

import click
from typer.core import HAS_SHELLINGHAM

if HAS_SHELLINGHAM:
import shellingham
import shellingham


class Shells(str, Enum):
Expand Down Expand Up @@ -246,14 +243,11 @@ def _get_shell_name() -> Union[str, None]:
returned.
"""
name: Union[str, None] # N.B. shellingham is untyped
if HAS_SHELLINGHAM:
try:
# N.B. detect_shell returns a tuple of (shell name, shell command).
# We only need the name.
name, _cmd = shellingham.detect_shell() # noqa: TID251
except shellingham.ShellDetectionFailure: # pragma: no cover
name = None
else:
name = None # pragma: no cover
try:
# N.B. detect_shell returns a tuple of (shell name, shell command).
# We only need the name.
name, _cmd = shellingham.detect_shell() # noqa: TID251
except shellingham.ShellDetectionFailure: # pragma: no cover
name = None

return name
3 changes: 1 addition & 2 deletions typer/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from ._completion_classes import completion_init
from ._completion_shared import Shells, _get_shell_name, get_completion_script, install
from .core import HAS_SHELLINGHAM
from .models import ParamMeta
from .params import Option
from .utils import get_params_from_function
Expand All @@ -18,7 +17,7 @@
def get_completion_inspect_parameters() -> tuple[ParamMeta, ParamMeta]:
completion_init()
test_disable_detection = os.getenv("_TYPER_COMPLETE_TEST_DISABLE_SHELL_DETECTION")
if HAS_SHELLINGHAM and not test_disable_detection:
if not test_disable_detection:
parameters = get_params_from_function(_install_completion_placeholder_function)
else:
parameters = get_params_from_function(
Expand Down
4 changes: 1 addition & 3 deletions typer/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import errno
import importlib.util
import inspect
import os
import sys
Expand Down Expand Up @@ -28,8 +27,7 @@
MarkupMode = Literal["markdown", "rich", None]
MARKUP_MODE_KEY = "TYPER_RICH_MARKUP_MODE"

HAS_RICH = importlib.util.find_spec("rich") is not None
HAS_SHELLINGHAM = importlib.util.find_spec("shellingham") is not None
HAS_RICH = os.getenv("TYPER_USE_RICH", True)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to rename this one to USE_RICH instead of HAS_RICH but that causes errors in the redistribution tests as they'd use an old pip-installed typer.


if HAS_RICH:
DEFAULT_MARKUP_MODE: MarkupMode = "rich"
Expand Down