[issue25634] Add a dedicated subclass for attribute missing errors

Jun Wang report at bugs.python.org
Thu Nov 19 14:47:33 EST 2015


Jun Wang added the comment:

I think this is a common problem while using both __getattr__ and descriptor/property. A descriptor example:

class Descriptor(): 
	def __get__(self, instance, owner=None): 
		raise AttributeError('Implicitly suppressed')

class A(): 
	d = Descriptor()
	def __getattr__(self, name): 
		return 'default'

print(A().d)


Without descriptor, unexpected AttributeError could only come from overriding __getattribute__, which is a rare case, although still an imperfection. But in descriptor/property, AttributeError which is too general just occurs frequently like in normal method. 

Surely any modification would break the backward compatibility, although I wonder how often it is used of raising AttributeError purposely, maybe in __getattribute__, to call __getattr__, instead of explicitly calling __getattr__. In my understanding this is the only case that will be affected.


"An unexpected exception should not result in subtly altered behaviour, but should cause a noisy and easily-debugged traceback. "—from PEP479

About the implementation, maybe something like "RuntimeError: descriptor raised AttributeError" simulating PEP479. Or in my lay opinion, the best solution is: add object.__getattr__, with the only behavior of raising AttributeError; when normal attribute lookup fails, object.__getattribute__ calls __getattr__ explicitly; __getattr__ not triggered by AttributeError anymore.

I know little about the CPython implementation, so I might be completely wrong. However this seems deserving more detailed discussion.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25634>
_______________________________________


More information about the Python-bugs-list mailing list