This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Martin.Thurau
Recipients Martin.Thurau
Date 2014-03-07.12:46:11
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1394196372.69.0.347625630871.issue20864@psf.upfronthosting.co.za>
In-reply-to
Content
If you have a descriptor (in my case it was an SQLAlchemy column) on an instance and this descriptor returns None for a call to __get__ then getattr with a given default value, will not return the default, but None.

I have no knowledge on the implementation details of getattr but I guess the logic is something like this:
- getattr looks at the given object and sees that the attribute in question is not None (since it is the descriptor object)
- getattr returns the descriptor
- the descriptors __get__ method is called
- __get__ return None

Maybe it should be more like this:
- getattr looks at the given object and sees that the attribute in question is not None (since it is the descriptor object)
- getattr sees that the attribute has __get__
- getattr calls __get__ method and looks if the return value is None

I'm not sure if this is really a bug but it's highly confusing and somewhat un-pythonic. I really should not care of an attribute of an object is a value or a descriptor. This is especially true since this problem also applies to @property. Effectively this means that if you call getattr you have *know* if the name in question is a property or not and one can't simply swap out an objects value for a property without risking to break calling code.

If this is actually *not* a bug, we should at least update the documentation to getattr, to mention this fact. Because currently it states that "getattr(x, 'foobar') is equivalent to x.foobar" which is obviously not true.
History
Date User Action Args
2014-03-07 12:46:12Martin.Thurausetrecipients: + Martin.Thurau
2014-03-07 12:46:12Martin.Thurausetmessageid: <1394196372.69.0.347625630871.issue20864@psf.upfronthosting.co.za>
2014-03-07 12:46:12Martin.Thuraulinkissue20864 messages
2014-03-07 12:46:12Martin.Thuraucreate