44import logging
55import types
66from inspect import Parameter , Signature
7- from typing import Callable , Dict , Sequence
7+ from typing import Any , Callable , Dict , Sequence , cast
88
99from azure .ai .ml .entities import Component
1010from azure .ai .ml .exceptions import ErrorCategory , ErrorTarget , UnexpectedKeywordError , ValidationException
@@ -26,7 +26,9 @@ class KwParameter(Parameter):
2626 :type _optional: bool
2727 """
2828
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 :
3032 super ().__init__ (name , Parameter .KEYWORD_ONLY , default = default , annotation = annotation )
3133 self ._type = _type
3234 self ._optional = _optional
@@ -54,23 +56,25 @@ def _replace_function_name(func: types.FunctionType, new_name: str) -> types.Fun
5456 else :
5557 # Before python<3.8, replace is not available, we can only initialize the code as following.
5658 # 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
5862 code_template .co_argcount ,
5963 code_template .co_kwonlyargcount ,
6064 code_template .co_nlocals ,
6165 code_template .co_stacksize ,
6266 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
6569 code_template .co_names ,
6670 code_template .co_varnames ,
67- code_template .co_filename ,
71+ code_template .co_filename , # type: ignore
6872 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
7175 # 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
7478 )
7579 # Initialize a new function with the code object and the new name, see the following ref for more details.
7680 # 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
8993
9094
9195# 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 :
9397 """Assert the arg keys are all in keys."""
9498 # pylint: disable=protected-access
9599 # validate component input names
@@ -114,7 +118,7 @@ def _assert_arg_valid(kwargs: dict, keys: list, func_name: str):
114118 kwargs [lower2original_parameter_names [key .lower ()]] = kwargs .pop (key )
115119
116120
117- def _update_dct_if_not_exist (dst : Dict , src : Dict ):
121+ def _update_dct_if_not_exist (dst : Dict , src : Dict ) -> None :
118122 """Computes the union of `src` and `dst`, in-place within `dst`
119123
120124 If a key exists in `dst` and `src` the value in `dst` is preserved
@@ -162,17 +166,18 @@ def create_kw_function_from_parameters(
162166 )
163167 default_kwargs = {p .name : p .default for p in parameters }
164168
165- def f (** kwargs ) :
169+ def f (** kwargs : Any ) -> Any :
166170 # We need to make sure all keys of kwargs are valid.
167171 # Merge valid group keys with original keys.
168172 _assert_arg_valid (kwargs , [* list (default_kwargs .keys ()), * flattened_group_keys ], func_name = func_name )
169173 # We need to put the default args to the kwargs before invoking the original function.
170174 _update_dct_if_not_exist (kwargs , default_kwargs )
171175 return func (** kwargs )
172176
173- f = _replace_function_name (f , func_name )
177+ f = _replace_function_name (cast ( types . FunctionType , f ) , func_name )
174178 # 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
176181 # Set doc/name/module to make sure help(f) shows following expected result.
177182 # Expected help(f):
178183 #
@@ -183,5 +188,5 @@ def f(**kwargs):
183188 f .__doc__ = documentation # Set documentation to update FUNC_DOC in help.
184189 # Set module = None to avoid showing the sentence `in module 'azure.ai.ml.component._dynamic' in help.`
185190 # See https://github.com/python/cpython/blob/2145c8c9724287a310bc77a2760d4f1c0ca9eb0c/Lib/pydoc.py#L1757
186- f .__module__ = None
191+ f .__module__ = None # type: ignore
187192 return f
0 commit comments