Skip to content

Commit 56b08e7

Browse files
committed
fixes #39
1 parent 03dc68d commit 56b08e7

File tree

6 files changed

+57
-12
lines changed

6 files changed

+57
-12
lines changed

django_typer/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
from typing import ParamSpec
114114

115115

116-
VERSION = (1, 0, 3)
116+
VERSION = (1, 0, 4)
117117

118118
__title__ = "Django Typer"
119119
__version__ = ".".join(str(i) for i in VERSION)

django_typer/parsers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ def handle(
7777
case_insensitive: bool = False
7878
on_error: t.Optional[error_handler] = None
7979

80-
_lookup = ""
80+
_lookup: str = ""
8181
_field: Field
8282
_completer: ModelObjectCompleter
8383

84-
__name__ = "ModelObjectParser" # typer internals expect this
84+
__name__: str = "MODEL" # typer internals expect this
8585

8686
def __init__(
8787
self,
@@ -105,7 +105,7 @@ def __init__(
105105
self._field = field
106106
if self.case_insensitive and "iexact" in self._field.get_lookups():
107107
self._lookup = "__iexact"
108-
self.name = (
108+
self.__name__ = (
109109
str(self.model_cls._meta.verbose_name)
110110
if self.model_cls._meta.verbose_name
111111
else self.model_cls.__name__
@@ -191,3 +191,6 @@ def handle(
191191
raise CommandError(
192192
_("{label} does not match any installed app label.").format(label=label)
193193
) from err
194+
195+
196+
parse_app_label.__name__ = "APP_LABEL"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
import typing as t
3+
4+
if sys.version_info < (3, 9):
5+
from typing_extensions import Annotated
6+
else:
7+
from typing import Annotated
8+
9+
from typer import Option
10+
11+
from django_typer import TyperCommand, model_parser_completer
12+
from django_typer.tests.polls.models import Question as Poll
13+
14+
15+
class Command(TyperCommand):
16+
help = "Closes the specified poll for voting"
17+
18+
def handle(
19+
self,
20+
polls: Annotated[
21+
t.List[Poll],
22+
Option(**model_parser_completer(Poll, help_field="question_text")),
23+
],
24+
delete: Annotated[
25+
bool, Option(help="Delete poll instead of closing it.")
26+
] = False,
27+
):
28+
for poll in polls:
29+
poll.opened = False
30+
poll.save()
31+
self.stdout.write(
32+
self.style.SUCCESS(f'Successfully closed poll "{poll.id}"')
33+
)
34+
if delete:
35+
poll.delete()

django_typer/tests/tests.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,15 @@ def tearDown(self) -> None:
19021902
ShellCompleteTester.objects.all().delete()
19031903
return super().tearDown()
19041904

1905+
def test_model_object_parser_metavar(self):
1906+
result = run_command("poll_as_option", "--help")
1907+
found = False
1908+
for line in result[0].splitlines():
1909+
if "--polls" in line:
1910+
self.assertTrue("POLL" in line)
1911+
found = True
1912+
self.assertTrue(found)
1913+
19051914
def test_model_object_parser_idempotency(self):
19061915
from django_typer.parsers import ModelObjectParser
19071916
from django_typer.tests.polls.models import Question
@@ -2585,13 +2594,6 @@ def test_option_complete(self):
25852594
with self.assertRaises(CommandError):
25862595
call_command("shellcompletion", "complete", "noargs cmd ", shell="zsh")
25872596

2588-
def test_model_parser_name(self):
2589-
from django_typer.parsers import ModelObjectParser
2590-
from django_typer.tests.polls.models import Question
2591-
2592-
parser = ModelObjectParser(Question)
2593-
self.assertEqual(parser.name, "Poll")
2594-
25952597
def test_unsupported_field(self):
25962598
from django_typer.completers import ModelObjectCompleter
25972599

doc/source/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
Change Log
33
==========
44

5+
v1.0.4
6+
======
7+
8+
* Fixed `METAVAR when ModelObjectParser supplied should default to model name <https://github.com/bckohan/django-typer/issues/39>`_
9+
510
v1.0.3
611
======
712

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "django-typer"
3-
version = "1.0.3"
3+
version = "1.0.4"
44
description = "Use Typer to define the CLI for your Django management commands."
55
authors = ["Brian Kohan <[email protected]>"]
66
license = "MIT"

0 commit comments

Comments
 (0)