Skip to content

Commit 79a70f1

Browse files
ioangatopmauvilsa
authored andcommitted
Improved error messages when fail_untyped=True #137.
1 parent 73a2522 commit 79a70f1

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Added
1919
^^^^^
2020
- ``CLI`` support for callable class instances `#238
2121
<https://github.com/omni-us/jsonargparse/issues/238>`__.
22+
- ``add_dataclass_arguments`` now supports the ``fail_untyped`` parameter `#241
23+
<https://github.com/omni-us/jsonargparse/issues/241>`__.
2224

2325
Fixed
2426
^^^^^
@@ -27,8 +29,10 @@ Fixed
2729

2830
Changed
2931
^^^^^^^
30-
- When parsing fails now ``argparse.ArgumentError`` is raised instead of
32+
- When parsing fails, now ``argparse.ArgumentError`` is raised instead of
3133
``ParserError``.
34+
- Improved error messages when ``fail_untyped=True`` `#137
35+
<https://github.com/omni-us/jsonargparse/issues/137>`__.
3236

3337
Deprecated
3438
^^^^^^^^^^

jsonargparse/signatures.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,12 @@ def _add_signature_parameter(
362362
action.sub_add_kwargs['skip'] = subclass_skip
363363
added_args.append(dest)
364364
elif is_required and fail_untyped:
365-
raise ValueError(f'Required parameter without a type for "{src}" parameter "{name}".')
365+
msg = f'With fail_untyped=True, all mandatory parameters must have a supported type. Parameter "{name}" from "{src}" '
366+
if isinstance(annotation, str):
367+
msg += 'specifies the type as a string. Types as a string and `from __future__ import annotations` is currently not supported.'
368+
else:
369+
msg += 'does not specify a type.'
370+
raise ValueError(msg)
366371

367372

368373
def add_dataclass_arguments(

jsonargparse_tests/test_signatures.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,25 @@ def func(a1: Optional[int] = None):
955955
self.assertIsNone(parser.parse_args(['--a1=null']).a1)
956956

957957

958+
def test_fail_untyped_true(self):
959+
def func1(a1):
960+
return a1
961+
962+
parser = ArgumentParser(exit_on_error=False)
963+
with self.assertRaises(ValueError) as ctx:
964+
parser.add_function_arguments(func1, fail_untyped=True)
965+
self.assertIn('Parameter "a1" from', str(ctx.exception))
966+
self.assertIn('does not specify a type', str(ctx.exception))
967+
968+
def func2(a2: 'int'):
969+
return a2
970+
971+
with self.assertRaises(ValueError) as ctx:
972+
parser.add_function_arguments(func2, fail_untyped=True)
973+
self.assertIn('Parameter "a2" from', str(ctx.exception))
974+
self.assertIn('specifies the type as a string', str(ctx.exception))
975+
976+
958977
def test_fail_untyped_false(self):
959978

960979
def func(a1, a2=None):

0 commit comments

Comments
 (0)