typeddjango / django-stubs

PEP-484 stubs for Django
MIT License
1.61k stars 451 forks source link

Admin change list callable attributes not recognized #151

Open treyhunner opened 5 years ago

treyhunner commented 5 years ago

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.

sobolevn commented 5 years ago

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

treyhunner commented 5 years ago

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

sobolevn commented 5 years ago

Sure! We need users' feedback to improve. 👍

YPCrumble commented 4 years ago

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

torbjorntorbjorn commented 4 years ago

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 commented 4 years ago

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

sobolevn commented 4 years ago

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
    ...
dehnert commented 3 years ago

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.

Oleksii-Kutsenko commented 1 year ago

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"
UnknownPlatypus commented 1 year ago

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

Oleksii-Kutsenko commented 1 year ago

@UnknownPlatypus Thank you, I appreciate your help