Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix for help from typing package, add test for optional and union #513

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions fire/helptext.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@

import collections
import itertools
import re
import sys
import typing

from fire import completion
from fire import custom_descriptions
Expand Down Expand Up @@ -538,12 +540,12 @@ def _GetArgType(arg, spec):
arg_type = spec.annotations[arg]
try:
if sys.version_info[0:2] >= (3, 3):
if isinstance(arg_type, typing._GenericAlias):
arg_type = re.search(r'\[(.*?)\]', repr(arg_type)).group(1)
return arg_type
return arg_type.__qualname__
return arg_type.__name__
except AttributeError:
# Some typing objects, such as typing.Union do not have either a __name__
# or __qualname__ attribute.
# repr(typing.Union[int, str]) will return ': typing.Union[int, str]'
return repr(arg_type)
return ''

Expand Down
36 changes: 36 additions & 0 deletions fire/helptext_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,42 @@ def testHelpTextFunctionWithTypesAndDefaultNone(self):
help_screen)
self.assertNotIn('NOTES', help_screen)

@testutils.skipIf(
sys.version_info[0:2] < (3, 5),
'Python < 3.5 does not support type hints.')
def testHelpTextFunctionWithTypesAndDefaultNoneFromTypingOptional(self):
component = (
tc.py3.WithDefaultsAndTypes().typing_optional_get_int) # pytype: disable=module-attr
help_screen = helptext.HelpText(
component=component,
trace=trace.FireTrace(component, name='get_int'))
self.assertIn('NAME\n get_int', help_screen)
self.assertIn('SYNOPSIS\n get_int <flags>', help_screen)
self.assertNotIn('DESCRIPTION', help_screen)
self.assertIn(
'FLAGS\n -v, --value=VALUE\n'
' Type: Optional[int]\n Default: None',
help_screen)
self.assertNotIn('NOTES', help_screen)

@testutils.skipIf(
sys.version_info[0:2] < (3, 5),
'Python < 3.5 does not support type hints.')
def testHelpTextFunctionWithTypesAndDefaultNoneFromTypingUnion(self):
component = (
tc.py3.WithDefaultsAndTypes().typing_union_get_int) # pytype: disable=module-attr
help_screen = helptext.HelpText(
component=component,
trace=trace.FireTrace(component, name='get_int'))
self.assertIn('NAME\n get_int', help_screen)
self.assertIn('SYNOPSIS\n get_int <flags>', help_screen)
self.assertNotIn('DESCRIPTION', help_screen)
self.assertIn(
'FLAGS\n -v, --value=VALUE\n'
' Type: Optional[int, str]\n Default: None',
Jemeljanov marked this conversation as resolved.
Show resolved Hide resolved
help_screen)
self.assertNotIn('NOTES', help_screen)

@testutils.skipIf(
sys.version_info[0:2] < (3, 5),
'Python < 3.5 does not support type hints.')
Expand Down
8 changes: 7 additions & 1 deletion fire/test_components_py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import asyncio
import functools
from typing import Tuple
from typing import Tuple, Optional, Union


# pylint: disable=keyword-arg-before-vararg
Expand Down Expand Up @@ -99,3 +99,9 @@ def double(self, count: float = 0) -> float:

def get_int(self, value: int = None):
return 0 if value is None else value

def typing_optional_get_int(self, value: Optional[int] = None):
return 0 if value is None else value

def typing_union_get_int(self, value: Union[int, str] = None):
return 0 if value is None else value