Skip to content

fix: handle None defaults in positional decorator (issue #2845) - #2846

Open
codeXsidd wants to merge 1 commit into
googleapis:mainfrom
codeXsidd:fix/issue-2845-positional-decorator-none-defaults
Open

codeXsidd wants to merge 1 commit into
googleapis:mainfrom
codeXsidd:fix/issue-2845-positional-decorator-none-defaults

Conversation

@codeXsidd

Copy link
Copy Markdown

Summary

Fixes TypeError: object of type 'NoneType' has no len() when decorating functions without default arguments using @_helpers.positional as a bare decorator.

Problem

In googleapiclient/_helpers.py, positional() automatically calculates the maximum positional arguments when used without arguments:

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

When a function does not define default arguments (e.g., def func(a, b): ... or def func(): ...), inspect.getfullargspec(...).defaults evaluates to None. Evaluating len(defaults) then crashes with:
TypeError: object of type 'NoneType' has no len()

Solution

Safely fallback to an empty tuple when defaults is None via len(defaults or ()). This correctly computes len(args) - 0 = len(args) positional arguments without raising a TypeError.

Testing

  • Added tests in tests/test__helpers.py verifying @_helpers.positional on functions without default parameters and functions without arguments.
  • Ran tests via pytest tests/test__helpers.py across default and POSITIONAL_EXCEPTION enforcement modes; all passed.

Related Issue

Closes #2845

@codeXsidd
codeXsidd requested a review from a team as a code owner October 22, 2026 10:38
@product-auto-label product-auto-label Bot added the size: s Pull request size is small. label Oct 22, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes a potential TypeError in the positional_wrapper helper function when a decorated function has no default arguments, causing inspect.getfullargspec to return None for defaults. The calculation has been updated to use an empty tuple as a fallback, and corresponding unit tests have been added to verify this behavior. I have no feedback to provide.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: s Pull request size is small.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant