Skip to content

Commit ffc78ab

Browse files
authored
Remove superfluous code now MicroPython supports inspect API for function signature inspection. (#2387)
* Remove superfluous code now MicroPython supports inspect API for function signature inspection. * Added test to ensure all callables are covered.
1 parent b609b60 commit ffc78ab

File tree

3 files changed

+88
-46
lines changed

3 files changed

+88
-46
lines changed

core/src/stdlib/pyscript.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/src/stdlib/pyscript/events.py

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -92,60 +92,26 @@ def when(target, *args, **kwargs):
9292
elements = selector if isinstance(selector, list) else [selector]
9393

9494
def decorator(func):
95-
if config["type"] == "mpy": # Is MicroPython?
95+
sig = inspect.signature(func)
96+
if sig.parameters:
9697
if is_awaitable(func):
9798

98-
async def wrapper(*args, **kwargs):
99-
"""
100-
This is a very ugly hack to get micropython working because
101-
`inspect.signature` doesn't exist. It may be actually better
102-
to not try any magic for now and raise the error.
103-
"""
104-
try:
105-
return await func(*args, **kwargs)
106-
107-
except TypeError as e:
108-
if "takes" in str(e) and "positional arguments" in str(e):
109-
return await func()
110-
raise
99+
async def wrapper(event):
100+
return await func(event)
111101

112102
else:
113-
114-
def wrapper(*args, **kwargs):
115-
"""
116-
This is a very ugly hack to get micropython working because
117-
`inspect.signature` doesn't exist. It may be actually better
118-
to not try any magic for now and raise the error.
119-
"""
120-
try:
121-
return func(*args, **kwargs)
122-
123-
except TypeError as e:
124-
if "takes" in str(e) and "positional arguments" in str(e):
125-
return func()
126-
raise
127-
103+
wrapper = func
128104
else:
129-
sig = inspect.signature(func)
130-
if sig.parameters:
131-
if is_awaitable(func):
105+
# Function doesn't receive events.
106+
if is_awaitable(func):
132107

133-
async def wrapper(event):
134-
return await func(event)
108+
async def wrapper(*args, **kwargs):
109+
return await func()
135110

136-
else:
137-
wrapper = func
138111
else:
139-
# Function doesn't receive events.
140-
if is_awaitable(func):
141-
142-
async def wrapper(*args, **kwargs):
143-
return await func()
144112

145-
else:
146-
147-
def wrapper(*args, **kwargs):
148-
return func()
113+
def wrapper(*args, **kwargs):
114+
return func()
149115

150116
wrapper = wraps(func)(wrapper)
151117
if isinstance(target, Event):

core/tests/python/tests/test_events.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,79 @@ def handler(result):
358358
# The function should have been called when the whenable object was
359359
# triggered.
360360
assert counter == 1
361+
362+
def test_when_on_different_callables():
363+
"""
364+
The when function works with:
365+
366+
* Synchronous functions
367+
* Asynchronous functions
368+
* Inner functions
369+
* Async inner functions
370+
* Closure functions
371+
* Async closure functions
372+
"""
373+
374+
def func(x=1):
375+
# A simple function.
376+
return x
377+
378+
async def a_func(x=1):
379+
# A simple async function.
380+
return x
381+
382+
def make_inner_func():
383+
# Creates a simple inner function.
384+
385+
def inner_func(x=1):
386+
return x
387+
388+
return inner_func
389+
390+
391+
def make_inner_a_func():
392+
# Creates a simple async inner function.
393+
394+
async def inner_a_func(x=1):
395+
return x
396+
397+
return inner_a_func
398+
399+
400+
def make_closure():
401+
# Creates a simple closure function.
402+
a = 1
403+
404+
def closure_func(x=1):
405+
return a + x
406+
407+
return closure_func
408+
409+
410+
def make_a_closure():
411+
# Creates a simple async closure function.
412+
a = 1
413+
414+
async def closure_a_func(x=1):
415+
return a + x
416+
417+
return closure_a_func
418+
419+
420+
inner_func = make_inner_func()
421+
inner_a_func = make_inner_a_func()
422+
cl_func = make_closure()
423+
cl_a_func = make_a_closure()
424+
425+
426+
whenable = Event()
427+
428+
# Each of these should work with the when function.
429+
when(whenable, func)
430+
when(whenable, a_func)
431+
when(whenable, inner_func)
432+
when(whenable, inner_a_func)
433+
when(whenable, cl_func)
434+
when(whenable, cl_a_func)
435+
# If we get here, everything worked.
436+
assert True

0 commit comments

Comments
 (0)