-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Serializer does not respect Python's C3 MRO #7735
Comments
When I was working through this, my expected behaviour was So I'd suggest it's a usage to be avoided, given that it's unclear which behaviour you'd expect to see. |
Thanks for sharing your thoughts! I respect your idea on this.
Are you open to having this usage documented as a pattern to be avoided? If this is the case, I'd be happy to make a pull request on it. Perhaps it might be a good idea to also detect and explicitly warn the user about this at runtime.
IMHO some folks used to how Python resolves multiple base classes may find the expected behavior stated above more "intuitive." For anyone not familiar with MRO, suppose the from rest_framework import serializers
# dummy fields for demonstration
class AllProductsField(serializers.Field): pass
class SupportedProductsField(serializers.Field): pass
class CommonSerializer:
product = AllProductsField()
class CustomerAccess(CommonSerializer):
def validate_product(self, value):
return do_something(value)
class TicketSerializer(CommonSerializer):
product = SupportedProductsField()
class CustomerTicketSerializer(CustomerAccess, TicketSerializer):
pass
print('product =', CustomerTicketSerializer().product) This code would produce Although this case may be solved by omitting the base class from the |
Personally I think this kind of usage invites confusion either ways around, and is best avoided. |
Do you think if this (anti-)pattern is worth being documented?
|
Not particularly, no. I think it's just one of those things that comes with the territory. |
Checklist
master
branch of Django REST framework.Steps to reproduce
Expected behavior
Actual behavior
Remarks
For compatibility I think this behavior shall be kept for a while, but not respecting Python MRO may turn out to be astonishing for some users and even lead to potential security problems (e.g. choice-limiting queryset not properly overridden).
The text was updated successfully, but these errors were encountered: