typedparser 0.10.6__tar.gz → 0.11.2__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.6
3
+ Version: 0.11.2
4
4
  Summary: Extension for python argparse with typehints and typechecks.
5
5
  Author: simon-ging
6
6
  License: Apache-2.0
@@ -181,7 +181,7 @@ Note: Some tests are skipped for python 3.7.
181
181
 
182
182
  ~~~bash
183
183
  pip install -e .
184
- pip install pytest pytest-cov pylint pytest-lazy-fixture
184
+ pip install pytest pytest-cov pylint
185
185
  pylint typedparser
186
186
 
187
187
  # run tests
@@ -154,7 +154,7 @@ Note: Some tests are skipped for python 3.7.
154
154
 
155
155
  ~~~bash
156
156
  pip install -e .
157
- pip install pytest pytest-cov pylint pytest-lazy-fixture
157
+ pip install pytest pytest-cov pylint
158
158
  pylint typedparser
159
159
 
160
160
  # run tests
@@ -28,5 +28,5 @@ def define(*args, **kwargs):
28
28
  )
29
29
 
30
30
 
31
- __version__ = "0.10.6"
31
+ __version__ = "0.11.2"
32
32
 
@@ -1,7 +1,4 @@
1
- import attrs
2
1
  import collections
3
- from attr import has, AttrsInstance
4
- from attrs import define, fields_dict, fields, Attribute
5
2
  from functools import partial
6
3
  from inspect import isclass
7
4
  from pathlib import Path
@@ -21,8 +18,16 @@ from typing import (
21
18
  Callable,
22
19
  )
23
20
 
21
+ import attrs
22
+ from attr import has, AttrsInstance
23
+ from attrs import define, fields_dict, fields, Attribute
24
+
24
25
  from .objects import check_object_equality, RecursorInterface, StrictRecursor, AttrsClass
25
- from types import UnionType
26
+
27
+ try:
28
+ from types import UnionType
29
+ except ImportError:
30
+ UnionType = None
26
31
 
27
32
  try:
28
33
  # python>=3.8
@@ -290,7 +295,7 @@ def _parse_nested(
290
295
  # f" depth={depth} type annotation origin={origin} args={args}"
291
296
  )
292
297
 
293
- def maybe_raise_typeerorr(full_err_msg):
298
+ def maybe_raise_typeerror(full_err_msg):
294
299
  if strict:
295
300
  raise TypeError(full_err_msg)
296
301
  _print_fn(f"Caught: {full_err_msg}. Returning as is.")
@@ -314,15 +319,20 @@ def _parse_nested(
314
319
  return value
315
320
 
316
321
  # resolve unions (mostly "optional")
317
- if origin == Union or isinstance(typ, UnionType):
322
+ if origin == Union or (UnionType is not None and isinstance(typ, UnionType)):
323
+ collected_errors = []
318
324
  for new_typ in args:
319
325
  try:
320
326
  # force strict parsing and catch errors to try all different types
321
327
  return parse_recursive(name, value, new_typ, strict=True)
322
- except TypeError:
323
- pass
328
+ except TypeError as e:
329
+ collected_errors.append(f"{e}")
324
330
  # if we get here, none of the types worked
325
- return maybe_raise_typeerorr(f"{err_msg}. No type in Union matches.")
331
+ collected_error_str = "\n".join(collected_errors)
332
+ return maybe_raise_typeerror(
333
+ f"No type in Union matches. Errors per type:\n{collected_error_str}\n"
334
+ f"Final error: {err_msg}"
335
+ )
326
336
 
327
337
  final_list_type = None
328
338
 
@@ -331,9 +341,9 @@ def _parse_nested(
331
341
  if not (len(args) == 2 and args[1] == Ellipsis):
332
342
  # resolve fixed tuple
333
343
  if not recursor.is_iterable_fn(value):
334
- return maybe_raise_typeerorr(f"{err_msg}. Expect a sequence, got {type(value)}")
344
+ return maybe_raise_typeerror(f"{err_msg}. Expect a sequence, got {type(value)}")
335
345
  if len(value) != len(args):
336
- return maybe_raise_typeerorr(
346
+ return maybe_raise_typeerror(
337
347
  f"{err_msg}. Expect a sequence with length {len(args)}, got length {len(value)}"
338
348
  )
339
349
  new_tuple = []
@@ -347,7 +357,7 @@ def _parse_nested(
347
357
  # check dict
348
358
  if origin in (dict, collections.defaultdict):
349
359
  if not recursor.is_mapping_fn(value):
350
- return maybe_raise_typeerorr(f"{err_msg}. Expect a mapping, got {type(value)}")
360
+ return maybe_raise_typeerror(f"{err_msg}. Expect a mapping, got {type(value)}")
351
361
  if len(args) == 0:
352
362
  args = [Any, Any]
353
363
  dict_keytype, dict_valuetype = args
@@ -375,7 +385,7 @@ def _parse_nested(
375
385
  # resolve list
376
386
  if origin == list:
377
387
  if not recursor.is_iterable_fn(value):
378
- return maybe_raise_typeerorr(f"{err_msg}. Expect iterable, got {type(value)}")
388
+ return maybe_raise_typeerror(f"{err_msg}. Expect iterable, got {type(value)}")
379
389
  list_arg_type = args[0]
380
390
  list_output = []
381
391
  for item in value:
@@ -404,7 +414,7 @@ def _parse_nested(
404
414
  except TypeError:
405
415
  is_sc = False
406
416
  if is_sc:
407
- return maybe_raise_typeerorr(f"{err_msg}. Abstract collections are not supported.")
417
+ return maybe_raise_typeerror(f"{err_msg}. Abstract collections are not supported.")
408
418
 
409
419
  # add explicit conversions
410
420
  for convert_source, convert_target, convert_callable in conversions:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: typedparser
3
- Version: 0.10.6
3
+ Version: 0.11.2
4
4
  Summary: Extension for python argparse with typehints and typechecks.
5
5
  Author: simon-ging
6
6
  License: Apache-2.0
@@ -181,7 +181,7 @@ Note: Some tests are skipped for python 3.7.
181
181
 
182
182
  ~~~bash
183
183
  pip install -e .
184
- pip install pytest pytest-cov pylint pytest-lazy-fixture
184
+ pip install pytest pytest-cov pylint
185
185
  pylint typedparser
186
186
 
187
187
  # run tests
File without changes
File without changes