From 99541895fbf258fc8b834fe7ffad917fef43ce68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=20Wei=20=28=E4=BB=BB=E5=8D=AB=29?= Date: Thu, 24 Mar 2022 18:01:14 +0800 Subject: [PATCH] the `defaults` in FullArgSpec may be `None` (#40882) --- tools/check_api_compatible.py | 8 +++++--- tools/test_check_api_compatible.py | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tools/check_api_compatible.py b/tools/check_api_compatible.py index cc307a88466..f91112abd64 100644 --- a/tools/check_api_compatible.py +++ b/tools/check_api_compatible.py @@ -75,7 +75,7 @@ def check_compatible(old_api_spec, new_api_spec): def check_compatible_str(old_api_spec_str, new_api_spec_str): patArgSpec = re.compile( - r'args=(.*), varargs=.*defaults=\((.*)\), kwonlyargs=.*') + r'args=(.*), varargs=.*defaults=(None|\((.*)\)), kwonlyargs=.*') mo_o = patArgSpec.search(old_api_spec_str) mo_n = patArgSpec.search(new_api_spec_str) if not (mo_o and mo_n): @@ -86,8 +86,10 @@ def check_compatible_str(old_api_spec_str, new_api_spec_str): args_o = eval(mo_o.group(1)) args_n = eval(mo_n.group(1)) - defaults_o = mo_o.group(2).split(', ') - defaults_n = mo_n.group(2).split(', ') + defaults_o = mo_o.group(2) if mo_o.group(3) is None else mo_o.group(3) + defaults_n = mo_n.group(2) if mo_n.group(3) is None else mo_n.group(3) + defaults_o = defaults_o.split(', ') if defaults_o else [] + defaults_n = defaults_n.split(', ') if defaults_n else [] return _check_compatible(args_o, args_n, defaults_o, defaults_n) diff --git a/tools/test_check_api_compatible.py b/tools/test_check_api_compatible.py index 23debef657f..24e7b3a8f8a 100644 --- a/tools/test_check_api_compatible.py +++ b/tools/test_check_api_compatible.py @@ -105,6 +105,11 @@ class Test_check_compatible_str(unittest.TestCase): argspec_o = self.argspec_str_o self.assertFalse(check_compatible_str(argspec_o, argspec_n)) + def test_args_defaults_None(self): + argspec_o = """inspect.FullArgSpec(args=['filename'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={'filename': })""" + argspec_n = """inspect.FullArgSpec(args=['filename'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={'filename': })""" + self.assertTrue(check_compatible_str(argspec_o, argspec_n)) + class Test_read_argspec_from_file(unittest.TestCase): def setUp(self) -> None: -- GitLab