Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

get_attribute swallows tracebacks for KeyError/AttributeError#7745

Unanswered
johtso asked this question inPotential Issue
Discussion options

get_attribute currently catchesKeyErrors andAttributeErrors when trying to retrieve fields, assuming that those errors resulted from the top level attribute getting or key getting.

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:

E AttributeError: Got AttributeError when attempting to get a value for fieldmy_prop on serializerWidgetSerializer.
E The serializer field might be named incorrectly and not match any attribute or key on theWidget instance.
E Original exception text was: 'something_entirely_unrelated' object has no attribute 'some_attribute'.

You must be logged in to vote

Replies: 12 comments

Comment options

Hmm, I think I have a possible solution.

First of all this error message logic could be moved tothe baseget_attribute function. It doesn't seem right that the error handling logic is split between theget_attribute function and the method. One catchesObjectDoesNotExist, the other catchesKeyError andAttributeError. Why not do that all in one place.

Secondly, instead of catchingKeyError andAttributeError exceptions, we could usegetitem andgetattr withdefaults and respond to the default being returned.

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?

You must be logged in to vote
0 replies
Comment options

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'})
You must be logged in to vote
0 replies
Comment options

@johtso my first thought is that is feels wrong to have aKeyError orAttributeError.
I'm going to keep this opened to dig a bit more into the issue before I make a final decision.

You must be logged in to vote
0 replies
Comment options

Just another note as I'm digging further into this: this problem only manifests forread_only=True fields. The exceptions otherwise bubble up properly.

You must be logged in to vote
0 replies
Comment options

Just got bitten by this again.. any more thoughts on this problem/solution?

You must be logged in to vote
0 replies
Comment options

just got bit. ouch.

You must be logged in to vote
0 replies
Comment options

Not obvious how to proceed with this. Thoughts/suggestions welcome.

You must be logged in to vote
0 replies
Comment options

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

You must be logged in to vote
0 replies
Comment options

Something to think about later -#5600 could be adapted to fix this.

You must be logged in to vote
0 replies
Comment options

@chidg: Good point; still appears in 2018.

You must be logged in to vote
0 replies
Comment options

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 fromserializer.data with no warning/error. Spent half a day tracing the source (thought it was something else breaking the dict comparison in my tests).

A warning log at the least would have been immensely helpful.

You must be logged in to vote
0 replies
Comment options

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
You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
None yet
10 participants
@johtso@sirpengi@tomchristie@munsu@xordoquy@rpkilby@chidg@drew-moore@AndersonSMed@cgi1
Converted from issue

This discussion was converted from issue #2884 on March 03, 2021 14:20.


[8]ページ先頭

©2009-2025 Movatter.jp