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

Allow None return form model Field.formfield() #2363

Merged
merged 1 commit into from
Sep 9, 2024
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
8 changes: 5 additions & 3 deletions django-stubs/contrib/admin/options.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ class BaseModelAdmin(Generic[_ModelT]):
admin_site: AdminSite
def __init__(self) -> None: ...
def check(self, **kwargs: Any) -> list[CheckMessage]: ...
def formfield_for_dbfield(self, db_field: Field, request: HttpRequest, **kwargs: Any) -> FormField: ...
def formfield_for_choice_field(self, db_field: Field, request: HttpRequest, **kwargs: Any) -> TypedChoiceField: ...
def formfield_for_dbfield(self, db_field: Field, request: HttpRequest, **kwargs: Any) -> FormField | None: ...
def formfield_for_choice_field(
self, db_field: Field, request: HttpRequest, **kwargs: Any
) -> TypedChoiceField | None: ...
def get_field_queryset(self, db: str | None, db_field: RelatedField, request: HttpRequest) -> QuerySet | None: ...
def formfield_for_foreignkey(
self, db_field: ForeignKey, request: HttpRequest, **kwargs: Any
) -> ModelChoiceField: ...
) -> ModelChoiceField | None: ...
def formfield_for_manytomany(
self, db_field: ManyToManyField, request: HttpRequest, **kwargs: Any
) -> ModelMultipleChoiceField | None: ...
Expand Down
2 changes: 1 addition & 1 deletion django-stubs/db/models/fields/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class Field(RegisterLookupMixin, Generic[_ST, _GT]):
form_class: type[forms.Field] | None = None,
choices_form_class: type[forms.ChoiceField] | None = None,
**kwargs: Any,
) -> forms.Field: ...
) -> forms.Field | None: ...

Choose a reason for hiding this comment

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

hmmm so this is triggering a bunch of false positives after upgrading django-stubs

it seems to me that django.db.models.fields.Field.formfield always returns a Field so this annotation is wrong (?) and subclasses should override it to be | None if applicable (?) (even though it would violate substitutability -- but such is django)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did you read the associated ticket and docs change? Any Field subclass may return None from formfield() to remove it from forms.

In Django itself, AutoFieldMixin and OneToOneField use this feature. I have used this feature at least once in my MySQL package: https://github.com/adamchainz/django-mysql/blob/1ba0471c06bf7e3b47a1ee2c544fc3738020611e/src/django_mysql/models/fields/dynamic.py#L295-L299

Adding the None option the base class fixes substitutability, no?

Choose a reason for hiding this comment

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

yes but fields.Field does not return None, and my subclass of fields.Field inherits the same implementation which does not return None -- and now the type checker is mad about that

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure.. I'm happy adding assert formfield is not None to such tests. That's just type-hinted life, in my experience.

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