Skip to content

django_stubs_ext.monkeypatch() does not work properly #557

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

Closed
egregors opened this issue Jan 19, 2021 · 4 comments
Closed

django_stubs_ext.monkeypatch() does not work properly #557

egregors opened this issue Jan 19, 2021 · 4 comments
Labels
bug Something isn't working

Comments

@egregors
Copy link

Bug report

What's wrong

I have  django_stubs and django_stubs_ext installed.
I have called monkeypatch() in the base Django settings file.
But still, got mypy errors for an absolutely generic Django Admin code:

admin.py

# ...
@admin.register(Content)
class ContentAdmin(admin.ModelAdmin):
    # ...
    def foo(self, obj: Content) -> Foo:
        return obj.bar.foo

    foo.short_description = _('Foo short description')
    foo.admin_order_field = 'foo__bar'

mypy errors

 error: Missing type parameters for generic type "ModelAdmin"
 error: "Callable[[SectionProgressAdmin, SectionProgress], Any]" has no attribute "short_description"

How is that should be

According to this issue (#507), calling monkeypatch() should resolve this problem. But, for some reason, it wasn't.

System information

  • OS: MacOS 11.1
  • python version: 3.9
  • django version: 3.1.5
  • mypy version: 0.790
  • django-stubs version: 1.7.0
  • django_stubs_ext version: 0.1.0
@sobolevn
Copy link
Member

Try class ContentAdmin(admin.ModelAdmin[Content]):, this error error: Missing type parameters for generic type "ModelAdmin" will go away.

@egregors
Copy link
Author

egregors commented Jan 19, 2021

Thanks a lot! 🔥 🔥 🔥 It solved Missing type parameters for generic type "ModelAdmin" issue.

Does it exist any known way to resolve has no attribute "short_description", has no attribute "admin_order_field" errors?

Looks like this workaround is the only way: python/mypy#2087 (comment)

@sobolevn
Copy link
Member

I use # type: ignore as my go-to workaround. Because that's the easiest way.

I would love to get more typing support here, because sometimes I make typos like sohrt_description and it just silently ignores it.

I am going to close this ticket. Feel free to reopen it or open new ones! 👍

@Milutinke92
Copy link

Thanks a lot! 🔥 🔥 🔥 It solved Missing type parameters for generic type "ModelAdmin" issue.

Does it exist any known way to resolve has no attribute "short_description", has no attribute "admin_order_field" errors?

Looks like this workaround is the only way: python/mypy#2087 (comment)

When I do this, I get TypeError: type 'ModelAdmin' is not subscriptable. Whats wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

3 participants