Skip to content

Comments

Fix #508: Show full type for Optional and generic type annotations in help#646

Open
cobyfrombrooklyn-bot wants to merge 1 commit intogoogle:masterfrom
cobyfrombrooklyn-bot:fix-issue-508
Open

Fix #508: Show full type for Optional and generic type annotations in help#646
cobyfrombrooklyn-bot wants to merge 1 commit intogoogle:masterfrom
cobyfrombrooklyn-bot:fix-issue-508

Conversation

@cobyfrombrooklyn-bot
Copy link

The help text for arguments with generic type annotations like Optional[str], List[int], or Union[int, str] only showed the base type name (e.g., Type: Optional or Type: Union) without the type arguments. This happened because _GetArgType used __qualname__ which strips generic type parameters.

Additionally, Optional[str] with default=None was double-wrapped as Type: Optional[Optional] or Type: Optional[Union].

Changes

  • fire/helptext.py: _GetArgType now checks for __args__ (present on generic types like Optional[str], List[int]) and uses repr() instead of __qualname__ to preserve type arguments
  • fire/helptext.py: The Optional-wrapping logic now checks if None is already in the type string to avoid double-wrapping
  • fire/test_components_py3.py: Added get_optional_str and get_optional_str_none test methods
  • fire/helptext_test.py: Added two tests:
    • testHelpTextOptionalTypeWithDefault: Verifies Optional[str] with non-None default shows str in type
    • testHelpTextOptionalTypeWithNoneDefault: Verifies no Optional[Optional] or Optional[Union] double-wrapping

Test

Both new tests fail without the fix and pass with it. Full test suite: 262 passed, 0 failed.
Tested locally on macOS ARM (Apple Silicon), Python 3.14.

Fixes #508

The help text for arguments with generic type annotations like
Optional[str], List[int], or Union[int, str] only showed the base
type name (e.g., 'Optional' or 'Union') without the type arguments.
This was because _GetArgType used __qualname__ which doesn't include
generic type arguments.

Additionally, Optional[str] with default=None was double-wrapped as
'Optional[Optional]' or 'Optional[Union]' because the code always
wrapped in Optional when default was None, even if the annotation
already indicated optionality.

Changes:
- _GetArgType now checks for __args__ (present on generic types) and
  uses repr() instead of __qualname__ to preserve type arguments
- The Optional wrapping now checks if 'None' is already in the type
  string to avoid double-wrapping

Fixes google#508
@google-cla
Copy link

google-cla bot commented Feb 24, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optional type args are mistyped in help

1 participant