From d2c6c9da7ae51fa993cca7146ebf4ef6dbd1b822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Thu, 2 Jan 2020 17:07:50 +0100 Subject: [PATCH] Type hints in shared.views.autocomplete --- shared/views/autocomplete.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/shared/views/autocomplete.py b/shared/views/autocomplete.py index 7fc7a886..270eae63 100644 --- a/shared/views/autocomplete.py +++ b/shared/views/autocomplete.py @@ -1,18 +1,22 @@ -from dal import autocomplete -from django.db.models import Q +from typing import Generic, Iterable, Type, TypeVar + +from dal import autocomplete # type: ignore +from django.db.models import Model, Q + +M = TypeVar("M", bound=Model) -class ModelSearch: +class ModelSearch(Generic[M]): """Basic search engine for models based on filtering. Subclasses should override the ``model`` class attribute and specify the list of search fields to be searched in. """ - model = None - search_fields = [] + model: Type[M] + search_fields: Iterable[str] - def get_queryset_filter(self, keywords): + def get_queryset_filter(self, keywords: Iterable[str]) -> Q: filter_q = Q() if not keywords: @@ -26,7 +30,7 @@ class ModelSearch: return filter_q - def search(self, keywords): + def search(self, keywords: Iterable[str]) -> Iterable[M]: """Returns the queryset of model instances matching all the keywords. The semantic of the search is the following: a model instance appears in the