Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7k
get_attribute swallows tracebacks for KeyError/AttributeError#7745
-
The problem with this is that ifanything else inside a property causes one of those errors, the traceback is swallowed and it's a nightmare to debug! Is there maybe a way to make sure that the exception doesn't come from deeper down? Contrived illustration:
|
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 12 comments
-
Hmm, I think I have a possible solution. First of all this error message logic could be moved tothe base Secondly, instead of catching Then we can be precise about the issue we're trying to catch, and avoid confusing a exception thrown by code triggered by thegetattr orgetitem / property call, for an error retrieving the key/attr itself. How does that sound? |
BetaWas this translation helpful?Give feedback.
All reactions
-
I'm having this issue as well, just here to add some more background. This is somewhat related to#2598, but instead of callables raising an attribute exception this is on property fields. Here's a reproduction: """requirements.txt:Django==1.8.2djangorestframework==3.1.3"""fromdjango.confimportsettingsSETTINGS=dict(DATABASES= {},DEBUG=True,TEMPLATE_DEBUG=True,ROOT_URLCONF=None,)settings.configure(**SETTINGS)fromrest_frameworkimportserializersclassExampleSerializer(serializers.Serializer):example_property=serializers.CharField(read_only=True)example_fail_property=serializers.CharField(read_only=True)classExampleInstance(object):@propertydefexample_property(self):return"foobar"@propertydefexample_fail_property(self):raiseAttributeError('property failed')serializer=ExampleSerializer(ExampleInstance())print(serializer.data.items())# ItemsView({'example_property': 'foobar'}) |
BetaWas this translation helpful?Give feedback.
All reactions
-
@johtso my first thought is that is feels wrong to have a |
BetaWas this translation helpful?Give feedback.
All reactions
-
Just another note as I'm digging further into this: this problem only manifests for |
BetaWas this translation helpful?Give feedback.
All reactions
-
Just got bitten by this again.. any more thoughts on this problem/solution? |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
just got bit. ouch. |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Not obvious how to proceed with this. Thoughts/suggestions welcome. |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Just for any googlers arriving here trying to solve a KeyError on get_attribute, it may be due to accessing serializer.data in a situation where validation has already been done. In that case you should be using serializer.validated_data. http://stackoverflow.com/questions/32236958/keyerror-on-relation-field-in-django-rest |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
-
Something to think about later -#5600 could be adapted to fix this. |
BetaWas this translation helpful?Give feedback.
All reactions
-
@chidg: Good point; still appears in 2018. |
BetaWas this translation helpful?Give feedback.
All reactions
-
Was caught by this. Some other package I rely on for a property had a non-obvious breaking change. My serializer tests failed instantly -- just dropped the field from A warning log at the least would have been immensely helpful. |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Just found out about this discussion and the very same thing still happens today. I also created a simple code snippet that illustrates this behavior: fromrest_frameworkimportserializersclassExampleSerializer(serializers.Serializer):title=serializers.CharField(read_only=True)classExampleObject:@propertydeftitle(self):returnself.fooobj=ExampleObject()serializer=ExampleSerializer()# Doesn't trigger any errorserializer.to_representation(obj)# Triggers an AttributeErrorobj.title |
BetaWas this translation helpful?Give feedback.
All reactions
This discussion was converted from issue #2884 on March 03, 2021 14:20.