-
-
Notifications
You must be signed in to change notification settings - Fork 459
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
Improve ModelAdmin and decorator hints and attributes, add tests #1292
Conversation
_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 |
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.
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 |
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.
It s unfortunate that we have replaceModelAdmin
with Any
here. But hopefully in the future it's possible to use Self
.
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.
Looks good, thanks!
Some follow-up additions on top of #1267:
@display
decorator now supports usage forModelAdmin
methods as well._StrPromise
to_StrOrPromise
where plainstr
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 classModelAdmin
)