You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
AttributeError should report the same details when raised by lookup_special() as when raised in the REPL #56231
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/serhiy-storchaka'closed_at=<Date2021-06-29.08:29:06.821>created_at=<Date2011-05-06.20:07:21.538>labels= ['interpreter-core', 'type-bug', '3.11']
title='AttributeError should report the same details when raised by lookup_special() as when raised in the REPL'updated_at=<Date2021-06-29.08:29:06.820>user='https://github.com/dholth'
Does not work in Python 2.7.1 on Ubuntu 11.04 or RHEL5? Frustratingly, the exception is no more specific than an AttributeError, even though hasattr(transaction, '__exit__')?
I would prefer to never get AttributeError: transaction.__exit__ when hasattr(transaction, '__exit__') as I find that to be very confusing. Maybe the interpreter could raise SpecialAttributeError('transaction.__exit__ is not sufficiently special') instead.
changed the title [-]'transaction' module-as-context-manager thwarted by Python 2.7.1[/-][+]improve special method lookup error message[/+]on May 7, 2011
Python should explain AttributeError in the same way when it's raised by the interpreter. The with: statement below should raise the second AttributeError, not the first.
changed the title [-]improve special method lookup error message[/-][+]AttributeError should report the same details when raised by lookup_special() as when raised in the REPL[/+]on May 7, 2011
http://docs.python.org/dev/reference/datamodel#special-method-names explains that magic methods are looked up on the class, not on the instances. There’s a lot of code out there that erroneously checks for __len__ or __call__ on instances, and this is the second time to my knowledge that a project abused a module-level __enter__ function.
Would not be better to change an AttributeError to TypeError? Seems this is the only place in the core when missing special method causes an AttributeError and not TypeError.
Activity
dholth commentedon May 6, 2011
"How much do we care about special method lookup?" Python recently bypasses __getattr__ entirely when looking up context managers. http://mail.python.org/pipermail/python-dev/2009-May/089535.html
Could this be the reason that ZODB's transaction module, which attempts to be a context manager by declaring
Does not work in Python 2.7.1 on Ubuntu 11.04 or RHEL5? Frustratingly, the exception is no more specific than an AttributeError, even though hasattr(transaction, '__exit__')?
I would prefer to never get AttributeError: transaction.__exit__ when hasattr(transaction, '__exit__') as I find that to be very confusing. Maybe the interpreter could raise SpecialAttributeError('transaction.__exit__ is not sufficiently special') instead.
http://svn.zope.org/repos/main/transaction/trunk/transaction/__init__.py
benjaminp commentedon May 6, 2011
Yes, that's why. I suggest you appeal to python-ideas about the new exception.
[-]'transaction' module-as-context-manager thwarted by Python 2.7.1[/-][+]improve special method lookup error message[/+]dholth commentedon May 7, 2011
Python should explain AttributeError in the same way when it's raised by the interpreter. The with: statement below should raise the second AttributeError, not the first.
[-]improve special method lookup error message[/-][+]AttributeError should report the same details when raised by lookup_special() as when raised in the REPL[/+]dholth commentedon May 7, 2011
Thank you Benjamin for following up on this issue
merwok commentedon May 23, 2011
http://docs.python.org/dev/reference/datamodel#special-method-names explains that magic methods are looked up on the class, not on the instances. There’s a lot of code out there that erroneously checks for __len__ or __call__ on instances, and this is the second time to my knowledge that a project abused a module-level __enter__ function.
serhiy-storchaka commentedon Oct 19, 2019
Would not be better to change an AttributeError to TypeError? Seems this is the only place in the core when missing special method causes an AttributeError and not TypeError.
iritkatriel commentedon Jun 19, 2021
I've reproduced this on 3.11 (though the AttributeError is now on __enter__ and not __exit__ as was the case in msg135484):
serhiy-storchaka commentedon Jun 20, 2021
PR 26809 makes "with" and "async with" raising TypeError instead of AttributeError for wrong types.
serhiy-storchaka commentedon Jun 29, 2021
New changeset 20a8800 by Serhiy Storchaka in branch 'main':
bpo-12022: Change error type for bad objects in "with" and "async with" (GH-26809)
20a8800