mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-23 22:00:25 -05:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
@@ -20,8 +20,8 @@ def when(target, *args, **kwargs):
|
|||||||
|
|
||||||
disconnect(whenable, handler)
|
disconnect(whenable, handler)
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# If "when" is called as a function, try to grab the handler from the
|
# If "when" is called as a function, try to grab the handler from the
|
||||||
# arguments. If there's no handler, this must be a decorator based call.
|
# arguments. If there's no handler, this must be a decorator based call.
|
||||||
handler = None
|
handler = None
|
||||||
@@ -35,7 +35,7 @@ def when(target, *args, **kwargs):
|
|||||||
whenable = hasattr(target, "__when__")
|
whenable = hasattr(target, "__when__")
|
||||||
# If not when-able, the DOM selector for the target event.
|
# If not when-able, the DOM selector for the target event.
|
||||||
if not whenable:
|
if not whenable:
|
||||||
# The target is an event linked to a DOM selector. Extract the
|
# The target is an event linked to a DOM selector. Extract the
|
||||||
# selector from the arguments or keyword arguments.
|
# selector from the arguments or keyword arguments.
|
||||||
if args:
|
if args:
|
||||||
selector = args[0]
|
selector = args[0]
|
||||||
@@ -68,11 +68,15 @@ def when(target, *args, **kwargs):
|
|||||||
if not sig.parameters:
|
if not sig.parameters:
|
||||||
# Function is async: must be awaited
|
# Function is async: must be awaited
|
||||||
if inspect.iscoroutinefunction(func):
|
if inspect.iscoroutinefunction(func):
|
||||||
|
|
||||||
async def wrapper(*args, **kwargs):
|
async def wrapper(*args, **kwargs):
|
||||||
await func()
|
await func()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
func()
|
func()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
wrapper = func
|
wrapper = func
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
|||||||
@@ -231,7 +231,7 @@ def test_when_decorates_a_whenable():
|
|||||||
self.handler = None
|
self.handler = None
|
||||||
self.args = None
|
self.args = None
|
||||||
self.kwargs = None
|
self.kwargs = None
|
||||||
|
|
||||||
def trigger(self):
|
def trigger(self):
|
||||||
"""
|
"""
|
||||||
Triggers the whenable object, resulting in the handler being
|
Triggers the whenable object, resulting in the handler being
|
||||||
@@ -242,7 +242,7 @@ def test_when_decorates_a_whenable():
|
|||||||
"args": self.args,
|
"args": self.args,
|
||||||
"kwargs": self.kwargs,
|
"kwargs": self.kwargs,
|
||||||
}
|
}
|
||||||
self.handler(result) # call the handler
|
self.handler(result) # call the handler
|
||||||
|
|
||||||
def __when__(self, handler, *args, **kwargs):
|
def __when__(self, handler, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
@@ -269,7 +269,7 @@ def test_when_decorates_a_whenable():
|
|||||||
counter += 1
|
counter += 1
|
||||||
assert result["args"] == ("foo", "bar")
|
assert result["args"] == ("foo", "bar")
|
||||||
assert result["kwargs"] == {"baz": "qux"}
|
assert result["kwargs"] == {"baz": "qux"}
|
||||||
|
|
||||||
# The function should not be called until the whenable object is triggered.
|
# The function should not be called until the whenable object is triggered.
|
||||||
assert counter == 0
|
assert counter == 0
|
||||||
# Trigger the whenable object.
|
# Trigger the whenable object.
|
||||||
@@ -294,7 +294,7 @@ def test_when_called_with_a_whenable():
|
|||||||
self.handler = None
|
self.handler = None
|
||||||
self.args = None
|
self.args = None
|
||||||
self.kwargs = None
|
self.kwargs = None
|
||||||
|
|
||||||
def trigger(self):
|
def trigger(self):
|
||||||
"""
|
"""
|
||||||
Triggers the whenable object, resulting in the handler being
|
Triggers the whenable object, resulting in the handler being
|
||||||
@@ -305,7 +305,7 @@ def test_when_called_with_a_whenable():
|
|||||||
"args": self.args,
|
"args": self.args,
|
||||||
"kwargs": self.kwargs,
|
"kwargs": self.kwargs,
|
||||||
}
|
}
|
||||||
self.handler(result) # call the handler
|
self.handler(result) # call the handler
|
||||||
|
|
||||||
def __when__(self, handler, *args, **kwargs):
|
def __when__(self, handler, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
@@ -330,7 +330,7 @@ def test_when_called_with_a_whenable():
|
|||||||
counter += 1
|
counter += 1
|
||||||
assert result["args"] == ("foo", "bar")
|
assert result["args"] == ("foo", "bar")
|
||||||
assert result["kwargs"] == {"baz": "qux"}
|
assert result["kwargs"] == {"baz": "qux"}
|
||||||
|
|
||||||
# When as a function.
|
# When as a function.
|
||||||
web.when(whenable, handler, "foo", "bar", baz="qux")
|
web.when(whenable, handler, "foo", "bar", baz="qux")
|
||||||
|
|
||||||
@@ -340,4 +340,4 @@ def test_when_called_with_a_whenable():
|
|||||||
whenable.trigger()
|
whenable.trigger()
|
||||||
# The function should have been called when the whenable object was
|
# The function should have been called when the whenable object was
|
||||||
# triggered.
|
# triggered.
|
||||||
assert counter == 1
|
assert counter == 1
|
||||||
|
|||||||
Reference in New Issue
Block a user