Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: nullable ListField support for ListField and inner field #22

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 70 additions & 42 deletions typings/mongoengine/fields.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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[DictField[Any]]: ...
) -> ListField[Optional[List[_ST_INNER]], List[_GT_INNER]]: ...
@overload
def __new__(
cls,
field: Any,
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[Optional[List[_ST_INNER]], List[_GT_INNER]]: ...
@overload
def __new__(
cls,
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
Expand Down Expand Up @@ -851,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]: ...
Expand Down