-
-
Notifications
You must be signed in to change notification settings - Fork 485
Generic Type Error with ModelAdmin when running in strict mode #507
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
Comments
We have to patch Django from time to time to fix this 🙂 |
@sobolevn just so I understand, you mean submit a patch to django directly for this? Presumably having these classes inherit from |
Nope, we submit patches to https://github.com/django/django Example: django/django@578c03b |
Oh awesome, thanks so much. I'll see if I can get a patch in 😄 |
Thanks for creating this ticket and for your work on https://code.djangoproject.com/ticket/32156 , @bradykieffer ! It looks like the Django devs decided not to accept your proposed patch; that's a bummer, but I see where they're coming from. @sobolevn , what do you think should be done next? Is reverting #504 on the table, or is a better option available? (I'm a drive-by commenter with no insight into that PR or the issue it sought to fix; all I know is that the codebase I work on has ~35 instances of this new |
Well, I agree with Carlton here. Because, it's a really bad idea to ask Django's devs to add I think we should go with monkeypatching stuff on our own end. For example, we can add import django_stubs
django_stubs.make_generics() in This way we would hide all the boilerplate away. And won't be dependent on django's implementation. |
@jrheard thanks for the update - it's been a hectic week on my end so I haven't been able to update this. I'm in the same boat as you where we're grappling with how to address this, it would be really nice if django stubs were compatible with supported Django versions for us as well. The approaches that I can see being immediately available are:
Not that it's directly related to this but I also had a similar proposal for Edit: @sobolevn literally posted long before my above rant, glad we agree |
oh jeez, i didn't even think about the fact that making ModelAdmin generic would actually fail to run. i hate that there's no clean answer here, but i think monkeypatching is absolutely the way to go |
hey @bradykieffer the fix for this just got merged. we ended up going with a sub-project as part of the main repo. you'll have to install it in addition to django stubs with then somewhere in your top-level urlconf add the following: import django_stubs_ext.monkeypatch
django_stubs_ext.monkeypatch() |
Bug report
What's wrong
I might be crazy here, but when I subclass from
admin.ModelAdmin
and run mypy with--strict
enabled I get an error,error: Missing type parameters for generic type "ModelAdmin"
. It's completely non-obvious to me how to exactly resolve this. The following code will fail on my machine:This code:
Generates a type error:
TypeError: 'MediaDefiningClass' object is not subscriptable
The only way I've been able to resolve this is to do the following:
But that feels like a pretty annoying workaround for all of our admin pages
How is that should be
I believe I should be able to pass in
db.Foo
when annotating the type forFooAdmin
like so:class FooAdmin(admin.ModelAdmin[db.Foo])
although I'm not sure on exactly how to go about this. Any guidance would be much appreciated hereSystem information
python
version: 3.8.6django
version: 2.2.16mypy
version: 0.70django-stubs
version: 1.7.0The text was updated successfully, but these errors were encountered: