Skip to content

Improve Field.formfield(), GeometryField.formfield() methods #1724

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 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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: 3 additions & 1 deletion django-stubs/contrib/gis/db/models/fields.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ class GeometryField(BaseSpatialField[_ST, _GT]):
validators: Iterable[_ValidatorCallable] = ...,
error_messages: _ErrorMessagesMapping | None = ...,
) -> None: ...
def formfield(self, **kwargs: Any) -> Any: ... # type: ignore[override]
def formfield( # type: ignore[override]
self, form_class: type[forms.GeometryField] | None = ..., geom_type: str = ..., srid: Any = ..., **kwargs: Any
Copy link
Collaborator

@intgr intgr Oct 18, 2023

Choose a reason for hiding this comment

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

Since these are handled from **kwargs, you should typehint them as keyword-only parameters. That should get rid of the allowlist ignores too.

Suggested change
self, form_class: type[forms.GeometryField] | None = ..., geom_type: str = ..., srid: Any = ..., **kwargs: Any
self, *, form_class: type[GeometryField] | None = ..., geom_type: str = ..., srid: Any = ..., **kwargs: Any

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@intgr Technically, these are not keyword-only arguments in the upstream Field.formfield method: https://github.com/django/django/blob/stable/4.2.x/django/db/models/fields/__init__.py#L1037

However, since the subclasses (CharField, etc.) only accept keyword arguments and we don't want to reimplement this for every subclass, I think it's fine to be slightly more strict than Django might actually allow. 👍

It'll probably be safer for users to be required to use keyword arguments too.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The * keyword-only change only affects the GeometryField method, not Field. So it should be accurate AFAICT. (https://github.com/django/django/blob/stable/4.2.x/django/contrib/gis/db/models/fields.py#L303)

) -> forms.GeometryField: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

No need to qualify these types, they are defined in this very file.

Suggested change
self, form_class: type[forms.GeometryField] | None = ..., geom_type: str = ..., srid: Any = ..., **kwargs: Any
) -> forms.GeometryField: ...
self, form_class: type[GeometryField] | None = ..., geom_type: str = ..., srid: Any = ..., **kwargs: Any
) -> GeometryField: ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@intgr Likewise, these are not the same types.

forms.GeometryField is a form field. The local GeometryField is a model field.

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 @@ -206,9 +206,9 @@ class Field(RegisterLookupMixin, Generic[_ST, _GT]):
def formfield(
self,
form_class: type[forms.Field] | None = ...,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
form_class: type[forms.Field] | None = ...,
form_class: type[Field] | None = ...,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@intgr No, these are different fields!

forms.Field is a form field. The local Field is a model field.

This change now breaks the annotation correctness. I'll open a supplemental PR to fix it now.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oops, sorry for messing that up. And thanks for responding so quickly.

Sadly this screw-up is now part of the django-stubs 4.2.5 release. I'll probably wait a few days to see if any other bugs turn up, and make another hotfix release.

choices_form_class: Any | None = ...,
choices_form_class: type[forms.ChoiceField] | None = ...,
**kwargs: Any,
) -> 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
4 changes: 4 additions & 0 deletions scripts/stubtest/allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ django.contrib.auth.migrations.*
django.contrib.flatpages.migrations.*
django.contrib.contenttypes.migrations.*

# Annotate optional parameters which Django accepts as **kwargs
django.contrib.gis.db.models.GeometryField.formfield
django.contrib.gis.db.models.fields.GeometryField.formfield

# default_storage is actually an instance of DefaultStorage, but it proxies through to a Storage
django.core.files.storage.default_storage

Expand Down