django-stubs icon indicating copy to clipboard operation
django-stubs copied to clipboard

Admin change list callable attributes not recognized

Open treyhunner opened this issue 6 years ago • 11 comments

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:

users/admin.py:242:5: error: "Callable[[Any, Any], Any]" has no attribute "admin_order_field"
users/admin.py:243:5: error: "Callable[[Any, Any], Any]" has no attribute "short_description"
users/admin.py:244:5: error: "Callable[[Any, Any], Any]" has no attribute "linked_field"
users/admin.py:266:5: error: "Callable[[Any, Any], Any]" has no attribute "boolean"

It seems like a custom type could be made for methods which will be read by Django admin's list_display.

treyhunner avatar Sep 03 '19 23:09 treyhunner

Thanks, @treyhunner! Do you want to give this a try?

sobolevn avatar Sep 04 '19 08:09 sobolevn

@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. 😊

treyhunner avatar Sep 04 '19 16:09 treyhunner

Sure! We need users' feedback to improve. 👍

sobolevn avatar Sep 04 '19 17:09 sobolevn

@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 ?

YPCrumble avatar Jan 31 '20 16:01 YPCrumble

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 ?

torbjorntorbjorn avatar Feb 21 '20 23:02 torbjorntorbjorn

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

torbjorntorbjorn avatar Feb 21 '20 23:02 torbjorntorbjorn

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
    ...

sobolevn avatar Feb 22 '20 05:02 sobolevn

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.

dehnert avatar Jun 13 '21 16:06 dehnert

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 avatar Nov 04 '23 13:11 Oleksii-Kutsenko

@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 avatar Nov 04 '23 15:11 UnknownPlatypus

@UnknownPlatypus Thank you, I appreciate your help

Oleksii-Kutsenko avatar Nov 05 '23 12:11 Oleksii-Kutsenko