typedparser 0.10.4__tar.gz → 0.10.5__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.10.4
3
+ Version: 0.10.5
4
4
  Summary: Extension for python argparse with typehints and typechecks.
5
5
  Author: simon-ging
6
6
  License: Apache-2.0
@@ -28,5 +28,5 @@ def define(*args, **kwargs):
28
28
  )
29
29
 
30
30
 
31
- __version__ = "0.10.4"
31
+ __version__ = "0.10.5"
32
32
 
@@ -1,4 +1,7 @@
1
+ import attrs
1
2
  import collections
3
+ from attr import has, AttrsInstance
4
+ from attrs import define, fields_dict, fields, Attribute
2
5
  from functools import partial
3
6
  from inspect import isclass
4
7
  from pathlib import Path
@@ -15,13 +18,9 @@ from typing import (
15
18
  List,
16
19
  Optional,
17
20
  get_type_hints,
18
- Callable,
21
+ Callable,
19
22
  )
20
23
 
21
- import attrs
22
- from attr import has, AttrsInstance
23
- from attrs import define, fields_dict, fields, Attribute
24
-
25
24
  from .objects import check_object_equality, RecursorInterface, StrictRecursor, AttrsClass
26
25
 
27
26
  try:
@@ -133,7 +132,10 @@ def _attrs_from_dict(
133
132
  skip_unknowns: bool = False,
134
133
  conversions: Optional[conversion_type] = None,
135
134
  more_error_info: str = "",
135
+ current_position: Optional[List] = None,
136
136
  ):
137
+ if current_position is None:
138
+ current_position = []
137
139
  _print_fn(f"Parsing {cls} from {input_dict_or_attrs}")
138
140
  input_cls = type(input_dict_or_attrs)
139
141
  if has(input_cls):
@@ -173,7 +175,7 @@ def _attrs_from_dict(
173
175
  for field_name, field_value in matching_input.items():
174
176
  field_att = cls_fields_dict[field_name]
175
177
  try:
176
- is_positional = bool(field_att.default == attrs.NOTHING)
178
+ is_positional = bool(field_att.default == attrs.NOTHING) and not field_att.kw_only
177
179
  except ValueError:
178
180
  # some field defaults, e.g. numpy arrays do not have comparison defined here
179
181
  # but that means that the default is set and the field is not positional
@@ -185,7 +187,17 @@ def _attrs_from_dict(
185
187
 
186
188
  # create an attrs instance from the dict
187
189
  # the instance will be flat (nested dicts are not resolved yet) and not typechecked.
188
- attrs_inst = attrs.evolve(cls(*in_args), **in_kwargs) # noqa
190
+ try:
191
+ cls_inst = cls(*in_args, **in_kwargs) # noqa
192
+ attrs_inst = cls_inst
193
+ # # not sure why evolve was used before
194
+ # attrs_inst = attrs.evolve(cls_inst, **in_kwargs) # noqa
195
+ except TypeError as e:
196
+ raise TypeError(
197
+ f"Failed creating instance of {cls} from input {input_dict} "
198
+ f"with args {in_args} and kwargs {in_kwargs}. Error occurred at: "
199
+ f"{'.'.join(current_position)}. {e}\n\nError context: {more_error_info}."
200
+ ) from e
189
201
 
190
202
  # typecheck and unfold nested values in the attrs instance
191
203
  type_hints = get_type_hints(cls)
@@ -205,6 +217,7 @@ def _attrs_from_dict(
205
217
  skip_unknowns=skip_unknowns,
206
218
  conversions=conversions,
207
219
  more_error_info=more_error_info,
220
+ current_position=current_position + [name],
208
221
  )
209
222
  setattr(attrs_inst, name, new_value)
210
223
 
@@ -242,14 +255,17 @@ def _parse_nested(
242
255
  conversions: conversion_type = None,
243
256
  depth: int = 0,
244
257
  more_error_info: str = "",
258
+ current_position: Optional[List] = None,
245
259
  ):
246
260
  conversions = default_conversions if conversions is None else conversions
247
261
  parse_recursive = partial(
248
262
  _parse_nested,
249
263
  recursor,
250
- depth=depth + 1,
251
264
  skip_unknowns=skip_unknowns,
265
+ conversions=conversions,
266
+ depth=depth + 1,
252
267
  more_error_info=more_error_info,
268
+ current_position=current_position,
253
269
  )
254
270
 
255
271
  if isinstance(typ, str):
@@ -288,6 +304,8 @@ def _parse_nested(
288
304
  strict=strict,
289
305
  skip_unknowns=skip_unknowns,
290
306
  conversions=conversions,
307
+ more_error_info=more_error_info,
308
+ current_position=current_position,
291
309
  )
292
310
 
293
311
  # resolve any
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: typedparser
3
- Version: 0.10.4
3
+ Version: 0.10.5
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