Skip to content

Commit

Permalink
Add __resolved__ property. Closes #41.
Browse files Browse the repository at this point in the history
  • Loading branch information
ionelmc committed Mar 22, 2021
1 parent 4885b1f commit 631a9d5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/lazy_object_proxy/cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,18 @@ static int Proxy_set_annotations(ProxyObject *self,

/* ------------------------------------------------------------------------- */

static PyObject *Proxy_get_resolved(
ProxyObject *self)
{
PyObject *result;

result = self->wrapped ? Py_True : Py_False;
Py_INCREF(result);
return result;
}

/* ------------------------------------------------------------------------- */

static PyObject *Proxy_get_wrapped(
ProxyObject *self)
{
Expand Down Expand Up @@ -1444,6 +1456,8 @@ static PyGetSetDef Proxy_getset[] = {
(setter)Proxy_set_wrapped, 0 },
{ "__factory__", (getter)Proxy_get_factory,
(setter)Proxy_set_factory, 0 },
{ "__resolved__", (getter)Proxy_get_resolved,
NULL, 0 },
{ NULL },
};

Expand Down
4 changes: 4 additions & 0 deletions src/lazy_object_proxy/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class Proxy(with_metaclass(_ProxyMetaType)):
def __init__(self, factory):
self.__dict__['__factory__'] = factory

@property
def __resolved__(self):
return '__wrapped__' in self.__dict__

@cached_property
def __wrapped__(self):
self = self.__dict__
Expand Down
10 changes: 10 additions & 0 deletions src/lazy_object_proxy/slots.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class Proxy(with_metaclass(_ProxyMetaType)):
* ``__factory__`` is the callback that "materializes" the object we proxy to.
* ``__target__`` will contain the object we proxy to, once it's "materialized".
* ``__resolved__`` is a boolean, `True` if factory was called.
* ``__wrapped__`` is a property that does either:
* return ``__target__`` if it's set.
Expand All @@ -83,6 +84,15 @@ class Proxy(with_metaclass(_ProxyMetaType)):
def __init__(self, factory):
object.__setattr__(self, '__factory__', factory)

@property
def __resolved__(self, __getattr__=object.__getattribute__):
try:
__getattr__(self, '__target__')
except AttributeError:
return False
else:
return True

@property
def __wrapped__(self, __getattr__=object.__getattribute__, __setattr__=object.__setattr__,
__delattr__=object.__delattr__):
Expand Down
20 changes: 20 additions & 0 deletions tests/test_lazy_object_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1846,6 +1846,7 @@ def test_proto(benchmark, prototype):
def test_subclassing_with_local_attr(lop):
class Foo:
pass

called = []

class LazyProxy(lop.Proxy):
Expand Down Expand Up @@ -1897,3 +1898,22 @@ def test_fspath(lop):

def test_fspath_method(lop):
assert lop.Proxy(FSPathMock).__fspath__() == '/tmp'


def test_resolved_new(lop):
obj = lop.Proxy.__new__(lop.Proxy)
assert obj.__resolved__ is False


def test_resolved(lop):
obj = lop.Proxy(lambda: None)
assert obj.__resolved__ is False
assert obj.__wrapped__ is None
assert obj.__resolved__ is True


def test_resolved_str(lop):
obj = lop.Proxy(lambda: None)
assert obj.__resolved__ is False
str(obj)
assert obj.__resolved__ is True

0 comments on commit 631a9d5

Please sign in to comment.