Open treyhunner opened 5 years ago
Thanks, @treyhunner! Do you want to give this a try?
@sobolevn I don't. I likely won't be using this package soon (I'm not using type hints yet, but have been eyeing them).
I'm interested in this project though and thought I'd run it against my project and report some issues for others to take up if interested. 😊
Sure! We need users' feedback to improve. 👍
@sobolevn I'd be happy to give this a try if/when it's possible!
Do you think it's currently blocked by https://github.com/python/mypy/issues/2087 ?
I hit the same result from mypy, trying to add typing the Django tutorial code.
@sobolevn, @YPCrumble, I'd be glad to help out improving this, but I'm quite green when it comes to mypy. Any suggestions on where I should start looking ?
The workaround suggested in the thread referenced by @YPCrumble worked for me. It is quite verbose, requiring a Protocol class, a decorator and usage of said decorator. Is there a way this can be simplified or generalized ?
https://github.com/python/mypy/issues/2087#issuecomment-462726600
We can just make these actions to return predefined Protocol
with all properties that are possible to be set.
Like so:
class ActionProtocol(Protocol):
def __call__(modeladmin: admin.ModelAdmin, request: HttpRequest, queryset: QuerySet) -> None:
...
short_description: str
boolean: bool
...
Another possible approach here, I think, is to support the action and display decorators added in Django 3.2, which reduce the need for setting attributes on functions.
Edit: I see support got added in commit a14f49c4b264 (but has not been release yet). Excellent! Conceivably this feature request is still worth implementing, but anybody running across this issue before it's fixed is probably well-served by just using action
and display
decorators.
Mypy: 1.6.1
Django-stubs: 4.2.3
Still face the same issue
"Callable[[AssetAllocationAdmin, Any], Any]" has no attribute "short_description"
Error triggers by this code
def currency_symbol(self, obj):
pass
currency_symbol.short_description = "Currency"
@Oleksii-Kutsenko See the above comment but using django display
decorator is the prefered way to declare short description. It will also solve your typing issue.
You might be interested by django-upgrade which provides an autofix for that.
@UnknownPlatypus Thank you, I appreciate your help
I'm trying this package out and decided to request a feature based on a number of errors I'm seeing on my own project.
This is a feature request to allow Django model admin classes to understand the significance of various attributes which can be added to their methods.
I customize my change list fields in my model admins often. I'm seeing lots of errors like this in my
admin.py
because of these customizations:It seems like a custom type could be made for methods which will be read by Django admin's
list_display
.