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

Open Beginner friendly
#2,844 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
88/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

Research direction

The issue points to googleapiclient/_helpers.py and the positional() default inference path. Read that function and inspect its existing tests for bare decorator use; add coverage for functions with no defaults and no parameters. Done when those cases no longer raise during decoration and the tests pass.

Written by the indexing model from the issue text.

Description

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)
Dominant language
Python
Stars
9k
Forks
2.6k
Avg merge
28m
Merged PRs (30d)
4

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from googleapis/google-api-python-client

All issues in googleapis/google-api-python-client

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.