4
4
import logging
5
5
import types
6
6
from inspect import Parameter , Signature
7
- from typing import Callable , Dict , Sequence
7
+ from typing import Any , Callable , Dict , Sequence , cast
8
8
9
9
from azure .ai .ml .entities import Component
10
10
from azure .ai .ml .exceptions import ErrorCategory , ErrorTarget , UnexpectedKeywordError , ValidationException
@@ -26,7 +26,9 @@ class KwParameter(Parameter):
26
26
:type _optional: bool
27
27
"""
28
28
29
- def __init__ (self , name , default , annotation = Parameter .empty , _type = "str" , _optional = False ) -> None :
29
+ def __init__ (
30
+ self , name : str , default : Any , annotation : Any = Parameter .empty , _type : str = "str" , _optional : bool = False
31
+ ) -> None :
30
32
super ().__init__ (name , Parameter .KEYWORD_ONLY , default = default , annotation = annotation )
31
33
self ._type = _type
32
34
self ._optional = _optional
@@ -54,23 +56,25 @@ def _replace_function_name(func: types.FunctionType, new_name: str) -> types.Fun
54
56
else :
55
57
# Before python<3.8, replace is not available, we can only initialize the code as following.
56
58
# https://github.com/python/cpython/blob/v3.7.8/Objects/codeobject.c#L97
57
- code = types .CodeType (
59
+
60
+ # Bug Item number: 2881688
61
+ code = types .CodeType ( # type: ignore
58
62
code_template .co_argcount ,
59
63
code_template .co_kwonlyargcount ,
60
64
code_template .co_nlocals ,
61
65
code_template .co_stacksize ,
62
66
code_template .co_flags ,
63
- code_template .co_code ,
64
- code_template .co_consts ,
67
+ code_template .co_code , # type: ignore
68
+ code_template .co_consts , # type: ignore
65
69
code_template .co_names ,
66
70
code_template .co_varnames ,
67
- code_template .co_filename ,
71
+ code_template .co_filename , # type: ignore
68
72
new_name , # Use the new name for the new code object.
69
- code_template .co_firstlineno ,
70
- code_template .co_lnotab ,
73
+ code_template .co_firstlineno , # type: ignore
74
+ code_template .co_lnotab , # type: ignore
71
75
# The following two values are required for closures.
72
- code_template .co_freevars ,
73
- code_template .co_cellvars ,
76
+ code_template .co_freevars , # type: ignore
77
+ code_template .co_cellvars , # type: ignore
74
78
)
75
79
# Initialize a new function with the code object and the new name, see the following ref for more details.
76
80
# https://github.com/python/cpython/blob/4901fe274bc82b95dc89bcb3de8802a3dfedab32/Objects/clinic/funcobject.c.h#L30
@@ -89,7 +93,7 @@ def _replace_function_name(func: types.FunctionType, new_name: str) -> types.Fun
89
93
90
94
91
95
# pylint: disable-next=docstring-missing-param
92
- def _assert_arg_valid (kwargs : dict , keys : list , func_name : str ):
96
+ def _assert_arg_valid (kwargs : dict , keys : list , func_name : str ) -> None :
93
97
"""Assert the arg keys are all in keys."""
94
98
# pylint: disable=protected-access
95
99
# validate component input names
@@ -114,7 +118,7 @@ def _assert_arg_valid(kwargs: dict, keys: list, func_name: str):
114
118
kwargs [lower2original_parameter_names [key .lower ()]] = kwargs .pop (key )
115
119
116
120
117
- def _update_dct_if_not_exist (dst : Dict , src : Dict ):
121
+ def _update_dct_if_not_exist (dst : Dict , src : Dict ) -> None :
118
122
"""Computes the union of `src` and `dst`, in-place within `dst`
119
123
120
124
If a key exists in `dst` and `src` the value in `dst` is preserved
@@ -162,17 +166,18 @@ def create_kw_function_from_parameters(
162
166
)
163
167
default_kwargs = {p .name : p .default for p in parameters }
164
168
165
- def f (** kwargs ) :
169
+ def f (** kwargs : Any ) -> Any :
166
170
# We need to make sure all keys of kwargs are valid.
167
171
# Merge valid group keys with original keys.
168
172
_assert_arg_valid (kwargs , [* list (default_kwargs .keys ()), * flattened_group_keys ], func_name = func_name )
169
173
# We need to put the default args to the kwargs before invoking the original function.
170
174
_update_dct_if_not_exist (kwargs , default_kwargs )
171
175
return func (** kwargs )
172
176
173
- f = _replace_function_name (f , func_name )
177
+ f = _replace_function_name (cast ( types . FunctionType , f ) , func_name )
174
178
# Set the signature so jupyter notebook could have param hint by calling inspect.signature()
175
- f .__signature__ = Signature (parameters )
179
+ # Bug Item number: 2883223
180
+ f .__signature__ = Signature (parameters ) # type: ignore
176
181
# Set doc/name/module to make sure help(f) shows following expected result.
177
182
# Expected help(f):
178
183
#
@@ -183,5 +188,5 @@ def f(**kwargs):
183
188
f .__doc__ = documentation # Set documentation to update FUNC_DOC in help.
184
189
# Set module = None to avoid showing the sentence `in module 'azure.ai.ml.component._dynamic' in help.`
185
190
# See https://github.com/python/cpython/blob/2145c8c9724287a310bc77a2760d4f1c0ca9eb0c/Lib/pydoc.py#L1757
186
- f .__module__ = None
191
+ f .__module__ = None # type: ignore
187
192
return f
0 commit comments