Skip to main content

django-typer

License: MIT Ruff PyPI version PyPI pyversions PyPI djversions PyPI status PyPI - Types Documentation Status Code Cov Test Status Lint Status Published on Django Packages OpenSSF Scorecard OpenSSF Best Practices

Use static typing to define the CLI for your Django management commands with Typer. Optionally use the provided TyperCommand class that inherits from BaseCommand. This class maps the Typer interface onto a class based interface that Django developers will be familiar with. All of the BaseCommand functionality is inherited, so that TyperCommand can be a drop in replacement.

django-typer makes it easy to:

Please refer to the full documentation for more information.

django-typer example

🚨 Upgrade Notice

There are breaking changes between 3.x and 4.x. Typer now vendors Click, so django-typer no longer depends on the external click package. Any click imports will need to change. See the changelog for migration steps.

Installation

  1. Clone django-typer from GitHub or install a release off PyPI:

    pip install django-typer
    
  2. Optionally add django_typer to your INSTALLED_APPS setting:

    INSTALLED_APPS = [
        ...
        'django_typer',
    ]
    

You only need to install django_typer as an app if you want to use the shell completion command to enable tab-completion or if you would like django-typer to install rich traceback rendering for you - which it does by default if rich is also installed.

Basic Example

TyperCommand is a drop in extension to BaseCommand. All of the documented features of BaseCommand work the same way!

from django_typer.management import TyperCommand


class Command(TyperCommand):
    def handle(self, arg1: str, arg2: str, arg3: float = 0.5, arg4: int = 1):
        """
        A basic command that uses Typer
        """

Or, you may also use an interface identical to Typer's. Simply import Typer from django_typer instead of typer.

from django_typer.management import Typer

app = Typer()


@app.command()
def main(arg1: str, arg2: str, arg3: float = 0.5, arg4: int = 1):
    """
    A basic command that uses Typer
    """

Basic Example

Multiple Subcommands Example

Commands with multiple subcommands can be defined:

import typing as t

from django.utils.translation import gettext_lazy as _
from typer import Argument

from django_typer.management import TyperCommand, command


class Command(TyperCommand):
    """
    A command that defines subcommands.
    """

    @command()
    def create(
        self,
        name: t.Annotated[str, Argument(help=_("The name of the object to create."))],
    ):
        """
        Create an object.
        """

    @command()
    def delete(
        self, id: t.Annotated[int, Argument(help=_("The id of the object to delete."))]
    ):
        """
        Delete an object.
        """

Or using the typer-style interface this could be written:

from django_typer.management import Typer
import typing as t

from django.utils.translation import gettext_lazy as _
from typer import Argument

app = Typer(help="A command that defines subcommands.")


@app.command()
def create(
    name: t.Annotated[str, Argument(help=_("The name of the object to create."))],
):
    """
    Create an object.
    """


@app.command()
def delete(id: t.Annotated[int, Argument(help=_("The id of the object to delete."))]):
    """
    Delete an object.
    """

Multiple Subcommands Example Multiple Subcommands Example - create Multiple Subcommands Example - delete

Grouping and Hierarchies Example

More complex groups and subcommand hierarchies can be defined. For example, this command defines a group of commands called math, with subcommands divide and multiply. The group has a common initializer that optionally sets a float precision value. We would invoke this command like so:

./manage.py hierarchy math --precision 5 divide 10 2.1
./manage.py hierarchy math multiply 10 2

Using the class-based interface we could define the command like this:

import typing as t
from functools import reduce

from django.utils.translation import gettext_lazy as _
from typer import Argument, Option

from django_typer.management import TyperCommand, group


class Command(TyperCommand):
    # values returned from the commands are written to stdout
    print_result = True

    help = _("A more complex command that defines a hierarchy of subcommands.")

    precision = 2

    @group(help=_("Do some math at the given precision."))
    def math(
        self,
        precision: t.Annotated[
            int, Option(help=_("The number of decimal places to output."))
        ] = precision,
    ):
        self.precision = precision

    # helps can be passed to the decorators
    @math.command(help=_("Multiply the given numbers."))
    def multiply(
        self,
        numbers: t.Annotated[
            t.List[float], Argument(help=_("The numbers to multiply"))
        ],
    ):
        return f"{reduce(lambda x, y: x * y, [1, *numbers]):.{self.precision}f}"

    # or if no help is supplied to the decorators, the docstring if present
    # will be used!
    @math.command()
    def divide(
        self,
        numerator: t.Annotated[float, Argument(help=_("The numerator"))],
        denominator: t.Annotated[float, Argument(help=_("The denominator"))],
        floor: t.Annotated[bool, Option(help=_("Use floor division"))] = False,
    ):
        """
        Divide the given numbers.
        """
        if floor:
            return str(numerator // denominator)
        return f"{numerator / denominator:.{self.precision}f}"

The typer-style interface builds a TyperCommand class for us that allows you to optionally accept the self argument in your commands. We could define the above command using the typer interface like this:

import typing as t
from functools import reduce

from django.utils.translation import gettext_lazy as _
from typer import Argument, Option

from django_typer.management import Typer


app = Typer(help=_("A more complex command that defines a hierarchy of subcommands."))


# values returned from the commands are written to stdout
app.django_command.print_result = True

math_grp = Typer(help=_("Do some math at the given precision."))

app.add_typer(math_grp, name="math")


@math_grp.callback()
def math(
    self,
    precision: t.Annotated[
        int, Option(help=_("The number of decimal places to output."))
    ] = 2,
):
    self.precision = precision


@math_grp.command(help=_("Multiply the given numbers."))
def multiply(
    self,
    numbers: t.Annotated[t.List[float], Argument(help=_("The numbers to multiply"))],
):
    return f"{reduce(lambda x, y: x * y, [1, *numbers]):.{self.precision}f}"


@math_grp.command()
def divide(
    self,
    numerator: t.Annotated[float, Argument(help=_("The numerator"))],
    denominator: t.Annotated[float, Argument(help=_("The denominator"))],
    floor: t.Annotated[bool, Option(help=_("Use floor division"))] = False,
):
    """
    Divide the given numbers.
    """
    if floor:
        return str(numerator // denominator)
    return f"{numerator / denominator:.{self.precision}f}"

Grouping and Hierarchies Example Grouping and Hierarchies Example - math Grouping and Hierarchies Example - math multiply Grouping and Hierarchies Example - math divide

Metadata

Release files for django-typer 4.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-typer 4.1.1
File Size Uploaded
django_typer-4.1.1.tar.gz 3.0 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-typer 4.1.1
File Interpreter ABI Platform
django_typer-4.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 3.3 MB

Release files / django_typer-4.1.1.tar.gz

Download URL django_typer-4.1.1.tar.gz
Size 3.0 MB
Tags Source
SHA-256 checksum
How to use checksums
dbf9dab8a8a49eff1b184430595b74d6215449cfd637d1946641ebcb6c64ed94
BLAKE2b-256 checksum
How to use checksums
c574f9e2b26e9154dacecdc9f8b70a19521616d814011f9194e38a2eee2c68dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.

Transparency log

Release files / django_typer-4.1.1-py3-none-any.whl

Download URL django_typer-4.1.1-py3-none-any.whl
Size 306.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d74928974003e9e58a6c8dcb809d39bd5d4a013bf64ccf9951174f44fe10fe85
BLAKE2b-256 checksum
How to use checksums
343ec4d592e99f05fae03e3bf81e165a9ab5ee935900daed48f72763dffd7caa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

4.1.1 This release

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.9.0

2 release files

3.8.0

2 release files

3.7.4

2 release files

3.7.3

2 release files

3.7.2

2 release files

3.7.1

2 release files

3.7.0

2 release files

3.6.6

2 release files

3.6.5

2 release files

3.6.4

2 release files

3.6.3

2 release files

3.6.2

2 release files

3.6.1

2 release files

3.6.0

2 release files

3.5.1

2 release files

3.5.0

2 release files

3.4.0

2 release files

3.3.2

2 release files

3.3.1

2 release files

3.3.0

2 release files

3.2.2

2 release files

3.2.1

2 release files

3.2.0

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.6.0

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.3

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page