transformers - ✅(Solved) Fix [auto_docstring] _process_kwargs_parameters crashes with AttributeError when module uses from __future__ import annotations [2 pull requests, 1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
huggingface/transformers#45103Fetched 2026-04-08 01:48:34
View on GitHub
Comments
1
Participants
2
Timeline
3
Reactions
0
Author
Timeline (top)
commented ×1cross-referenced ×1labeled ×1

@auto_docstring crashes at import time when applied to a class in a module that uses from __future__ import annotations. The decorator's _process_kwargs_parameters function accesses kwarg_param.annotation.__args__ directly, but from __future__ import annotations makes all annotations strings at runtime — strings don't have __args__.

Root Cause

from __future__ import annotations defers all annotations to strings at runtime. kwarg_param.annotation is "Optional[IsaacKwargs]" (a string) instead of the actual type Optional[IsaacKwargs]. Accessing .__args__ on a string crashes.

The problematic line in _process_kwargs_parameters:

# annotation is a string at runtime — crashes here
if kwarg_param.annotation.__args__[0].__name__ not in BASIC_KWARGS_TYPES:

Fix Action

Fixed

PR fix notes

PR #45104: Fix auto_docstring crash with from future import annotations

Description (problem / solution / changelog)

Description

Fixes #45103

The @auto_docstring decorator crashes at import time when applied to a class in a module that uses from __future__ import annotations. This is because from __future__ import annotations makes all annotations strings at runtime, and the code was trying to access __args__ on a string, which doesn't have that attribute.

Changes

In _process_kwargs_parameters, after the inspect.Parameter.empty check, resolve string annotations before accessing .\_\_args\_\_:

  • If annotation is a string (from from __future__ import annotations), try to resolve it via typing.get_type_hints()
  • If resolution fails, skip gracefully
  • Leave all existing behaviour for non-string annotations unchanged

Testing

  • All existing tests in tests/utils/test_auto_docstring.py pass
  • Tested with modules using from __future__ import annotations
  • Tested with normal modules (without future annotations) to ensure no regression

Changed files

  • src/transformers/utils/auto_docstring.py (modified, +13/-0)

PR #45105: Fix @auto_docstring crash with from future import annotations in _process_kwargs_parameters

Description (problem / solution / changelog)

What does this PR do?

Fixes _process_kwargs_parameters crashing with AttributeError when @auto_docstring is applied in a module that uses from __future__ import annotations.

Fixes #45103

Root cause

from __future__ import annotations defers all annotations to strings at runtime. _process_kwargs_parameters accessed kwarg_param.annotation.__args__ directly, which fails when the annotation is a string instead of a type object:

# annotation is "Optional[IsaacKwargs]" (str), not the actual type — crashes here
if kwarg_param.annotation.__args__[0].__name__ not in BASIC_KWARGS_TYPES:

## Changed files

- `src/transformers/utils/auto_docstring.py` (modified, +19/-1)
- `tests/utils/test_auto_docstring.py` (modified, +53/-0)

Code Example

# annotation is a string at runtime — crashes here
if kwarg_param.annotation.__args__[0].__name__ not in BASIC_KWARGS_TYPES:

---

from transformers import AutoTokenizer
AutoTokenizer.from_pretrained("PerceptronAI/Isaac-0.1", trust_remote_code=True)
RAW_BUFFERClick to expand / collapse

System Info

Bug: _process_kwargs_parameters crashes with AttributeError when module uses from __future__ import annotations

Description

@auto_docstring crashes at import time when applied to a class in a module that uses from __future__ import annotations. The decorator's _process_kwargs_parameters function accesses kwarg_param.annotation.__args__ directly, but from __future__ import annotations makes all annotations strings at runtime — strings don't have __args__.

Environment

  • transformers version: 5.x (main)
  • Python version: 3.12

Root Cause

from __future__ import annotations defers all annotations to strings at runtime. kwarg_param.annotation is "Optional[IsaacKwargs]" (a string) instead of the actual type Optional[IsaacKwargs]. Accessing .__args__ on a string crashes.

The problematic line in _process_kwargs_parameters:

# annotation is a string at runtime — crashes here
if kwarg_param.annotation.__args__[0].__name__ not in BASIC_KWARGS_TYPES:

Relation to #45056

#45056 defers class docstring generation lazily, which avoids the crash in cases where cls.__doc__ is never accessed. However it does not fix the underlying bug — an explicit cls.__doc__ access would still crash. This issue is about fixing the root cause directly and is complementary to #45056.

Who can help?

Resolved the issue and validated #45105

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

Any trust_remote_code=True model checkpoint whose custom module uses both from __future__ import annotations and @auto_docstring will trigger this. Concretely, PerceptronAI/Isaac-0.1:

from transformers import AutoTokenizer
AutoTokenizer.from_pretrained("PerceptronAI/Isaac-0.1", trust_remote_code=True)

Expected behavior

Should resolve the string annotation via typing.get_type_hints() and generate kwargs documentation normally. If resolution fails, skip gracefully rather than crash.

extent analysis

Fix Plan

To resolve the issue, we need to modify the _process_kwargs_parameters function to handle string annotations correctly. We can use the typing.get_type_hints() function to resolve the string annotations to their actual types.

Here are the concrete steps:

  • Modify the _process_kwargs_parameters function to check if the annotation is a string.
  • If it's a string, use typing.get_type_hints() to resolve it to its actual type.
  • If resolution fails, skip the parameter gracefully.

Example code:

import typing
from typing import get_type_hints

def _process_kwargs_parameters(kwarg_param):
    # Check if annotation is a string
    if isinstance(kwarg_param.annotation, str):
        # Try to resolve the string annotation
        try:
            type_hints = get_type_hints(kwarg_param.__class__)
            resolved_annotation = type_hints.get(kwarg_param.name)
            if resolved_annotation:
                # Use the resolved annotation
                annotation = resolved_annotation
            else:
                # If resolution fails, skip the parameter
                return
        except Exception:
            # If resolution fails, skip the parameter
            return
    else:
        # If annotation is not a string, use it directly
        annotation = kwarg_param.annotation

    # Rest of the function remains the same
    if annotation.__args__[0].__name__ not in BASIC_KWARGS_TYPES:
        # ...

Verification

To verify that the fix worked, you can try the following:

  • Load a model checkpoint that triggers the issue, such as PerceptronAI/Isaac-0.1.
  • Check that the @auto_docstring decorator does not crash and generates the correct documentation for the kwargs parameters.

Example code:

from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("PerceptronAI/Isaac-0.1", trust_remote_code=True)
print(tokenizer.__class__.__doc__)

This should print the correct documentation for the tokenizer class without crashing.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

Should resolve the string annotation via typing.get_type_hints() and generate kwargs documentation normally. If resolution fails, skip gracefully rather than crash.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING