Skip to content

Commit

Permalink
Drop safe_hasattr; obsolete in Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer committed Nov 17, 2023
1 parent a5d8150 commit 304c046
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 48 deletions.
4 changes: 1 addition & 3 deletions README.rst
Expand Up @@ -16,8 +16,6 @@ pydoc extras is your friend. extras currently contains the following functions:

* try_imports

* safe_hasattr

Which do what their name suggests.


Expand All @@ -31,7 +29,7 @@ the extras authors. See LICENSE for details.
Required Dependencies
---------------------

* Python 2.6+ or 3.0+
* Python 3.7+


Bug reports and patches
Expand Down
13 changes: 0 additions & 13 deletions extras/__init__.py
Expand Up @@ -5,7 +5,6 @@
import sys

__all__ = [
'safe_hasattr',
'try_import',
'try_imports',
]
Expand Down Expand Up @@ -96,15 +95,3 @@ def try_imports(module_names, alternative=_RAISE_EXCEPTION, error_callback=None)
raise ImportError(
"Could not import any of: %s" % ', '.join(module_names))
return alternative


def safe_hasattr(obj, attr, _marker=object()):
"""Does 'obj' have an attribute 'attr'?
Use this rather than built-in hasattr, as the built-in swallows exceptions
in some versions of Python and behaves unpredictably with respect to
properties.
"""
return getattr(obj, attr, _marker) is not _marker


32 changes: 0 additions & 32 deletions extras/tests/test_extras.py
Expand Up @@ -11,7 +11,6 @@
)

from extras import (
safe_hasattr,
try_import,
try_imports,
)
Expand Down Expand Up @@ -43,37 +42,6 @@ def cb(e):
test.assertEquals(len(cb_calls), expected_error_count)


class TestSafeHasattr(TestCase):

def test_attribute_not_there(self):
class Foo(object):
pass
self.assertEqual(False, safe_hasattr(Foo(), 'anything'))

def test_attribute_there(self):
class Foo(object):
pass
foo = Foo()
foo.attribute = None
self.assertEqual(True, safe_hasattr(foo, 'attribute'))

def test_property_there(self):
class Foo(object):
@property
def attribute(self):
return None
foo = Foo()
self.assertEqual(True, safe_hasattr(foo, 'attribute'))

def test_property_raises(self):
class Foo(object):
@property
def attribute(self):
1/0
foo = Foo()
self.assertRaises(ZeroDivisionError, safe_hasattr, foo, 'attribute')


class TestTryImport(TestCase):

def test_doesnt_exist(self):
Expand Down

0 comments on commit 304c046

Please sign in to comment.