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

Improve ModelAdmin and decorator hints and attributes, add tests #1292

Merged
merged 5 commits into from Dec 15, 2022

Conversation

intgr
Copy link
Collaborator

@intgr intgr commented Dec 13, 2022

Some follow-up additions on top of #1267:

  • Added tests for admin decorators.
  • The @display decorator now supports usage for ModelAdmin methods as well.
  • Changed _StrPromise to _StrOrPromise where plain str should be supported as well.
  • ModelAdmin.display didn't previously allow functions returning booleans.
  • ModelAdmin.actions didn't previously allow having accurate type hints of the passed function (only allowed base class ModelAdmin)

_Model = TypeVar("_Model", bound=Model)
_ModelAdmin = TypeVar("_ModelAdmin", bound=ModelAdmin)
_Request = TypeVar("_Request", bound=HttpRequest)
_QuerySet = TypeVar("_QuerySet", bound=QuerySet)
# This is deliberately different from _DisplayT defined in contrib.admin.options
_DisplayCallable: TypeAlias = Union[Callable[[_ModelAdmin, _Model], Any], Callable[[_Model], Any]] # noqa: Y037
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Need to suppress Y037. Can't use the pipe syntax here for some reason, otherwise mypy-self-check (3.9) fails with:

django-stubs/contrib/admin/decorators.pyi:18: error: Unsupported left operand type for | ("object")

@@ -154,7 +156,7 @@ class ModelAdmin(BaseModelAdmin[_ModelT]):
delete_selected_confirmation_template: _TemplateForResponseT | None
object_history_template: _TemplateForResponseT | None
popup_response_template: _TemplateForResponseT | None
actions: Sequence[Callable[[ModelAdmin, HttpRequest, QuerySet], None] | str] | None
actions: Sequence[_ActionCallable[Any, _ModelT] | str] | None
Copy link
Collaborator Author

@intgr intgr Dec 14, 2022

Choose a reason for hiding this comment

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

It s unfortunate that we have replaceModelAdmin with Any here. But hopefully in the future it's possible to use Self.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@intgr intgr changed the title Improve ModelAdmin hints and decorators, add tests Improve ModelAdmin and decorator hints and, add tests Dec 15, 2022
@intgr intgr merged commit b0edae3 into typeddjango:master Dec 15, 2022
@intgr intgr changed the title Improve ModelAdmin and decorator hints and, add tests Improve ModelAdmin and decorator hints and attributes, add tests Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants