Skip to content

Commit

Permalink
fix completers linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bckohan committed Jan 27, 2024
1 parent cf08161 commit 06af219
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
41 changes: 31 additions & 10 deletions django_typer/completers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
"""
A collection of completer classes that can be used to quickly add shell completion
for various kinds of django objects.
# TODO: - add default query builders for these field types:
GenericIPAddressField,
TimeField,
DateField,
DateTimeField,
DurationField,
FilePathField,
FileField
"""

import typing as t
Expand All @@ -9,7 +18,7 @@
from click import Context, Parameter
from click.shell_completion import CompletionItem
from django.apps import apps
from django.db.models import ( # TODO:; GenericIPAddressField,; TimeField,; DateField,; DateTimeField,; DurationField,; FilePathField,; FileField
from django.db.models import (
CharField,
DecimalField,
FloatField,
Expand Down Expand Up @@ -78,18 +87,22 @@ def default_query(
:raises TypeError: If there is a problem using the incomplete string as a
lookup given the field class.
"""
field = self.model_cls._meta.get_field(self.lookup_field)
field = self.model_cls._meta.get_field(
self.lookup_field
) # pylint: disable=protected-access
if issubclass(field.__class__, IntegerField):
return self.int_query(context, parameter, incomplete)
elif issubclass(field.__class__, (CharField, TextField)):
if issubclass(field.__class__, (CharField, TextField)):
return self.text_query(context, parameter, incomplete)
elif issubclass(field.__class__, UUIDField):
if issubclass(field.__class__, UUIDField):
return self.uuid_query(context, parameter, incomplete)
elif issubclass(field.__class__, (FloatField, DecimalField)):
if issubclass(field.__class__, (FloatField, DecimalField)):
return self.float_query(context, parameter, incomplete)
raise ValueError(f"Unsupported lookup field class: {field.__class__.__name__}")

def int_query(self, context: Context, parameter: Parameter, incomplete: str) -> Q:
def int_query( # pylint: disable=unused-argument
self, context: Context, parameter: Parameter, incomplete: str
) -> Q:
"""
The default completion query builder for integer fields. This method will
return a Q object that will match any value that starts with the incomplete
Expand All @@ -114,7 +127,9 @@ def int_query(self, context: Context, parameter: Parameter, incomplete: str) ->
)
return qry

def float_query(self, context: Context, parameter: Parameter, incomplete: str):
def float_query( # pylint: disable=unused-argument
self, context: Context, parameter: Parameter, incomplete: str
) -> Q:
"""
The default completion query builder for float fields. This method will
return a Q object that will match any value that starts with the incomplete
Expand All @@ -137,7 +152,9 @@ def float_query(self, context: Context, parameter: Parameter, incomplete: str):
**{f"{self.lookup_field}__lt": upper}
)

def text_query(self, context: Context, parameter: Parameter, incomplete: str) -> Q:
def text_query( # pylint: disable=unused-argument
self, context: Context, parameter: Parameter, incomplete: str
) -> Q:
"""
The default completion query builder for text-based fields. This method will
return a Q object that will match any value that starts with the incomplete
Expand All @@ -152,7 +169,9 @@ def text_query(self, context: Context, parameter: Parameter, incomplete: str) ->
return Q(**{f"{self.lookup_field}__istartswith": incomplete})
return Q(**{f"{self.lookup_field}__startswith": incomplete})

def uuid_query(self, context: Context, parameter: Parameter, incomplete: str) -> Q:
def uuid_query( # pylint: disable=unused-argument
self, context: Context, parameter: Parameter, incomplete: str
) -> Q:
"""
The default completion query builder for UUID fields. This method will
return a Q object that will match any value that starts with the incomplete
Expand Down Expand Up @@ -219,7 +238,9 @@ def __call__(

if incomplete:
try:
completion_qry &= self.query(context, parameter, incomplete)
completion_qry &= self.query( # pylint: disable=not-callable
context, parameter, incomplete
)
except (ValueError, TypeError):
return []

Expand Down
4 changes: 2 additions & 2 deletions django_typer/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def test_get_command(self):
get_command("callback1", "init")


class CallbackTests(TestCase):
class CallbackTests(NoColorMixin, TestCase):
cmd_name = "callback1"

def test_helps(self, top_level_only=False):
Expand Down Expand Up @@ -571,7 +571,7 @@ def test_call_direct(self):
)


class Callback2Tests(NoColorMixin, CallbackTests):
class Callback2Tests(CallbackTests):
cmd_name = "callback2"

def test_call_command(self):
Expand Down

0 comments on commit 06af219

Please sign in to comment.