Skip to content

Commit 384e212

Browse files
authored
Fix for non-IS decorators (#71)
* Handling for functions which are decorated with other decorators which don't surface underlying functions * Some tests for the function
1 parent e87133a commit 384e212

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

inference_schema/schema_util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def _get_decorators(func):
9999
"""
100100

101101
dec = []
102-
if not func.__closure__:
102+
if not hasattr(func, '__closure__') or not func.__closure__:
103103
return [func]
104104
if hasattr(func, '__closure__'):
105105
for closure in func.__closure__:
@@ -118,7 +118,7 @@ def _get_function_full_qual_name(func):
118118
"""
119119

120120
original_func = _get_decorators(func)[-1]
121-
base_func_name = original_func.__name__
121+
base_func_name = getattr(original_func, '__name__', '<unknown>')
122122
module_name = getattr(original_func, '__module__', '<unknown>')
123123
return '{}.{}'.format(module_name, base_func_name)
124124

tests/test_schema_util.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# ---------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# ---------------------------------------------------------
4+
5+
from inference_schema.parameter_types.standard_py_parameter_type import StandardPythonParameterType
6+
from inference_schema.schema_decorators import input_schema, output_schema
7+
from inference_schema.schema_util import is_schema_decorated
8+
9+
10+
@input_schema('data', StandardPythonParameterType('Hello'))
11+
def input_decorated_function(data):
12+
return data
13+
14+
15+
@output_schema(StandardPythonParameterType('Hello'))
16+
def output_decorated_function(data):
17+
return data
18+
19+
20+
@input_schema('data', StandardPythonParameterType('Hello'))
21+
@output_schema(StandardPythonParameterType('Hello'))
22+
def both_decorated_function(data):
23+
return data
24+
25+
26+
def passthrough_decorator(wrapt_func):
27+
def inner_func(*args, **kwargs):
28+
return wrapt_func(*args, **kwargs)
29+
return inner_func
30+
31+
32+
@passthrough_decorator
33+
def custom_decorator_function(data):
34+
return data
35+
36+
37+
class TestSchemaUtil(object):
38+
39+
def test_is_schema_decorated(self):
40+
assert is_schema_decorated(input_decorated_function)
41+
assert is_schema_decorated(output_decorated_function)
42+
assert is_schema_decorated(both_decorated_function)
43+
assert not is_schema_decorated(custom_decorator_function)

0 commit comments

Comments
 (0)