-
-
Notifications
You must be signed in to change notification settings - Fork 480
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||||
) -> forms.GeometryField: ... | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @intgr Likewise, these are not the same types.
intgr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
def select_format(self, compiler: Any, sql: Any, params: Any) -> Any: ... | ||||||||||
|
||||||||||
class PointField(GeometryField[_ST, _GT]): | ||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -206,9 +206,9 @@ class Field(RegisterLookupMixin, Generic[_ST, _GT]): | |||||
def formfield( | ||||||
self, | ||||||
form_class: type[forms.Field] | None = ..., | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @intgr No, these are different fields!
This change now breaks the annotation correctness. I'll open a supplemental PR to fix it now. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: ... | ||||||
intgr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
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: ... | ||||||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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#L1037However, 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.
There was a problem hiding this comment.
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 theGeometryField
method, notField
. So it should be accurate AFAICT. (https://github.com/django/django/blob/stable/4.2.x/django/contrib/gis/db/models/fields.py#L303)