From 5b1ae2e5b6c0d2d799a70c3f912d31112390db5c Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Thu, 15 Apr 2021 09:07:45 -0400 Subject: [PATCH 1/2] add: nullable ListField support for ListField and inner field --- typings/mongoengine/fields.pyi | 69 +++++++++++++++++----------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/typings/mongoengine/fields.pyi b/typings/mongoengine/fields.pyi index c9eb0d4..53f1550 100644 --- a/typings/mongoengine/fields.pyi +++ b/typings/mongoengine/fields.pyi @@ -711,64 +711,65 @@ class EmbeddedDocumentField(Generic[_ST, _GT], BaseField): class DynamicField(BaseField): ... -class ListField(Generic[_T], ComplexBaseField): +_ST_INNER = TypeVar("_ST_INNER") +_GT_INNER = TypeVar("_GT_INNER") + +class ListField(Generic[_ST, _GT], ComplexBaseField): # see: https://github.com/python/mypy/issues/4236#issuecomment-521628880 @overload def __new__( cls, - field: StringField[Any, Any] = ..., - required: bool = ..., - default: Optional[Union[List[Any], Callable[[], List[Any]]]] = ..., + field: StringField[_ST_INNER, _GT_INNER] = ..., + required: Literal[False] = ..., verbose_name: str = ..., help_text: str = ..., null: bool = ..., - ) -> ListField[StringField[Any, Any]]: ... + ) -> ListField[Optional[List[_ST_INNER]], Optional[List[_GT_INNER]]]: ... @overload def __new__( cls, - field: DictField[Any], - required: bool = ..., - default: Optional[Union[List[Any], Callable[[], List[Any]]]] = ..., + field: StringField[_ST_INNER, _GT_INNER] = ..., + required: Literal[False] = ..., + default: Union[List[_ST_INNER], Callable[[], List[_ST_INNER]]] = ..., + verbose_name: str = ..., + help_text: str = ..., + null: bool = ..., + ) -> ListField[Optional[List[_ST_INNER]], List[_GT_INNER]]: ... + @overload + def __new__( + cls, + field: StringField[_ST_INNER, _GT_INNER] = ..., + required: Literal[True] = ..., + verbose_name: str = ..., + help_text: str = ..., + null: bool = ..., + ) -> ListField[List[_ST_INNER], List[_GT_INNER]]: ... + @overload + def __new__( + cls, + field: StringField[_ST_INNER, _GT_INNER] = ..., + required: Literal[True] = ..., + default: Union[List[_ST_INNER], Callable[[], List[_ST_INNER]]] = ..., verbose_name: str = ..., help_text: str = ..., null: bool = ..., - ) -> ListField[DictField[Any]]: ... + ) -> ListField[Optional[List[_ST_INNER]], List[_GT_INNER]]: ... @overload def __new__( cls, - field: Any, + field: DictField[Any], required: bool = ..., default: Optional[Union[List[Any], Callable[[], List[Any]]]] = ..., verbose_name: str = ..., help_text: str = ..., null: bool = ..., - ) -> ListField[Any]: ... - def __getitem__(self, arg: Any) -> _T: ... - def __iter__(self) -> Iterator[_T]: ... - @overload + ) -> ListField[Optional[List[Dict[Any, Any]]], Optional[List[Dict[Any, Any]]]]: ... def __set__( - self: ListField[StringField[Any, Any]], + self: ListField[_ST, _GT], instance: Any, - value: Optional[List[str]], - ) -> None: ... - @overload - def __set__( - self: ListField[DictField[Any]], instance: Any, value: List[Dict[str, Any]] + value: _ST, ) -> None: ... - @overload - def __set__(self: ListField[_T], instance: Any, value: List[_T]) -> None: ... - @overload - def __get__( - self: ListField[DynamicField], instance: Any, owner: Any - ) -> List[Any]: ... - @overload - def __get__( - self: ListField[StringField[Any, Any]], instance: Any, owner: Any - ) -> List[str]: ... - @overload - def __get__( - self: ListField[DictField[Any]], instance: Any, owner: Any - ) -> List[Dict[str, Any]]: ... + def __get__(self: ListField[_ST, _GT], instance: Any, owner: Any) -> _GT: ... class DictField(Generic[_T], ComplexBaseField): # not sure we need the init method overloads From 8d238ff43d0e7059a881033e8f8e740b3783023e Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Thu, 15 Apr 2021 09:17:30 -0400 Subject: [PATCH 2/2] embedded list field --- typings/mongoengine/fields.pyi | 43 +++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/typings/mongoengine/fields.pyi b/typings/mongoengine/fields.pyi index 53f1550..3c3ba0f 100644 --- a/typings/mongoengine/fields.pyi +++ b/typings/mongoengine/fields.pyi @@ -852,18 +852,45 @@ class DictField(Generic[_T], ComplexBaseField): ) -> Dict[str, List[str]]: ... def __getitem__(self, arg: Any) -> _T: ... -class EmbeddedDocumentListField(Generic[_T], BaseField): +class EmbeddedDocumentListField(Generic[_ST, _GT], BaseField): + @overload def __new__( cls, kind: Type[_T], - required: bool = ..., - default: Optional[Any] = ..., + required: Literal[False] = ..., help_text: str = ..., - ) -> EmbeddedDocumentListField[_T]: ... - def __getitem__(self, arg: Any) -> _T: ... - def __iter__(self) -> Iterator[_T]: ... - def __set__(self, instance: Any, value: List[_T]) -> None: ... - def __get__(self, instance: Any, owner: Any) -> List[_T]: ... + ) -> EmbeddedDocumentListField[Optional[List[_T]], Optional[List[_T]]]: ... + @overload + def __new__( + cls, + kind: Type[_T], + required: Literal[False] = ..., + default: Union[List[_ST_INNER], Callable[[], List[_ST_INNER]]] = ..., + help_text: str = ..., + ) -> EmbeddedDocumentListField[Optional[List[_T]], List[_T]]: ... + @overload + def __new__( + cls, + kind: Type[_T], + required: Literal[True] = ..., + help_text: str = ..., + ) -> EmbeddedDocumentListField[List[_T], List[_T]]: ... + @overload + def __new__( + cls, + kind: Type[_T], + required: Literal[True] = ..., + default: Union[List[_ST_INNER], Callable[[], List[_ST_INNER]]] = ..., + help_text: str = ..., + ) -> EmbeddedDocumentListField[Optional[List[_T]], List[_T]]: ... + def __set__( + self: EmbeddedDocumentListField[_ST, _GT], + instance: Any, + value: _ST, + ) -> None: ... + def __get__( + self: EmbeddedDocumentListField[_ST, _GT], instance: Any, owner: Any + ) -> _GT: ... class LazyReference(Generic[_T], BaseField): def __getitem__(self, arg: Any) -> LazyReference[_T]: ...