Skip to content

Commit 17fd306

Browse files
committed
fix some completer issues, make sure settings arent accessed in module load
1 parent 3683bf4 commit 17fd306

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

django_typer/completers.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from uuid import UUID
2727

2828
from click import Context, Parameter
29+
from click.core import ParameterSource
2930
from click.shell_completion import CompletionItem
3031
from django.apps import apps
3132
from django.conf import settings
@@ -321,7 +322,12 @@ def __call__(
321322
return []
322323

323324
excluded: t.List[t.Type[Model]] = []
324-
if self.distinct and parameter.name:
325+
if (
326+
self.distinct
327+
and parameter.name
328+
and context.get_parameter_source(parameter.name)
329+
is not ParameterSource.DEFAULT
330+
):
325331
excluded = context.params.get(parameter.name, []) or []
326332

327333
return [
@@ -380,7 +386,12 @@ def handle(
380386
:return: A list of matching app labels or names. Labels already present for the
381387
parameter on the command line will be filtered out.
382388
"""
383-
present = [app.label for app in (ctx.params.get(param.name or "") or [])]
389+
present = []
390+
if (
391+
param.name
392+
and ctx.get_parameter_source(param.name) is not ParameterSource.DEFAULT
393+
):
394+
present = [app.label for app in (ctx.params.get(param.name) or [])]
384395
ret = [
385396
CompletionItem(app.label)
386397
for app in apps.get_app_configs()
@@ -503,29 +514,38 @@ def exists(pth: Path) -> bool:
503514
"""
504515

505516

506-
def these_strings(strings: t.List[str], allow_duplicates: bool = False):
517+
def these_strings(
518+
strings: t.Union[t.Callable[[], t.Sequence[str]], t.Sequence[str]],
519+
allow_duplicates: bool = False,
520+
):
507521
"""
508522
Get a completer that provides completion logic that matches the allowed strings.
509523
510-
:param strings: A list of allowed strings.
524+
:param strings: A sequence of allowed strings or a callable that generates a sequence of
525+
allowed strings.
511526
:param allow_duplicates: Whether or not to allow duplicate values. Defaults to False.
512527
:return: A completer function.
513528
"""
514529

515530
def complete(ctx: Context, param: Parameter, incomplete: str):
516531
present = []
517-
if not allow_duplicates:
518-
present = [value for value in (ctx.params.get(param.name or "") or [])]
532+
if (
533+
not allow_duplicates
534+
and param.name
535+
and ctx.get_parameter_source(param.name) is not ParameterSource.DEFAULT
536+
):
537+
present = [value for value in (ctx.params.get(param.name) or [])]
519538
return [
520539
CompletionItem(item)
521-
for item in strings
540+
for item in (strings() if callable(strings) else strings)
522541
if item.startswith(incomplete) and item not in present
523542
]
524543

525544
return complete
526545

527546

528-
databases = these_strings([alias for alias in settings.DATABASES.keys()])
547+
# use a function that returns a generator because we should not access settings on import
548+
databases = these_strings(lambda: settings.DATABASES.keys())
529549
"""
530550
A completer that provides completion logic for the Django database aliases
531551
configured in settings.DATABASES.

0 commit comments

Comments
 (0)