Skip to content

Fix Field.formfield(), GeometryField.formfield() methods #1778

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

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions django-stubs/contrib/gis/db/models/fields.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,13 @@ class GeometryField(BaseSpatialField[_ST, _GT]):
error_messages: _ErrorMessagesMapping | None = ...,
) -> None: ...
def formfield( # type: ignore[override]
self, *, form_class: type[GeometryField] | None = ..., geom_type: str = ..., srid: Any = ..., **kwargs: Any
) -> GeometryField: ...
self,
*,
form_class: type[forms.GeometryField] | None = ...,
geom_type: str = ...,
srid: Any = ...,
**kwargs: Any,
) -> forms.GeometryField: ...
def select_format(self, compiler: Any, sql: Any, params: Any) -> Any: ...

class PointField(GeometryField[_ST, _GT]):
Expand Down
4 changes: 2 additions & 2 deletions django-stubs/db/models/fields/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ class Field(RegisterLookupMixin, Generic[_ST, _GT]):
# TODO: plugin support
def formfield(
self,
form_class: type[Field] | None = ...,
form_class: type[forms.Field] | None = ...,
choices_form_class: type[forms.ChoiceField] | None = ...,
**kwargs: Any,
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you're feeling adventurous, it would be useful to typehint additional keys supported in kwargs, in a separate PR. Looking at the formfield() method code, seems like you can copy certain arguments from CharField constructor:

    def formfield(self, form_class=None, choices_form_class=None, **kwargs):
            ...
            for k in list(kwargs):
                if k not in (
                    "coerce",
                    "empty_value",
                    "choices",
                    "required",
                    "widget",
                    "label",
                    "initial",
                    "help_text",
                    "error_messages",
                    "show_hidden_initial",
                    "disabled",
                ):
                    del kwargs[k]
        defaults.update(kwargs)
        if form_class is None:
            form_class = forms.CharField
        return form_class(**defaults)

) -> Field: ...
) -> forms.Field: ...
def save_form_data(self, instance: Model, data: Any) -> None: ...
def contribute_to_class(self, cls: type[Model], name: str, private_only: bool = ...) -> None: ...
def to_python(self, value: Any) -> Any: ...
Expand Down