From e8d52fe7daf88cfe7db5d0dd312e1b5946577051 Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Tue, 5 Jul 2022 23:19:51 +0100 Subject: [PATCH] Monkeypatch some more generics. (#1050) * Add generic monkeypatching for `FileProxyMixin`. This fixes `TypeError: 'type' object is not subscriptable` for `django.core.files.File` and `django.core.files.base.ContentFile`. * Add generic monkeypatching for `ForeignKey`. This matches the change coming in Django 4.1. See https://github.com/django/django/pull/15571 --- django_stubs_ext/django_stubs_ext/patch.py | 5 +++++ django_stubs_ext/tests/test_monkeypatching.py | 2 ++ 2 files changed, 7 insertions(+) diff --git a/django_stubs_ext/django_stubs_ext/patch.py b/django_stubs_ext/django_stubs_ext/patch.py index 1c299f9..03bf7f2 100644 --- a/django_stubs_ext/django_stubs_ext/patch.py +++ b/django_stubs_ext/django_stubs_ext/patch.py @@ -4,8 +4,10 @@ from django import VERSION as VERSION from django.contrib.admin import ModelAdmin from django.contrib.admin.options import BaseModelAdmin from django.contrib.syndication.views import Feed +from django.core.files.utils import FileProxyMixin from django.core.paginator import Paginator from django.db.models.fields import Field +from django.db.models.fields.related import ForeignKey from django.db.models.manager import BaseManager from django.db.models.query import QuerySet from django.forms.formsets import BaseFormSet @@ -54,9 +56,12 @@ _need_generic: List[MPGeneric[Any]] = [ MPGeneric(BaseModelForm), MPGeneric(BaseModelFormSet), MPGeneric(Feed), + MPGeneric(FileProxyMixin), # These types do have native `__class_getitem__` method since django 3.1: MPGeneric(QuerySet, (3, 1)), MPGeneric(BaseManager, (3, 1)), + # These types do have native `__class_getitem__` method since django 4.1: + MPGeneric(ForeignKey, (4, 1)), ] diff --git a/django_stubs_ext/tests/test_monkeypatching.py b/django_stubs_ext/tests/test_monkeypatching.py index c6f9bf9..fa668fc 100644 --- a/django_stubs_ext/tests/test_monkeypatching.py +++ b/django_stubs_ext/tests/test_monkeypatching.py @@ -82,6 +82,8 @@ def test_patched_extra_classes_generics(make_generic_classes: _MakeGenericClasse (3, 0), (3, 1), (3, 2), + (4, 0), + (4, 1), ], ) def test_patched_version_specific(