Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ Version 2.4.2
attribute of a ``PartialCallableObjectProxy`` which was initialized, but
with no keyword arguments, continues to be an empty dictionary.

* Calling ``__get__()`` on an ``AutoObjectProxy`` or ``LazyObjectProxy``
wrapping a descriptor, with only the ``instance`` argument supplied,
failed with a ``TypeError`` as the ``owner`` argument was required. The
descriptor protocol makes ``owner`` optional, and builtin descriptors
such as functions and ``property`` accept the one argument form, so
manual binding through the proxy did not behave the same as manual
binding of the wrapped descriptor. The ``owner`` argument now defaults
to ``None``, consistent with ``__get__()`` on function wrappers, and
``None`` is what the wrapped descriptor is passed when it is omitted.
Normal attribute lookup was not affected as Python always supplies both
arguments in that case.

Version 2.4.1
-------------

Expand Down
20 changes: 20 additions & 0 deletions tests/core/test_auto_object_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,25 @@ def method(instance):
proxy.__get__(None, object), descriptor.__get__(None, object)
)

def test_descriptor_optional_owner_required_by_wrapped(self):
# A descriptor implemented in Python can require the owner argument,
# as Python always supplies it on attribute lookup. When the owner
# is omitted in a manual call through the proxy, it is None which
# must be passed to the wrapped descriptor.

class Descriptor:
def __get__(self, instance, owner):
return (instance, owner)

instance = object()

proxy = wrapt.AutoObjectProxy(Descriptor())

self.assertEqual(proxy.__get__(instance), (instance, None))
self.assertEqual(proxy.__get__(instance, None), (instance, None))
self.assertEqual(proxy.__get__(instance, object), (instance, object))
self.assertEqual(proxy.__get__(None, object), (None, object))

def test_descriptor(self):
class Descriptor:
def __init__(self, value):
Expand Down Expand Up @@ -409,6 +428,7 @@ def __get__(self, instance, owner):
self.assertFalse(hasattr(proxy, "__get__"))
proxy.__wrapped__ = Descriptor()
self.assertTrue(hasattr(proxy, "__get__"))
self.assertEqual(proxy.__get__(object()), 42)

def test_get_removed_on_reassignment(self):
class Descriptor:
Expand Down
19 changes: 19 additions & 0 deletions tests/core/test_lazy_object_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ def method(instance):
proxy.__get__(None, object), descriptor.__get__(None, object)
)

def test_descriptor_optional_owner_required_by_wrapped(self):
# A descriptor implemented in Python can require the owner argument,
# as Python always supplies it on attribute lookup. When the owner
# is omitted in a manual call through the proxy, it is None which
# must be passed to the wrapped descriptor.

class Descriptor:
def __get__(self, instance, owner):
return (instance, owner)

instance = object()

proxy = wrapt.LazyObjectProxy(Descriptor, interface=Descriptor)

self.assertEqual(proxy.__get__(instance), (instance, None))
self.assertEqual(proxy.__get__(instance, None), (instance, None))
self.assertEqual(proxy.__get__(instance, object), (instance, object))
self.assertEqual(proxy.__get__(None, object), (None, object))

def test_lazy_import(self):
if "sched" in sys.modules:
del sys.modules["sched"]
Expand Down
Loading