google-api-python-client icon indicating copy to clipboard operation
google-api-python-client copied to clipboard

fix: @_helpers.positional raises TypeError when decorating functions without default arguments

Open codeXsidd opened this issue 1 month ago • 0 comments

Problem

When @googleapiclient._helpers.positional is used as a bare decorator without arguments (e.g., @positional) on a callable that does not define default argument values (or takes no parameters), it raises TypeError: object of type 'NoneType' has no len() at function definition time.

Current Behavior

In googleapiclient/_helpers.py, positional() infers the maximum number of positional arguments when called without arguments:

    if isinstance(max_positional_args, int):
        return positional_decorator
    else:
        args, _, _, defaults, _, _, _ = inspect.getfullargspec(max_positional_args)
        return positional(len(args) - len(defaults))(max_positional_args)

When max_positional_args has no default parameters (or takes 0 parameters), inspect.getfullargspec(max_positional_args).defaults returns None. Evaluating len(defaults) then raises: TypeError: object of type 'NoneType' has no len()

Expected Behavior

When @positional is used without arguments on a function without default parameters (such as def func(a, b): ...), defaults should default to an empty sequence (e.g., len(defaults or ())), correctly calculating len(args) - 0 = len(args) positional parameters instead of crashing with a TypeError.

Reproduction

from googleapiclient import _helpers

@_helpers.positional
def my_function(a, b):
    return a + b

Result:

TypeError: object of type 'NoneType' has no len()

Technical Context

In googleapiclient/_helpers.py, replacing len(defaults) with len(defaults or ()) safely treats None as 0 default arguments:

args, _, _, defaults, _, _, _ = inspect.getfullargspec(max_positional_args)
return positional(len(args) - len(defaults or ()))(max_positional_args)

codeXsidd avatar Oct 22 '26 10:10 codeXsidd