terryyin / lizard

A simple code complexity analyser without caring about the C/C++ header files or Java imports, supports most of the popular languages.
Other
1.81k stars 246 forks source link

fix: Correct parameter count for python function with trailing comma and parameterized type hints #385

Closed jmal0 closed 3 months ago

jmal0 commented 3 months ago

Addresses https://github.com/terryyin/lizard/issues/354

In several situations, the argument count reported for python is incorrect. In particular:

Below is a code snippet that reproduces these issues:

def func_with_trailing_comma(
    a,
    b,
    c,
):
    pass

def func_with_type_parameterized_args(a: dict[str, tuple[int, float]]):
    pass

Running lizard -a 2 demo.py on this reports the following:

================================================
  NLOC    CCN   token  PARAM  length  location  
------------------------------------------------
       6      1     11      4       6 func_with_trailing_comma@1-6@demo.py
       2      1     18      3       2 func_with_type_parameterized_args@9-10@demo.py
1 file analyzed.
==============================================================
NLOC    Avg.NLOC  AvgCCN  Avg.token  function_cnt    file
--------------------------------------------------------------
      8       4.0     1.0       14.5         2     demo.py

=========================================================================================================
!!!! Warnings (cyclomatic_complexity > 15 or length > 1000 or nloc > 1000000 or parameter_count > 2) !!!!
================================================
  NLOC    CCN   token  PARAM  length  location  
------------------------------------------------
       6      1     11      4       6 func_with_trailing_comma@1-6@demo.py
       2      1     18      3       2 func_with_type_parameterized_args@9-10@demo.py
==========================================================================================
Total nloc   Avg.NLOC  AvgCCN  Avg.token   Fun Cnt  Warning cnt   Fun Rt   nloc Rt
------------------------------------------------------------------------------------------
         8       4.0     1.0       14.5        2            2      1.00    1.00

The correct result is reported with the fixes included in the second commit of this branch:

================================================
  NLOC    CCN   token  PARAM  length  location  
------------------------------------------------
       6      1     11      3       6 func_with_trailing_comma@1-6@demo.py
       2      1     18      1       2 func_with_type_parameterized_args@9-10@demo.py
1 file analyzed.
==============================================================
NLOC    Avg.NLOC  AvgCCN  Avg.token  function_cnt    file
--------------------------------------------------------------
      8       4.0     1.0       14.5         2     demo.py

=========================================================================================================
!!!! Warnings (cyclomatic_complexity > 15 or nloc > 1000000 or length > 1000 or parameter_count > 2) !!!!
================================================
  NLOC    CCN   token  PARAM  length  location  
------------------------------------------------
       6      1     11      3       6 func_with_trailing_comma@1-6@demo.py
==========================================================================================
Total nloc   Avg.NLOC  AvgCCN  Avg.token   Fun Cnt  Warning cnt   Fun Rt   nloc Rt
------------------------------------------------------------------------------------------
         8       4.0     1.0       14.5        2            1      0.50    0.75

I have included tests in the first commit of this branch that reproduce the issue and check for the correct result

jmal0 commented 3 months ago

The workflow initially failed due to the use of python 3.6+ syntax in my added tests, in the second push I work around this by embedding the code to be parsed in strings. The python tests are now passing when run with python 2.7, however there were failures in testErlang.py and testKotlin.py which are failing on master as well.

mmmtastymmm commented 3 months ago

My company needs this functionality as well. Would be interesting to see if there is any feedback.

terryyin commented 3 months ago

Thanks for the addition and fix. The test coverage looks great.