Skip to content

Commit fef63fd

Browse files
committed
add test for #75, simplify BFS for tree structures, tests for coverage
1 parent 2ed5f13 commit fef63fd

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
lines changed

check.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ else
1313
fi
1414

1515
poetry run mypy django_typer
16-
pyright
16+
poetry run pyright
1717
poetry check
1818
poetry run pip check
1919
cd ./doc

django_typer/__init__.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,9 +1768,6 @@ def _names(tc: t.Union[typer.models.CommandInfo, Typer]) -> t.List[str]:
17681768
names.append(tc.name)
17691769
if tc.info.name and tc.info.name != tc.name:
17701770
names.append(tc.info.name)
1771-
cb_name = getattr(tc.info.callback, "__name__", None)
1772-
if cb_name and cb_name not in names:
1773-
names.append(cb_name)
17741771
return names
17751772

17761773

@@ -1799,24 +1796,21 @@ def find_at_level(
17991796
if found := find_at_level(app):
18001797
return found
18011798

1802-
visited = set()
18031799
bfs_order: t.List[Typer] = []
18041800
queue = deque([app])
18051801

18061802
while queue:
18071803
grp = queue.popleft()
1808-
if grp not in visited:
1809-
visited.add(grp)
1810-
bfs_order.append(grp)
1811-
# if names conflict, only pick the first the others have been
1812-
# overridden - avoids walking down stale branches
1813-
seen = []
1814-
for child_grp in reversed(grp.registered_groups):
1815-
child_app = t.cast(Typer, child_grp.typer_instance)
1816-
assert child_app
1817-
if child_app not in visited and child_app.name not in seen:
1818-
seen.extend(_names(child_app))
1819-
queue.append(child_app)
1804+
bfs_order.append(grp)
1805+
# if names conflict, only pick the first the others have been
1806+
# overridden - avoids walking down stale branches
1807+
seen = []
1808+
for child_grp in reversed(grp.registered_groups):
1809+
child_app = t.cast(Typer, child_grp.typer_instance)
1810+
assert child_app
1811+
if child_app.name not in seen:
1812+
seen.extend(_names(child_app))
1813+
queue.append(child_app)
18201814

18211815
for grp in bfs_order[1:]:
18221816
found = find_at_level(grp)
@@ -1968,7 +1962,7 @@ def command_bases() -> t.Generator[t.Type[TyperCommand], None, None]:
19681962
yield base
19691963

19701964
typer_app = Typer(
1971-
name=name or attrs["__module__"].rsplit(".", maxsplit=1)[-1],
1965+
name=name or attrs.get("__module__", "").rsplit(".", maxsplit=1)[-1],
19721966
cls=kwargs.pop("cls", DTGroup),
19731967
help=help or attr_help, # pyright: ignore[reportArgumentType]
19741968
invoke_without_command=invoke_without_command,

django_typer/tests/shellcompletion/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ def run_app_completion(self):
181181
self.assertIn("sessions", completions)
182182
self.assertIn("staticfiles", completions)
183183

184+
def run_bad_command_completion(self):
185+
completions = self.get_completions(
186+
self.launch_script, "completion_does_not_exist", " "
187+
)
188+
self.assertTrue("Exception" not in completions)
189+
self.assertTrue("traceback" not in completions)
190+
184191
def run_command_completion(self):
185192
completions = self.get_completions(self.launch_script, "complet")
186193
# annoingly in CI there are some spaces inserted between the incomplete phrase
@@ -197,6 +204,7 @@ def test_shell_complete(self):
197204
self.run_app_completion()
198205
self.install()
199206
self.run_app_completion()
207+
self.run_bad_command_completion()
200208
self.run_command_completion()
201209
self.remove()
202210
with self.assertRaises(AssertionError):

django_typer/tests/test_interface.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,6 @@ def sqrt():
324324

325325
with self.assertRaises(AttributeError):
326326
TyperCommandMeta.maths
327+
328+
with self.assertRaises(AttributeError):
329+
TyperCommandMeta("dummy", tuple(), {}).maths

0 commit comments

Comments
 (0)