Skip to content

Commit

Permalink
Renames staticmethod promise_for_dict to for_dict. Improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Jun 1, 2016
1 parent ca0387b commit a180485
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ This function wraps the `obj` act as a `Promise` if possible.
Python `Future`s are supported, with a callback to `promise.done` when resolved.


#### Promise.promise_for_dict(d)
#### Promise.for_dict(d)

A special function that takes a dictionary of promises and turns them
into a promise for a dictionary of values. In other words, this turns
Expand Down
30 changes: 19 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ The ``resolver`` function is passed two arguments:
2. ``reject`` should be called with a single argument. The returned
promise will be rejected with that argument.

Static Functions
~~~~~~~~~~~~~~~~
Class Methods
~~~~~~~~~~~~~

These methods are invoked by calling ``Promise.methodName``.

Expand All @@ -71,8 +71,8 @@ something that is close to a promise (such as a jQuery attempt at a
promise) it returns a Promise that takes on the state of ``value``
(rejected or fulfilled).

Promise.reject(value)
^^^^^^^^^^^^^^^^^^^^^
Promise.rejected(value)
^^^^^^^^^^^^^^^^^^^^^^^

Returns a rejected promise with the given value.

Expand All @@ -90,6 +90,21 @@ replaced by their fulfilled values. e.g.
assert p.value is True
Promise.promisify(obj)
^^^^^^^^^^^^^^^^^^^^^^

This function wraps the ``obj`` act as a ``Promise`` if possible. Python
``Future``\ s are supported, with a callback to ``promise.done`` when
resolved.

Promise.for\_dict(d)
^^^^^^^^^^^^^^^^^^^^

A special function that takes a dictionary of promises and turns them
into a promise for a dictionary of values. In other words, this turns an
dictionary of promises for values into a promise for a dictionary of
values.

Instance Methods
~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -141,13 +156,6 @@ is\_thenable(obj)
This function checks if the ``obj`` is a ``Promise``, or could be
``promisify``\ ed.

promisify(obj)
~~~~~~~~~~~~~~

This function wraps the ``obj`` act as a ``Promise`` if possible. Python
``Future``\ s are supported, with a callback to ``promise.done`` when
resolved.

Notes
=====

Expand Down
6 changes: 3 additions & 3 deletions promise.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def handleSuccess(_):

@classmethod
def promisify(cls, obj):
if isinstance(obj, Promise):
if isinstance(obj, cls):
return obj
elif is_future(obj):
promise = cls()
Expand All @@ -436,7 +436,7 @@ def promisify(cls, obj):
raise TypeError("Object is not a Promise like object.")

@classmethod
def promise_for_dict(cls, m):
def for_dict(cls, m):
"""
A special function that takes a dictionary of promises
and turns them into a promise for a dictionary of values.
Expand All @@ -456,7 +456,7 @@ def handleSuccess(resolved_values):


promisify = Promise.promisify
promise_for_dict = Promise.promise_for_dict
promise_for_dict = Promise.for_dict


def _process_future_result(promise):
Expand Down
13 changes: 12 additions & 1 deletion tests/test_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def test_promise_all_if():

# promise_for_dict
@pytest.fixture(params=[
Promise.promise_for_dict,
Promise.for_dict,
free_promise_for_dict,
])
def promise_for_dict(request):
Expand Down Expand Up @@ -471,3 +471,14 @@ def test_promisify_object(promisify):
with pytest.raises(TypeError) as excinfo:
promisify(object())
assert str(excinfo.value) == "Object is not a Promise like object."


def test_promisify_promise_subclass():
class MyPromise(Promise):
pass

p = Promise()
p.fulfill(10)
m_p = MyPromise.promisify(p)
assert isinstance(m_p, MyPromise)
assert m_p.get() == p.get()

0 comments on commit a180485

Please sign in to comment.