typedparser 0.31.2__tar.gz → 0.32.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: typedparser
3
- Version: 0.31.2
3
+ Version: 0.32.1
4
4
  Summary: Extension for python argparse with typehints and typechecks.
5
5
  Author: simon-ging
6
6
  License: Apache-2.0
@@ -25,4 +25,4 @@ __all__ = [
25
25
  ]
26
26
 
27
27
 
28
- __version__ = "0.31.2"
28
+ __version__ = "0.32.1"
@@ -105,15 +105,19 @@ def modify_nested_object(
105
105
  modifier_fn: Callable,
106
106
  return_copy: bool = False,
107
107
  parser_class: Type[RecursorInterface] = StrictRecursor,
108
+ tuple_to_list: bool = False,
108
109
  ) -> Any:
109
110
  """
110
111
  Traverse a python object and apply a modifier function to all leaves.
111
112
 
113
+ Note: This also modifies tuples and sets.
114
+
112
115
  Args:
113
116
  d: object to modify (e.g. dict)
114
117
  modifier_fn: modifier function to apply to all leaves
115
118
  return_copy: whether to copy the object before modifying it
116
119
  parser_class: which parser to use for iterable and mapping checks
120
+ tuple_to_list: if True, convert tuples to lists
117
121
 
118
122
  Examples:
119
123
  >>> e_dict = {'a': 1, 'b': {'c': 2, 'd': 3}}
@@ -124,14 +128,19 @@ def modify_nested_object(
124
128
 
125
129
  """
126
130
  parser = parser_class()
131
+ tuple_type = list if tuple_to_list else tuple
132
+ if return_copy:
133
+ d = deepcopy(d)
127
134
 
128
135
  def _modify_nested_object(d_inner: Any, depth: int = 0) -> Any:
129
136
  recursive_fn = partial(_modify_nested_object, depth=depth + 1)
130
- if return_copy:
131
- d_inner = deepcopy(d_inner)
132
137
  if parser.is_mapping_fn(d_inner):
133
138
  for k, v in d_inner.items():
134
139
  d_inner[k] = recursive_fn(v)
140
+ elif isinstance(d_inner, tuple): # tuple does not support in-place modification
141
+ d_inner = tuple_type(recursive_fn(v) for v in d_inner)
142
+ elif isinstance(d_inner, set): # set does not support in-place modification
143
+ d_inner = set(recursive_fn(v) for v in d_inner)
135
144
  elif parser.is_iterable_fn(d_inner):
136
145
  for i, v in enumerate(d_inner):
137
146
  d_inner[i] = recursive_fn(v)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: typedparser
3
- Version: 0.31.2
3
+ Version: 0.32.1
4
4
  Summary: Extension for python argparse with typehints and typechecks.
5
5
  Author: simon-ging
6
6
  License: Apache-2.0
File without changes
File without changes
File without changes