-
-
Notifications
You must be signed in to change notification settings - Fork 121
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
Monkeypatching breaks DRF filters #299
Comments
This happened to me this week on a production app for a client. We realised the problem because our Zapier integration stopped working when the REST API no longer had search and pagination support. Took me a whole day to track the problem down. ReproductionIf anyone wants to see minimal repro - https://github.com/LucidDan/drf-repro
The only difference between the two settings files is broken_settings has: Root CauseI think what is actually happening here is that importing from rest_framework in the settings.py causes django-rest-framework to fail to load any settings and just use its defaults. Unfortunately, it does this silently, so you can easily end up with things breaking without realising it. I think this is not really a djangorestframework-stubs bug. It's maybe not even really a rest-framework "bug" - more of a feature of past design choices (eg choosing to allow REST_FRAMEWORK to be optional and not throw an error if it isn't defined). WorkaroundsThe workarounds are not ideal, and involve some level of compromise:
You might also be able to get away with monkeypatching in an apps.py file or something, so that it gets monkeypatched AFTER settings has been loaded fully...but I found this approach a bit too brittle. There's too much dependent on the order stuff gets imported. For now, I'm using option 2 - it's ugly to late-import stuff at the end of settings.py, but at least its DRY-er than monkeypatching in every file I use rest-framework. Permanent FixFor fixing this properly, I have no idea how it can be done on the drf-stubs side; it seems more like a rest-framework issue, and for -stubs, it should just be documented more clearly that you need to apply monkeypatching and that there are some potential gotchas in doing so. |
I've run into this issue too. In my case, authentication classes aren't getting configured and Unfortunately, importing |
Allow Request, Response, Field, and GenericAPIView to be subscriptable. This allows the classes to be made generic for type checking. This is especially useful since monkey patching DRF can be problematic as seen in this [issue][1]. [1]: typeddjango/djangorestframework-stubs#299
I've submitted encode/django-rest-framework#8825 in hopes that it would solve this problem at the root. |
Allow Request, Response, Field, and GenericAPIView to be subscriptable. This allows the classes to be made generic for type checking. This is especially useful since monkey patching DRF can be problematic as seen in this [issue][1]. [1]: typeddjango/djangorestframework-stubs#299
Allow Request, Response, Field, and GenericAPIView to be subscriptable. This allows the classes to be made generic for type checking. This is especially useful since monkey patching DRF can be problematic as seen in this [issue][1]. [1]: typeddjango/djangorestframework-stubs#299
Allow Request, Response, Field, and GenericAPIView to be subscriptable. This allows the classes to be made generic for type checking. This is especially useful since monkey patching DRF can be problematic as seen in this [issue][1]. [1]: typeddjango/djangorestframework-stubs#299
Allow Request, Response, Field, and GenericAPIView to be subscriptable. This allows the classes to be made generic for type checking. This is especially useful since monkey patching DRF can be problematic as seen in this [issue][1]. [1]: typeddjango/djangorestframework-stubs#299
I think I've got the same problem with https://github.com/vbabiy/djangorestframework-camel-case, when I apply the monkeypatching, my Thanks for the thourough explanation above, I think it all makes sense. Unfortunately, the second option in the workarounds don't work in my case. Given that the fix in DRF is merged is hopefully going to be released soon, I'll probably wait for it. Thanks for the great work @jalaziz! |
I can't believe there are others who have the same issue! Yes, DRF loading the settings "prematurely" causes the issue. I reverted to add class_getitem to Django's Here's my code: from django.views import generic # For patching
patchable_classes = (
generic.View,
)
def patch() -> None:
"""Patch classes."""
for cls in patchable_classes:
cls.__class_getitem__ = classmethod(lambda cls, *args, **kwargs: cls) |
I ran into the same issue after monkeypatching |
Bug report
What's wrong
It appears that mokeypatching DRF viewsets (as suggested here) breaks the usage of
django_filters
FilterSets:With this code the
DocumentViewSet
completely ignores the filters defined inDocumentFilterSet
. GET params simply stop working for filtering and the filter UI is completely missing from DRF's HTML view.Refer to this test in the demo repository. In the original commit, the test passes, but after types are added, the test fails.
How is that should be
DRF filters should continue working after monkeypatching.
System information
python
version: 3.10django
version: 4.1mypy
version: 0.991django-stubs
version: 1.13.0django-stubs-ext
version: 0.7.0The text was updated successfully, but these errors were encountered: