Skip to content

Commit

Permalink
Fixed lint and mypy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Mar 5, 2017
1 parent b627500 commit 1f103fd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 33 deletions.
4 changes: 2 additions & 2 deletions promise/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .promise import Promise, promise_for_dict, promisify, is_thenable, async
from .promise import Promise, promise_for_dict, promisify, is_thenable, async_instance

__all__ = ['Promise', 'promise_for_dict', 'promisify', 'is_thenable', 'async']
__all__ = ['Promise', 'promise_for_dict', 'promisify', 'is_thenable', 'async_instance']
11 changes: 5 additions & 6 deletions promise/async_.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from threading import Timer, Thread
# from threading import Timer, Thread

from .compat import Queue
from .context import Context
# from .context import Context

# Based on https://github.com/petkaantonov/bluebird/blob/master/src/async.js


class Scheduler(object):

def call(self, fn):

# thread = Thread(target=fn)
# thread = Timer(0.001, fn)
# fn = thread.start
Expand All @@ -18,7 +17,7 @@ def call(self, fn):
# if not c:
fn()
# else:
# c.on_exit(fn)
# c.on_exit(fn)
except:
pass
# thread = Thread(target=fn)
Expand Down Expand Up @@ -72,8 +71,8 @@ def invoke_later(self, fn, context):
else:
self.schedule.call_later(0.1, fn)

def invoke(self, fn, context, with_trampoline=None):
if with_trampoline or (self.trampoline_enabled and with_trampoline != False):
def invoke(self, fn, context):
if self.trampoline_enabled:
self._async_invoke(fn, context)
else:
self.schedule.call(
Expand Down
17 changes: 13 additions & 4 deletions promise/context.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from typing import List, Callable # flake8: noqa

context_stack = []


class Context(object):

__slots__ = ('_parent', '_exited', '_exit_fns')

def __init__(self):
self._parent = self.peek_context()
if self._parent:
self._parent.on_exit(self._exit)
self._exited = False
self._exit_fns = []
self._exit_fns = [] # type: List[Callable]

def push_context(self):
# if self._trace:
Expand All @@ -20,9 +25,13 @@ def __enter__(self):

def __exit__(self, type, value, traceback):
assert not self._exited, "Can't exit a Context twice"
self.pop_context()
self._exited = True
self.drain_queue()
self._exit()

def _exit(self):
if not self._exited:
self._exited = True
self.pop_context()
self.drain_queue()

def drain_queue(self):
exit_fns = self._exit_fns
Expand Down
14 changes: 5 additions & 9 deletions promise/dataloader.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from collections import Iterable, namedtuple
from functools import partial

from promise import Promise, async
from typing import Sized # flake8: noqa
from typing import List, Sized # flake8: noqa

from .promise import Promise, async_instance
from .context import Context


def identity(x):
Expand Down Expand Up @@ -169,10 +171,6 @@ def prime(self, key, value):
# Private: cached resolved Promise instance
resolved_promise = None



from .promise import async

# def enqueue_post_promise_job(fn):
# # t.run()
# # from threading import Timer
Expand All @@ -184,14 +182,12 @@ def prime(self, key, value):
# resolved_promise = Promise.resolve(None)
# resolved_promise.then(lambda v: queue.invoke(fn)) # TODO: Change to async

from .context import Context

def enqueue_post_promise_job(fn):
global resolved_promise
if not resolved_promise:
resolved_promise = Promise.resolve(None)
# queue.invoke(fn)
async.invoke(fn, context=Context.peek_context())
async_instance.invoke(fn, context=Context.peek_context())
# Promise.resolve(None).then(lambda v: async.invoke(fn, context=Context.peek_context()))
# resolved_promise.then(lambda v: queue.invoke(fn, context=Context.peek_context()))

Expand Down
22 changes: 11 additions & 11 deletions promise/promise.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from sys import version_info
from threading import Event, RLock

from typing import (Any, Callable, Dict, Iterator, Optional, # flake8: noqa
from typing import (List, Any, Callable, Dict, Iterator, Optional, # flake8: noqa
Union)

from .async_ import Async
Expand All @@ -13,7 +13,7 @@
from .context import Context
from .promise_list import PromiseList

async = Async()
async_instance = Async()

IS_PYTHON2 = version_info[0] == 2
DEFAULT_TIMEOUT = 0.5
Expand Down Expand Up @@ -103,7 +103,7 @@ def __init__(self, executor=None):
self._rejection_handler0 = None # type: Union[Callable, partial]
self._promise0 = None # type: Promise
self._future = None # type: Future
self._event_instance = None
self._event_instance = None # type: Event
self._trace = Context.peek_context()

self._is_waiting = False
Expand Down Expand Up @@ -194,7 +194,7 @@ def _fulfill(self, value):
if self._is_async_guaranteed:
self._settle_promises()
else:
async.settle_promises(self)
async_instance.settle_promises(self)

def _reject(self, reason):
self._state = States.REJECTED
Expand All @@ -203,21 +203,21 @@ def _reject(self, reason):

if self._is_final:
assert self._length == 0
return async.fatal_error(reason)
return async_instance.fatal_error(reason)

if self._length > 0:
async.settle_promises(self)
async_instance.settle_promises(self)
else:
self._ensure_possible_rejection_handled()

if self._is_async_guaranteed:
self._settle_promises()
else:
async.settle_promises(self)
async_instance.settle_promises(self)

def _ensure_possible_rejection_handled(self):
# self._rejection_is_unhandled = True
# async.invoke_later(self._notify_unhandled_rejection, self)
# async_instance.invoke_later(self._notify_unhandled_rejection, self)
pass

def _reject_callback(self, reason, synchronous=False):
Expand Down Expand Up @@ -496,7 +496,7 @@ def _then(self, did_fulfill=None, did_reject=None):
value = target._fulfillment_handler0
handler = did_reject
# target._rejection_is_unhandled = False
async.invoke(
async_instance.invoke(
partial(self._settle_promise, promise, handler, value),
context=target._trace,
# target._settle_promise instead?
Expand Down Expand Up @@ -546,12 +546,12 @@ def done(self, did_fulfill=None, did_reject=None):
promise._is_final = True

def done_all(self, handlers=None):
# type: (Promise, List[Callable]) -> List[Promise]
# type: (Promise, List[Callable]) -> None
"""
:type handlers: list[(Any) -> object] | list[((Any) -> object, (Any) -> object)]
"""
if not handlers:
return []
return

for handler in handlers:
if isinstance(handler, tuple):
Expand Down
2 changes: 1 addition & 1 deletion promise/promise_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _iterate(self, values):
Promise = self._promise_class
is_resolved = False
self._length = len(values)
self._values = [ None ] * self._length
self._values = [None] * self._length

result = self.promise

Expand Down

0 comments on commit 1f103fd

Please sign in to comment.