Skip to content

Commit 142bb0c

Browse files
committed
fix #222
1 parent 0d5f19f commit 142bb0c

File tree

6 files changed

+16
-8
lines changed

6 files changed

+16
-8
lines changed

doc/source/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
Change Log
55
==========
66

7+
v3.2.2 (2025-07-17)
8+
===================
9+
10+
* Fixed `Raise a CommandError instead of a KeyError if get_command does not find the command. <https://github.com/django-commons/django-typer/issues/222>`_
11+
712
v3.2.1 (2025-07-16)
813
===================
914

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "django-typer"
7-
version = "3.2.1"
7+
version = "3.2.2"
88
requires-python = ">=3.9,<4.0"
99
description = "Use Typer to define the CLI for your Django management commands."
1010
authors = [

src/django_typer/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
and keep a tight version lock on Typer.
3434
"""
3535

36-
VERSION = (3, 2, 1)
36+
VERSION = (3, 2, 2)
3737

3838
__title__ = "Django Typer"
3939
__version__ = ".".join(str(i) for i in VERSION)

src/django_typer/management/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,15 @@ def get_command(
221221
:param no_color: whether to disable color
222222
:param force_color: whether to force color
223223
:param kwargs: t.Any other parameters to pass through to the command constructor
224-
:raises ModuleNotFoundError: if the command is not found
224+
:raises CommandError: if the command is not found
225225
:raises LookupError: if the subcommand is not found
226226
"""
227-
module = import_module(
228-
f"{get_commands()[command_name]}.management.commands.{command_name}"
229-
)
227+
try:
228+
module = import_module(
229+
f"{get_commands()[command_name]}.management.commands.{command_name}"
230+
)
231+
except (KeyError, ModuleNotFoundError) as err:
232+
raise CommandError(f"Unknown command: {command_name}") from err
230233
cmd: BaseCommand = module.Command(
231234
stdout=stdout,
232235
stderr=stderr,

src/django_typer/management/commands/shellcompletion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ def get_completion() -> str:
508508
cmd_idx += 1
509509
try:
510510
cmd = get_command(args[cmd_idx])
511-
except KeyError:
511+
except CommandError:
512512
pass
513513
except IndexError:
514514
if command.endswith(" "):

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)