Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions django-stubs/contrib/postgres/forms/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .array import *
from .hstore import *
from .jsonb import *
from .ranges import *
41 changes: 41 additions & 0 deletions django-stubs/contrib/postgres/forms/array.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from typing import Any, Iterable, List, Optional, Type, Union

from django import forms as forms

class SimpleArrayField(forms.CharField):
base_field: forms.Field = ...
delimiter: str = ...
min_length: Optional[Union[int, str]] = ...
max_length: Optional[Union[int, str]] = ...
def __init__(
self,
base_field: forms.Field,
*,
delimiter: str = ...,
max_length: Optional[Any] = ...,
min_length: Optional[Any] = ...,
**kwargs: Any
) -> None: ...
def to_python(self, value: Optional[Iterable]) -> List: ...
def validate(self, value: Optional[Iterable]) -> None: ...
def run_validators(self, value: Optional[Iterable]) -> None: ...

class SplitArrayWidget(forms.Widget):
template_name: str = ...
widget: forms.Widget = ...
size: int = ...
def __init__(self, widget: Union[forms.Widget, Type[forms.Widget]], size: int, **kwargs: Any) -> None: ...
def value_from_datadict(self, data: Dict[str, Any], files: Mapping[str, Iterable[Any]], name: str) -> List: ...
def get_context(self, name: Any, value: Any, attrs: Optional[Any] = ...): ...
@property
def needs_multipart_form(self) -> bool: ...

class SplitArrayField(forms.Field):
base_field: forms.Field = ...
size: int = ...
remove_trailing_nulls: bool = ...
def __init__(
self, base_field: forms.Field, size: int, *, remove_trailing_nulls: bool = ..., **kwargs: Any
) -> None: ...
def to_python(self, value: Optional[Iterable]) -> List: ...
def clean(self, value: Any) -> List: ...
3 changes: 3 additions & 0 deletions django-stubs/contrib/postgres/forms/hstore.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django import forms

class HStoreField(forms.CharField): ...
9 changes: 9 additions & 0 deletions django-stubs/contrib/postgres/forms/jsonb.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django import forms
from typing import Any, Type, Union

class InvalidJSONInput(str): ...
class JSONString(str): ...

class JSONField(forms.CharField):
widget: Union[forms.Widget, Type[forms.Widget]] = ...
Copy link
Member

@mkurnikov mkurnikov Jan 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as in the comment about base_field

Copy link
Contributor Author

@asfaltboy asfaltboy Jan 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Django is weird ... the field classes expect widget to define a static attribute specifying the widget class, but then instantiate and set the self.widget to the instance in the constuctor 🤦‍♂ ... so if I pull the above line to forms.Field I start seeing errors like this:

django-sources/tests/postgres_tests/test_ranges.py:723: error: Argument 1 to "render" of "Widget" has incompatible type "str"; expected "Widget"
django-sources/tests/postgres_tests/test_ranges.py:723: error: Too few arguments for "render" of "Widget"
django-sources/tests/postgres_tests/test_ranges.py:727: error: Argument 1 to "render" of "Widget" has incompatible type "str"; expected "Widget"
django-sources/tests/postgres_tests/test_ranges.py:727: error: Argument 2 to "render" of "Widget" has incompatible type "None"; expected "str"

I could only resolve this by pulling defining this in child class:

class BaseRangeField(forms.MultiValueField):
    widget: Widget

In that case, I guess it's better to keep the Union on the parent, and add the Argument 1 to "render" of "Widget" has incompatible type "str" error under: enabled_test_modules.py?

def prepare_value(self, value: Any) -> str: ...
25 changes: 25 additions & 0 deletions django-stubs/contrib/postgres/forms/ranges.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django import forms
from django.forms.widgets import MultiWidget
from typing import Any, Callable, Optional, Type, Union

_OptAttrs = Dict[str, Any]

class BaseRangeField(forms.MultiValueField):
def __init__(self, **kwargs: Any) -> None: ...
def compress(self, values: Any) -> Tuple[Optional[Callable], Optional[Callable]]: ...

class IntegerRangeField(BaseRangeField):
base_field: Union[Type[forms.Field], forms.Field] = ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

base_field defined on forms.Field, I think. If it may be both Type[Field] and Field, better fix it there, and remove all this duplication.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in 67d664d


class FloatRangeField(BaseRangeField):
base_field: Union[Type[forms.Field], forms.Field] = ...

class DateTimeRangeField(BaseRangeField):
base_field: Union[Type[forms.Field], forms.Field] = ...

class DateRangeField(BaseRangeField):
base_field: Union[Type[forms.Field], forms.Field] = ...

class RangeWidget(MultiWidget):
def __init__(self, base_widget: forms.Widget, attrs: Optional[_OptAttrs] = ...) -> None: ...
def decompress(self, value: Any) -> List: ...
5 changes: 5 additions & 0 deletions django-stubs/contrib/postgres/utils.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from typing import Mapping

from django.core.exceptions import ValidationError

def prefix_validation_error(error: ValidationError, prefix: str, code: str, params: Mapping) -> ValidationError: ...