Skip to content

Commit baec7c3

Browse files
committed
Updating union type error message and readme
1 parent 9845d1b commit baec7c3

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,11 @@ Literal is analagous to argparse's [choices](https://docs.python.org/3/library/a
262262
More complex types _must_ be specified with the `type` keyword argument in `add_argument`, as in the example below.
263263

264264
```python
265-
def to_number(string: str):
265+
def to_number(string: str) -> Union[float, int]:
266266
return float(string) if '.' in string else int(string)
267267

268268
class MyTap(Tap):
269-
number: Union[int, float]
269+
number: Union[float, int]
270270

271271
def configure(self):
272272
self.add_argument('--number', type=to_number)

tap/tap.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,13 @@ def _add_argument(self, *name_or_flags, **kwargs) -> None:
187187
raise ArgumentTypeError(
188188
'For Union types, you must include an explicit type function in the configure method. '
189189
'For example,\n\n'
190+
'def to_number(string: str) -> Union[float, int]:\n'
191+
' return float(string) if \'.\' in string else int(string)\n\n'
190192
'class Args(Tap):\n'
191-
' arg: Union[int, float]\n'
193+
' arg: Union[float, int]\n'
192194
'\n'
193195
' def configure(self) -> None:\n'
194-
' self.add_argument("--arg", type=lambda x: float(x) if "." in x else int(x))'
196+
' self.add_argument(\'--arg\', type=to_number)'
195197
)
196198

197199
if len(var_args) > 0:

0 commit comments

Comments
 (0)