未验证 提交 d6b94d26 编写于 作者: N Nyakku Shigure 提交者: GitHub

[CodeStyle][py2][py311] replace deprecated `inspect.getargspec` with...

[CodeStyle][py2][py311] replace deprecated `inspect.getargspec` with `inspect.getfullargspec` (#48218)

* [CodeStyle][py2] use inspect.getfullargspect instead of deprecated inspect.getargspec

* refactor to f-string
上级 6992170e
......@@ -306,7 +306,7 @@ class DistributedTensor:
def _copy_kwargs(serial_tensor):
kwargs = {}
no_need_copy_args = ["self", "block", "shape", "name"]
arg_spec = inspect.getargspec(Variable.__init__)
arg_spec = inspect.getfullargspec(Variable.__init__)
for key in arg_spec.args:
# TODO: Check the copied attribute from serial tensor whether valid
......
......@@ -96,21 +96,6 @@ FOR_ITER_ZIP_TO_LIST_PREFIX = '__for_loop_iter_zip'
RE_PYNAME = '[a-zA-Z0-9_]+'
RE_PYMODULE = r'[a-zA-Z0-9_]+\.'
# FullArgSpec is valid from Python3. Defined a Namedtuple to
# to make it available in Python2.
FullArgSpec = collections.namedtuple(
'FullArgSpec',
[
'args',
'varargs',
'varkw',
'defaults',
'kwonlyargs',
'kwonlydefaults',
'annotations',
],
)
def data_layer_not_check(name, shape, dtype='float32', lod_level=0):
"""
......@@ -199,27 +184,11 @@ def saw(x):
return x
def getfullargspec(target):
if hasattr(inspect, "getfullargspec"):
return inspect.getfullargspec(target)
else:
argspec = inspect.getargspec(target)
return FullArgSpec(
args=argspec.args,
varargs=argspec.varargs,
varkw=argspec.keywords,
defaults=argspec.defaults,
kwonlyargs=[],
kwonlydefaults=None,
annotations={},
)
def parse_arg_and_kwargs(function):
"""
Returns full argument names as list. e.g ['x', 'y', 'z']
"""
fullargspec = getfullargspec(function)
fullargspec = inspect.getfullargspec(function)
arg_names = fullargspec.args
if arg_names and 'self' == arg_names[0]:
arg_names = fullargspec.args[1:]
......@@ -239,7 +208,7 @@ def parse_varargs_name(function):
"""
Returns varargs name string of function. e.g: 'input' from `foo(x, *input)`
"""
fullargspec = getfullargspec(function)
fullargspec = inspect.getfullargspec(function)
varargs = fullargspec.varargs
return varargs
......@@ -354,7 +323,7 @@ def _delete_keywords_from(node):
func_src = astor.to_source(gast.gast_to_ast(node.func))
import paddle.fluid as fluid
full_args = eval("inspect.getargspec({})".format(func_src))
full_args = eval(f"inspect.getfullargspec({func_src})")
full_args_name = full_args[0]
node.keywords = [k for k in node.keywords if k.arg in full_args_name]
......@@ -438,9 +407,7 @@ def update_args_of_func(node, dygraph_node, method_name):
if method_name == "__init__" or eval(
"issubclass({}, fluid.dygraph.Layer)".format(class_src)
):
full_args = eval(
"inspect.getargspec({}.{})".format(class_src, method_name)
)
full_args = eval(f"inspect.getfullargspec({class_src}.{method_name})")
full_args_name = [
arg_name for arg_name in full_args[0] if arg_name != "self"
]
......
......@@ -12700,7 +12700,7 @@ class PyFuncRegistry:
self._func = func
# find named args using reflection
args = inspect.getargspec(self._func)
args = inspect.getfullargspec(self._func)
if len(args[0]) == 0 and args[1] is None and args[2] is None:
# Function with no inputs
self._named_args = None
......
......@@ -94,10 +94,7 @@ def restore_flatten_list(l, splits):
def extract_args(func):
if hasattr(inspect, 'getfullargspec'):
return inspect.getfullargspec(func)[0]
else:
return inspect.getargspec(func)[0]
return inspect.getfullargspec(func).args
def _all_gather(x, nranks, ring_id=0, use_calc_stream=True):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册