Skip to content

Commit 6805865

Browse files
committed
Check types more explicitly.
1 parent 9ce1049 commit 6805865

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

innerscope/core.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,10 +745,12 @@ def inner_scoped_func(func):
745745
use_globals=use_globals,
746746
method=method,
747747
)
748-
if inspect.isgeneratorfunction(func):
748+
if type(func) is ScopedGeneratorFunction or inspect.isgeneratorfunction(func):
749749
klass = ScopedGeneratorFunction
750-
else:
750+
elif type(func) is ScopedFunction or inspect.isfunction(func):
751751
klass = ScopedFunction
752+
else:
753+
raise TypeError(f"scoped_function expects a Python function. Got type: {type(func)}")
752754
return klass(
753755
func,
754756
*mappings,

innerscope/tests/test_core.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,6 @@ def f(sum):
592592
return dict + 1
593593

594594
sf = scoped_function(f)
595-
# 1/0
596595
assert sf.missing == set()
597596
assert sf.outer_scope == {"min": 1}
598597
assert sf.builtin_names == {"max"}
@@ -641,3 +640,24 @@ def g():
641640
assert sg.builtin_names == set()
642641
assert sg.missing == set()
643642
assert sg() == {"a": 101, "hex": 100}
643+
644+
645+
def test_from_scopedgenerator():
646+
def gen():
647+
x = 1
648+
649+
sgen = scoped_function(gen)
650+
sgen2 = scoped_function(sgen)
651+
assert sgen() == {"x": 1}
652+
assert sgen2() == {"x": 1}
653+
654+
655+
def test_bad_type():
656+
def f(x):
657+
y = x + 1
658+
return y + 1
659+
660+
cf = classmethod(f)
661+
with raises(TypeError, match="expects a Python function"):
662+
scoped_function(cf)
663+
assert innerscope.call(f, 1) == {"x": 1, "y": 2}

0 commit comments

Comments
 (0)