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.

Title: hasattr catches only AttributeError
Type: Stage:
Components: Interpreter Core Versions:
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: tim.peters Nosy List: arigo, jvr, quarl, quinn_dunkan, tim.peters
Priority: normal Keywords: patch

Created on 2002-01-17 02:52 by quinn_dunkan, last changed 2022-04-10 16:04 by admin. This issue is now closed.

File name Uploaded Description Edit
di quinn_dunkan, 2002-01-17 02:52
Messages (6)
msg38717 - (view) Author: Quinn Dunkan (quinn_dunkan) Date: 2002-01-17 02:52
Curse me for a fool.  I reported this exact same thing
in getattr but failed to
look 30 lines down to notice hasattr.

hasattr(foo, 'bar') catches all exceptions.  I think it
should only catch 
AttributeError.  Example:

>>> class Foo:
...     def __getattr__(self, attr):
...         assert 0
>>> f = Foo()
>>> hasattr(f, 'bar')
0               # should have gotten an AssertionError

This patch makes hasattr only catch AttributeError.  I
changed the 
docstring to reflect that, and also changed the getattr
to read a little more naturally.
msg38718 - (view) Author: Just van Rossum (jvr) * (Python triager) Date: 2002-03-16 08:24
Logged In: YES 

(The patch seems to be reversed.)
The patch otherwise looks fine to me, but it will break 
code that depends on the current behavior.
It can be argued that if getattr() raises *any* error, the 
attr doesn't exist, so the current behavior is in fact 
msg38719 - (view) Author: Quinn Dunkan (quinn_dunkan) Date: 2002-03-16 08:55
Logged In: YES 

That's true, but the current behavior can mask bugs
unexpectedly.  For example, if you ask someone if the
brakes are engaged, and they discover that the brakes have
crumbled to dust and fallen off, you probably want a
different answer than "no". :)

getattr() (now) only catches AttributeErrors, so there's 
a consistency thing too.

Anyway, it's your call :)
msg38720 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2002-11-12 13:27
Logged In: YES 

This looks like a possibly worthwhile semantic change.  In
order to help this patch progress I would recommend you to
raise the question in python-dev.

The library documentation should also be patched to reflect
the change.
msg38721 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2002-11-12 22:38
Logged In: YES 

Sorry, I'm rejecting this, at Guido's request.  I agree:  far too 
much code would break if this were to change now; if you 
want hasattr()-like functionality that passes on "unexpected" 
exceptions, you can get it now via doing getattr(obj, attr, 
None); and, most fundamentally, it's part of hasattr's contract 
(design) that it never raises an exception.
msg165640 - (view) Author: Karl Chen (quarl) Date: 2012-07-16 17:16
For the record, this was eventually fixed for Python 3.2.  See

Adding this here because issue504714 comes up earlier than issue9666 in many web searches.
Date User Action Args
2022-04-10 16:04:53adminsetgithub: 35932
2012-07-16 17:16:20quarlsetnosy: + quarl
messages: + msg165640
2002-01-17 02:52:03quinn_dunkancreate