Skip to content

Commit

Permalink
Python 3.9 support
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjm97 committed Jun 20, 2021
1 parent 25644c3 commit a4c6701
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 43 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ Tuple, Tuple[Type1, Type2, etc.], Tuple[Type, ...]
Literal
```

If you're using Python 3.9+, then you can replace `List` with `list`, `Set` with `set`, and `Tuple` with `tuple`.

#### `str`, `int`, and `float`

Each is automatically parsed to their respective types, just like argparse.
Expand Down Expand Up @@ -241,7 +243,7 @@ Tuples can be used to specify a fixed number of arguments with specified types u

#### `Literal`

Literal is analagous to `argparse`'s [choices](https://docs.python.org/3/library/argparse.html#choices), which specifies the values that an argument can take. For example, if arg can only be one of 'H', 1, False, or 1.0078 then you would specify that `arg: Literal['H', 1, False, 1.0078]`. For instance, `--arg False` assigns arg to False and `--arg True` throws error. The `Literal` type was introduced in Python 3.8 ([PEP 586](https://www.python.org/dev/peps/pep-0586/)) and can be imported with `from typing_extensions import Literal`.
Literal is analagous to argparse's [choices](https://docs.python.org/3/library/argparse.html#choices), which specifies the values that an argument can take. For example, if arg can only be one of 'H', 1, False, or 1.0078 then you would specify that `arg: Literal['H', 1, False, 1.0078]`. For instance, `--arg False` assigns arg to False and `--arg True` throws error. The `Literal` type was introduced in Python 3.8 ([PEP 586](https://www.python.org/dev/peps/pep-0586/)) and can be imported with `from typing_extensions import Literal`.

#### Complex types

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
package_data={'tap': ['py.typed']},
install_requires=[
'typing_extensions >= 3.7.4',
'typing-inspect == 0.6.0'
'typing-inspect >= 0.7.1'
],
tests_require=['pytest'],
classifiers=[
Expand Down
2 changes: 1 addition & 1 deletion tap/_version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__all__ = ['__version__']

# major, minor, patch
version_info = 1, 6, 3
version_info = 1, 7, 0

# Nice string for the version
__version__ = '.'.join(map(str, version_info))
80 changes: 40 additions & 40 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,46 +124,46 @@ def test_both_assigned_okay(self):


# TODO: need to implement list[str] etc.
# class ParameterizedStandardCollectionTap(Tap):
# arg_list_str: list[str]
# arg_list_int: list[int]
# arg_list_int_default: list[int] = [1, 2, 5]
# arg_set_float: set[float]
# arg_set_str_default: set[str] = ['one', 'two', 'five']
# arg_tuple_int: tuple[int, ...]
# arg_tuple_float_default: tuple[float, float, float] = (1.0, 2.0, 5.0)
# arg_tuple_str_override: tuple[str, str] = ('hi', 'there')
# arg_optional_list_int: Optional[list[int]] = None


# class ParameterizedStandardCollectionTests(TestCase):
# @unittest.skipIf(sys.version_info < (3, 9), 'Parameterized standard collections (e.g., list[int]) introduced in Python 3.9')
# def test_parameterized_standard_collection(self):
# arg_list_str = ['a', 'b', 'pi']
# arg_list_int = [-2, -5, 10]
# arg_set_float = {3.54, 2.235}
# arg_tuple_int = (-4, 5, 9, 103)
# arg_tuple_str_override = ('why', 'so', 'many', 'tests?')
# arg_optional_list_int = [5, 4, 3]

# args = ParameterizedStandardCollectionTap().parse_args([
# '--arg_list_str', *arg_list_str,
# '--arg_list_int', *[str(var) for var in arg_list_int],
# '--arg_set_float', *[str(var) for var in arg_set_float],
# '--arg_tuple_int', *[str(var) for var in arg_tuple_int],
# '--arg_tuple_str_override', *arg_tuple_str_override,
# '--arg_optional_list_int', *[str(var) for var in arg_optional_list_int]
# ])

# self.assertEqual(args.arg_list_str, arg_list_str)
# self.assertEqual(args.arg_list_int, arg_list_int)
# self.assertEqual(args.arg_list_int_default, ParameterizedStandardCollectionTap.arg_list_int_default)
# self.assertEqual(args.arg_set_float, arg_set_float)
# self.assertEqual(args.arg_set_str_default, ParameterizedStandardCollectionTap.arg_set_str_default)
# self.assertEqual(args.arg_tuple_int, arg_tuple_int)
# self.assertEqual(args.arg_tuple_float_default, ParameterizedStandardCollectionTap.arg_tuple_float_default)
# self.assertEqual(args.arg_tuple_str_override, arg_tuple_str_override)
# self.assertEqual(args.arg_optional_list_int, arg_optional_list_int)
class ParameterizedStandardCollectionTap(Tap):
arg_list_str: list[str]
arg_list_int: list[int]
arg_list_int_default: list[int] = [1, 2, 5]
arg_set_float: set[float]
arg_set_str_default: set[str] = {'one', 'two', 'five'}
arg_tuple_int: tuple[int, ...]
arg_tuple_float_default: tuple[float, float, float] = (1.0, 2.0, 5.0)
arg_tuple_str_override: tuple[str, str] = ('hi', 'there')
arg_optional_list_int: Optional[list[int]] = None


class ParameterizedStandardCollectionTests(TestCase):
@unittest.skipIf(sys.version_info < (3, 9), 'Parameterized standard collections (e.g., list[int]) introduced in Python 3.9')
def test_parameterized_standard_collection(self):
arg_list_str = ['a', 'b', 'pi']
arg_list_int = [-2, -5, 10]
arg_set_float = {3.54, 2.235}
arg_tuple_int = (-4, 5, 9, 103)
arg_tuple_str_override = ('why', 'so')
arg_optional_list_int = [5, 4, 3]

args = ParameterizedStandardCollectionTap().parse_args([
'--arg_list_str', *arg_list_str,
'--arg_list_int', *[str(var) for var in arg_list_int],
'--arg_set_float', *[str(var) for var in arg_set_float],
'--arg_tuple_int', *[str(var) for var in arg_tuple_int],
'--arg_tuple_str_override', *arg_tuple_str_override,
'--arg_optional_list_int', *[str(var) for var in arg_optional_list_int]
])

self.assertEqual(args.arg_list_str, arg_list_str)
self.assertEqual(args.arg_list_int, arg_list_int)
self.assertEqual(args.arg_list_int_default, ParameterizedStandardCollectionTap.arg_list_int_default)
self.assertEqual(args.arg_set_float, arg_set_float)
self.assertEqual(args.arg_set_str_default, ParameterizedStandardCollectionTap.arg_set_str_default)
self.assertEqual(args.arg_tuple_int, arg_tuple_int)
self.assertEqual(args.arg_tuple_float_default, ParameterizedStandardCollectionTap.arg_tuple_float_default)
self.assertEqual(args.arg_tuple_str_override, arg_tuple_str_override)
self.assertEqual(args.arg_optional_list_int, arg_optional_list_int)


class NestedOptionalTypesTap(Tap):
Expand Down

0 comments on commit a4c6701

Please sign in to comment.