Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: omni-us/jsonargparse
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v3.4.1
Choose a base ref
...
head repository: omni-us/jsonargparse
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v3.5.0
Choose a head ref
  • 4 commits
  • 7 files changed
  • 1 contributor

Commits on Feb 11, 2021

  1. Copy the full SHA
    5b0fe90 View commit details

Commits on Feb 12, 2021

  1. - Tuples with ellipsis are now supported #40.

    - Fixed nested tuples were not working correctly #40.
    - Added unit test for dict type.
    - Preparing for release 3.5.0.
    mauvilsa committed Feb 12, 2021
    Copy the full SHA
    c5cfadd View commit details
  2. Copy the full SHA
    6eaaaff View commit details
  3. Copy the full SHA
    0819291 View commit details
Showing with 68 additions and 11 deletions.
  1. +1 −1 .bumpversion.cfg
  2. +1 −1 .sonarcloud.properties
  3. +13 −0 CHANGELOG.rst
  4. +1 −1 jsonargparse/__init__.py
  5. +21 −7 jsonargparse/jsonschema.py
  6. +30 −0 jsonargparse_tests/jsonschema_tests.py
  7. +1 −1 setup.cfg
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 3.4.1
current_version = 3.5.0
commit = True
tag = True
tag_name = v{new_version}
2 changes: 1 addition & 1 deletion .sonarcloud.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
sonar.sources=jsonargparse
sonar.projectVersion=3.4.1
sonar.projectVersion=3.5.0
13 changes: 13 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -10,6 +10,19 @@ only be introduced in major versions with advance notice in the **Deprecated**
section of releases.


v3.5.0 (2021-02-12)
-------------------

Added
-----
- Tuples with ellipsis are now supported #40.

Fixed
^^^^^
- Using dict as type incorrectly considered as class requiring class_path.
- Nested tuples were not working correctly #40.


v3.4.1 (2021-02-03)
-------------------

2 changes: 1 addition & 1 deletion jsonargparse/__init__.py
Original file line number Diff line number Diff line change
@@ -19,4 +19,4 @@
)


__version__ = '3.4.1'
__version__ = '3.5.0'
28 changes: 21 additions & 7 deletions jsonargparse/jsonschema.py
Original file line number Diff line number Diff line change
@@ -240,9 +240,11 @@ def validate_adapt(v, subschema):
if reverse:
val = list(val)
for n, v in enumerate(val):
if n < len(subschemas) and subschemas[n] is not None:
for subschema in subschemas[n]:
val[n] = validate_adapt(v, subschema)
if len(subschemas) == 0:
break
subschema = subschemas[n if n < len(subschemas) else -1]
if subschema is not None:
val[n] = validate_adapt(v, subschema)
if not reverse:
val = tuple(val) if annotation.__origin__ in {Tuple, tuple} else set(val)

@@ -282,6 +284,9 @@ def _typing_schema(annotation):
elif _issubclass(annotation, (str, int, float)):
return annotation_to_schema(annotation), None

elif annotation == dict:
return {'type': 'object'}, None

elif not hasattr(annotation, '__origin__'):
if annotation != inspect._empty:
schema = {
@@ -313,15 +318,24 @@ def _typing_schema(annotation):
return {'anyOf': members}, union_subschemas

elif annotation.__origin__ in {Tuple, tuple}:
has_ellipsis = False
items = []
tuple_subschemas = []
for arg in annotation.__args__:
if arg == Ellipsis:
has_ellipsis = True
break
item, subschemas = ActionJsonSchema._typing_schema(arg)
items.append(item)
tuple_subschemas.append(subschemas)
#if any(a is None for a in items):
# return None, None
return {'type': 'array', 'items': items, 'minItems': len(items), 'maxItems': len(items)}, tuple_subschemas
if arg not in typesmap:
jsonvalidator = import_jsonschema('ActionJsonSchema')[1]
tuple_subschemas.append((arg, jsonvalidator(item), subschemas))
schema = {'type': 'array', 'items': items, 'minItems': len(items)}
if has_ellipsis:
schema['additionalItems'] = items[-1]
else:
schema['maxItems'] = len(items)
return schema, tuple_subschemas

elif annotation.__origin__ in {List, list, Iterable, Sequence, Set, set}:
items, subschemas = ActionJsonSchema._typing_schema(annotation.__args__[0])
30 changes: 30 additions & 0 deletions jsonargparse_tests/jsonschema_tests.py
Original file line number Diff line number Diff line change
@@ -207,6 +207,14 @@ class MyEnum(Enum):
self.assertEqual([3, MyEnum.ab], parser.parse_args(['--list2=[3, "ab"]']).list2)


def test_dict(self):
parser = ArgumentParser(error_handler=None, parse_as_dict=True)
parser.add_argument('--dict', type=dict)
self.assertEqual({}, parser.parse_args(['--dict={}'])['dict'])
self.assertEqual({'a': 1, 'b': '2'}, parser.parse_args(['--dict={"a":1, "b":"2"}'])['dict'])
self.assertRaises(ParserError, lambda: parser.parse_args(['--dict=1']))


def test_dict_union(self):
class MyEnum(Enum):
ab = 1
@@ -230,11 +238,33 @@ class MyEnum(Enum):
cfg = parser.parse_args(['--tuple=[2, "a", "b"]'])
self.assertEqual((2, 'a', 'b'), cfg.tuple)
self.assertIsInstance(cfg.tuple[1], Path)
self.assertRaises(ParserError, lambda: parser.parse_args(['--tuple=[]']))
self.assertRaises(ParserError, lambda: parser.parse_args(['--tuple=[2, "a", "b", 5]']))
self.assertRaises(ParserError, lambda: parser.parse_args(['--tuple=[2, "a"]']))
self.assertRaises(ParserError, lambda: parser.parse_args(['--tuple=["2", "a", "b"]']))


def test_nested_tuples(self):
parser = ArgumentParser(error_handler=None)
parser.add_argument('--tuple', type=Tuple[Tuple[str, str], Tuple[Tuple[int, float], Tuple[int, float]]])
cfg = parser.parse_args(['--tuple=[["foo", "bar"], [[1, 2.02], [3, 3.09]]]'])
self.assertEqual((('foo', 'bar'), ((1, 2.02), (3, 3.09))), cfg.tuple)


def test_tuple_ellipsis(self):
parser = ArgumentParser(error_handler=None)
parser.add_argument('--tuple', type=Tuple[float, ...])
self.assertEqual((1.2,), parser.parse_args(['--tuple=[1.2]']).tuple)
self.assertEqual((1.2, 3.4), parser.parse_args(['--tuple=[1.2, 3.4]']).tuple)
self.assertRaises(ParserError, lambda: parser.parse_args(['--tuple=[]']))
self.assertRaises(ParserError, lambda: parser.parse_args(['--tuple=[2, "a"]']))

parser = ArgumentParser(error_handler=None)
parser.add_argument('--tuple', type=Tuple[Tuple[str, str], Tuple[Tuple[int, float], ...]])
cfg = parser.parse_args(['--tuple=[["foo", "bar"], [[1, 2.02], [3, 3.09]]]'])
self.assertEqual((('foo', 'bar'), ((1, 2.02), (3, 3.09))), cfg.tuple)


def test_class_type(self):
parser = ArgumentParser(error_handler=None, parse_as_dict=True)
parser.add_argument('--op', type=Optional[List[Calendar]])
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@ doc =

[metadata]
name = jsonargparse
version = 3.4.1
version = 3.5.0
description = Parsing of command line options, yaml/jsonnet config files and/or environment variables based on argparse.
long_description_content_type = text/x-rst
author = Mauricio Villegas