From 843d539af1c6803b9b39557f55d0e9a299af6a3a Mon Sep 17 00:00:00 2001 From: John Maloney Date: Tue, 9 Apr 2024 22:11:47 -0400 Subject: [PATCH 1/2] test: Implement tests showing issues in python parameter parsing --- test/test_languages/testPython.py | 56 +++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/test/test_languages/testPython.py b/test/test_languages/testPython.py index 107df2f..a052024 100644 --- a/test/test_languages/testPython.py +++ b/test/test_languages/testPython.py @@ -149,6 +149,62 @@ def function_with_2_parameters_and_default_value(a, b=None): functions = get_python_function_list(inspect.getsource(namespace_df)) self.assertEqual(2, functions[0].parameter_count) self.assertEqual(['a', 'b'], functions[0].parameters) + self.assertEqual("function_with_2_parameters_and_default_value( a , b = None )", + functions[0].long_name) + + def test_parameter_count_with_type_annotations(self): + functions = get_python_function_list(''' + def function_with_3_parameters(a: str, b: int, c: float): + pass + ''') + self.assertEqual(1, len(functions)) + self.assertEqual(3, functions[0].parameter_count) + self.assertEqual(['a', 'b', 'c'], functions[0].parameters) + self.assertEqual("function_with_3_parameters( a : str , b : int , c : float )", + functions[0].long_name) + + def test_parameter_count_with_type_annotation_and_default(self): + functions = get_python_function_list(''' + def function_with_3_parameters(a: int = 1): + pass + ''') + self.assertEqual(1, len(functions)) + self.assertEqual(1, functions[0].parameter_count) + self.assertEqual(['a'], functions[0].parameters) + self.assertEqual("function_with_3_parameters( a : int = 1 )", + functions[0].long_name) + + def test_parameter_count_with_parameterized_type_annotations(self): + functions = get_python_function_list(''' + def function_with_parameterized_parameter(a: dict[str, tuple[int, float]]): + pass + def function_with_3_parameterized_parameters(a: dict[str, int], + b: list[float], + c: tuple[int, float, str] + ): + pass + + ''') + self.assertEqual(2, len(functions)) + self.assertEqual(1, functions[0].parameter_count) + self.assertEqual(['a'], functions[0].parameters) + self.assertEqual("function_with_parameterized_parameter( a : dict [ str , tuple [ int , float ] ] )", + functions[0].long_name) + self.assertEqual(3, functions[1].parameter_count) + self.assertEqual(['a', 'b', 'c'], functions[1].parameters) + self.assertEqual("function_with_3_parameterized_parameters( a : dict [ str , int ] , b : list [ float ] , c : tuple [ int , float , str ] )", + functions[1].long_name) + + def test_parameter_count_with_trailing_comma(self): + functions = get_python_function_list(''' + def foo(arg1, + arg2, + ): + # comment + return True + ''') + self.assertEqual(2, functions[0].parameter_count) + self.assertEqual(['arg1', 'arg2'], functions[0].parameters) def test_function_end(self): class namespace3: From 0c744afdc7f114ddcc1574d1ded78ddbe470faf8 Mon Sep 17 00:00:00 2001 From: John Maloney Date: Tue, 9 Apr 2024 22:37:27 -0400 Subject: [PATCH 2/2] fix: Fixes for counting type annotations and trailing commas as parameters --- lizard.py | 10 ++++++++-- lizard_languages/python.py | 7 +++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lizard.py b/lizard.py index 63f0153..4ebb4f5 100755 --- a/lizard.py +++ b/lizard.py @@ -301,11 +301,17 @@ def unqualified_name(self): " %(name)s@%(start_line)s-%(end_line)s@%(filename)s" % self.__dict__) - parameter_count = property(lambda self: len(self.full_parameters)) + parameter_count = property(lambda self: len(self.parameters)) @property def parameters(self): - matches = [re.search(r'(\w+)(\s=.*)?$', f) + # Exclude empty tokens as parameters. These can occur in languages + # allowing a trailing comma on the last parameter in an function + # argument list. + # Regex matches the parameter name, then optionally: + # - a default value given after an '=' sign + # - a type annotation given after a ':' + matches = [re.search(r'(\w+)(\s=.*)?(\s:.*)?$', f) for f in self.full_parameters] return [m.group(1) for m in matches if m] diff --git a/lizard_languages/python.py b/lizard_languages/python.py index 4a23f9e..3e935cc 100644 --- a/lizard_languages/python.py +++ b/lizard_languages/python.py @@ -84,6 +84,8 @@ def _function(self, token): def _dec(self, token): if token == ')': self._state = self._state_colon + elif token == '[': + self._state = self._state_parameterized_type_annotation else: self.context.parameter(token) return @@ -100,3 +102,8 @@ def _state_first_line(self, token): if token.startswith('"""') or token.startswith("'''"): self.context.add_nloc(-token.count('\n') - 1) self._state_global(token) + + def _state_parameterized_type_annotation(self, token): + self.context.add_to_long_function_name(" " + token) + if token == ']': + self._state = self._dec