Skip to content

Commit ea8254d

Browse files
committed
Exposing argparse ArgumentError and ArgumentTypeError, updating error types, and fixing UnionType import for Python < 3.10
1 parent 7fb1420 commit ea8254d

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

tap/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1+
from argparse import ArgumentError, ArgumentTypeError
12
from tap._version import __version__
23
from tap.tap import Tap
34

4-
__all__ = ['Tap', '__version__']
5+
__all__ = ['ArgumentError', 'ArgumentTypeError', 'Tap', '__version__']

tap/tap.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from argparse import ArgumentParser
1+
from argparse import ArgumentParser, ArgumentTypeError
22
from collections import OrderedDict
33
from copy import deepcopy
44
from functools import partial
@@ -8,7 +8,7 @@
88
from shlex import quote, split
99
import sys
1010
import time
11-
from types import MethodType, UnionType
11+
from types import MethodType
1212
from typing import Any, Callable, Dict, List, Optional, Sequence, Set, Tuple, TypeVar, Union, get_type_hints
1313
from typing_inspect import is_literal_type, get_args
1414
from warnings import warn
@@ -30,11 +30,14 @@
3030
enforce_reproducibility,
3131
)
3232

33+
if sys.version_info >= (3, 10):
34+
from types import UnionType
35+
3336

3437
# Constants
3538
EMPTY_TYPE = get_args(List)[0] if len(get_args(List)) > 0 else tuple()
3639
BOXED_COLLECTION_TYPES = {List, list, Set, set, Tuple, tuple}
37-
OPTIONAL_TYPES = {Optional, Union, UnionType}
40+
OPTIONAL_TYPES = {Optional, Union} | ({UnionType} if sys.version_info >= (3, 10) else set())
3841
BOXED_TYPES = BOXED_COLLECTION_TYPES | OPTIONAL_TYPES
3942

4043

@@ -202,8 +205,8 @@ def _add_argument(self, *name_or_flags, **kwargs) -> None:
202205

203206
# Don't allow Tuple[()]
204207
if len(types) == 1 and types[0] == tuple():
205-
raise ValueError('Empty Tuples (i.e. Tuple[()]) are not a valid Tap type '
206-
'because they have no arguments.')
208+
raise ArgumentTypeError('Empty Tuples (i.e. Tuple[()]) are not a valid Tap type '
209+
'because they have no arguments.')
207210

208211
# Handle Tuple[type, ...]
209212
if len(types) == 2 and types[1] == Ellipsis:

tap/utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import subprocess
1313
import sys
1414
import tokenize
15-
from types import UnionType
1615
from typing import (
1716
Any,
1817
Callable,
@@ -27,6 +26,8 @@
2726
from typing_extensions import Literal
2827
from typing_inspect import get_args, get_origin as typing_inspect_get_origin
2928

29+
if sys.version_info >= (3, 10):
30+
from types import UnionType
3031

3132
NO_CHANGES_STATUS = """nothing to commit, working tree clean"""
3233
PRIMITIVES = (str, int, float, bool)
@@ -256,7 +257,7 @@ def get_literals(literal: Literal, variable: str) -> Tuple[Callable[[str], Any],
256257
literals = list(get_args(literal))
257258

258259
if not all(isinstance(literal, PRIMITIVES) for literal in literals):
259-
raise ValueError(
260+
raise ArgumentTypeError(
260261
f'The type for variable "{variable}" contains a literal'
261262
f'of a non-primitive type e.g. (str, int, float, bool).\n'
262263
f'Currently only primitive-typed literals are supported.'
@@ -265,7 +266,7 @@ def get_literals(literal: Literal, variable: str) -> Tuple[Callable[[str], Any],
265266
str_to_literal = {str(literal): literal for literal in literals}
266267

267268
if len(literals) != len(str_to_literal):
268-
raise ValueError('All literals must have unique string representations')
269+
raise ArgumentTypeError('All literals must have unique string representations')
269270

270271
def var_type(arg: str) -> Any:
271272
return str_to_literal.get(arg, arg)
@@ -404,7 +405,7 @@ def as_python_object(dct: Any) -> Any:
404405
return UnpicklableObject()
405406

406407
else:
407-
raise ValueError(f'Special type "{_type}" not supported for JSON loading.')
408+
raise ArgumentTypeError(f'Special type "{_type}" not supported for JSON loading.')
408409

409410
return dct
410411

@@ -482,7 +483,7 @@ def get_origin(tp: Any) -> Any:
482483
if origin is None:
483484
origin = tp
484485

485-
if isinstance(origin, UnionType):
486+
if sys.version_info >= (3, 10) and isinstance(origin, UnionType):
486487
origin = UnionType
487488

488489
return origin

0 commit comments

Comments
 (0)