question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError with partial=True

See original GitHub issue

MR #18 created a new issue.

Consider this simple serializer:

class MyPolymorphicSerializer(PolymorphicSerializer):
    pass  # Mostly irrelevant

class MySerializer(Serializer):
    foo = CharField()
    bar = MyPolymorphicSerializer(required=false)

I use standard DRF viewsets to do a PATCH request (i.e. serializer has partial=True) to an endpoint using MySerializer, with JSON content: {"foo": "my_foo"}. We exclude bar, which for PATCH requests should mean that this field is virtually ignored and kept as it is before the request. This worked as expected before this MR.

Now however, it will raise this error:

  File "/usr/local/lib/python3.8/site-packages/rest_framework/mixins.py", line 67, in update
    serializer.is_valid(raise_exception=True)
  File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 234, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 433, in run_validation
    value = self.to_internal_value(data)
  File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 490, in to_internal_value
    validated_value = field.run_validation(primitive_value)
  File "/usr/local/lib/python3.8/site-packages/rest_polymorphic/serializers.py", line 95, in run_validation
    resource_type = self._get_resource_type_from_mapping(data)
  File "/usr/local/lib/python3.8/site-packages/rest_polymorphic/serializers.py", line 112, in _get_resource_type_from_mapping
    return mapping[self.resource_type_field_name]
TypeError: 'type' object is not subscriptable

This happens because normally, Serializer.run_validation does checks (validate_empty_values) to ensure there is some data to begin with. Now however, it tries to determine the resource_type before running the base implementation. When determining the resource_type, it tries to do empty['somekey'], which of course results in a TypeError.

Note that there might be other similar edge-cases I didn’t cover here.

IMO this issue is more critical than the one solved by #18 in the first place.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

10reactions
denisorehovskycommented, May 6, 2020

Hi. Thanks. I haven’t used this library for a long time and I’m not interested in doing any fixes right now. If you have some free time, you can send a PR if you want. I will take a look. Thanks

3reactions
iglosiggiocommented, May 6, 2020

I’m not working w/DRF nor django-polymorphic rigth now. Is there any reason why adding a check for empty data on my previous PR wouldn’t fix your problem?

I’ll try to come up with a fix later 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError with partial=True · Issue #26 - GitHub
I use standard DRF viewsets to do a PATCH request (i.e. serializer has partial=True ) to an endpoint using MySerializer , with JSON...
Read more >
Not raise exception on serializer with partial = true
The exception does not raise when i add partial=True, It work ok ... TypeError: parse() missing 1 required positional argument: 'stream'.
Read more >
uncaught typeerror: cannot read properties of undefined ... - You.com
In your example Uncaught TypeError: Cannot read properties of undefined (reading 'replace' ) can occur only if your variables song and/or artist are ......
Read more >
learning — Sp2Learning 1.1 documentation - PythonHosted.org
_sample = sample # else: # raise TypeError("sample should be a dictionary. ... a list of strings if partial=True; otherwise, based on self.pref...
Read more >
Django REST Framework - Serializers
URL_FIELD_NAME])} except (TypeError, KeyError): return {} ... *args, **kwargs): kwargs['partial'] = True return self.update(request, *args, **kwargs).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found