pythonwrench 0.6.8__py3-none-any.whl → 0.6.9__py3-none-any.whl

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.
Files changed (57) hide show
  1. pythonwrench/__init__.py +15 -4
  2. pythonwrench/__main__.py +0 -1
  3. pythonwrench/_core.py +7 -13
  4. pythonwrench/abc.py +0 -1
  5. pythonwrench/argparse/__init__.py +0 -1
  6. pythonwrench/argparse/dataclass_.py +93 -41
  7. pythonwrench/argparse/parsers.py +24 -40
  8. pythonwrench/cast.py +1 -15
  9. pythonwrench/checksum.py +0 -4
  10. pythonwrench/collections/__init__.py +0 -1
  11. pythonwrench/collections/collections.py +7 -39
  12. pythonwrench/collections/prop.py +0 -3
  13. pythonwrench/collections/reducers.py +0 -24
  14. pythonwrench/concurrent.py +0 -1
  15. pythonwrench/csv.py +0 -1
  16. pythonwrench/dataclasses.py +70 -4
  17. pythonwrench/datetime.py +7 -5
  18. pythonwrench/difflib.py +0 -1
  19. pythonwrench/disk_cache.py +1 -23
  20. pythonwrench/entrypoints/info.py +2 -1
  21. pythonwrench/entrypoints/safe_rmdir.py +45 -57
  22. pythonwrench/entrypoints/tree.py +26 -45
  23. pythonwrench/enum.py +0 -1
  24. pythonwrench/functools.py +0 -9
  25. pythonwrench/hashlib.py +40 -10
  26. pythonwrench/importlib.py +14 -8
  27. pythonwrench/inspect.py +0 -1
  28. pythonwrench/json.py +0 -1
  29. pythonwrench/jsonl.py +0 -1
  30. pythonwrench/logging.py +2 -3
  31. pythonwrench/math.py +0 -2
  32. pythonwrench/os.py +8 -7
  33. pythonwrench/pickle.py +0 -1
  34. pythonwrench/random.py +0 -3
  35. pythonwrench/re.py +5 -2
  36. pythonwrench/semver.py +29 -13
  37. pythonwrench/serialization/__init__.py +17 -2
  38. pythonwrench/serialization/_core.py +353 -14
  39. pythonwrench/serialization/csv.py +188 -207
  40. pythonwrench/serialization/json.py +128 -95
  41. pythonwrench/serialization/jsonl.py +109 -97
  42. pythonwrench/serialization/pickle.py +96 -96
  43. pythonwrench/serialization/yaml/__init__.py +48 -0
  44. pythonwrench/serialization/yaml/_yaml_fallback.py +63 -0
  45. pythonwrench/serialization/yaml/definitions.py +81 -0
  46. pythonwrench/serialization/yaml/func.py +318 -0
  47. pythonwrench/time.py +0 -1
  48. pythonwrench/typing/__init__.py +0 -1
  49. pythonwrench/typing/checks.py +27 -7
  50. pythonwrench/typing/classes.py +0 -1
  51. pythonwrench/warnings.py +0 -1
  52. {pythonwrench-0.6.8.dist-info → pythonwrench-0.6.9.dist-info}/METADATA +8 -5
  53. pythonwrench-0.6.9.dist-info/RECORD +56 -0
  54. {pythonwrench-0.6.8.dist-info → pythonwrench-0.6.9.dist-info}/WHEEL +1 -1
  55. pythonwrench-0.6.8.dist-info/RECORD +0 -52
  56. {pythonwrench-0.6.8.dist-info → pythonwrench-0.6.9.dist-info}/entry_points.txt +0 -0
  57. {pythonwrench-0.6.8.dist-info → pythonwrench-0.6.9.dist-info}/licenses/LICENSE +0 -0
pythonwrench/__init__.py CHANGED
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
2
 
4
3
  """Python library with tools for typing, manipulating collections, and more!"""
5
4
 
@@ -9,7 +8,7 @@ __author_email__ = "labbeti.pub@gmail.com"
9
8
  __license__ = "MIT"
10
9
  __maintainer__ = "Étienne Labbé (Labbeti)"
11
10
  __status__ = "Development"
12
- __version__ = "0.6.8"
11
+ __version__ = "0.6.9"
13
12
 
14
13
 
15
14
  from typing import TYPE_CHECKING
@@ -127,7 +126,7 @@ if TYPE_CHECKING or lazy is None:
127
126
  identity,
128
127
  repeat_fn,
129
128
  )
130
- from .hashlib import hash_file
129
+ from .hashlib import hash_bytes, hash_bytes_buffer, hash_file, hash_str
131
130
  from .importlib import (
132
131
  ModulePlaceholder,
133
132
  Placeholder,
@@ -177,26 +176,32 @@ if TYPE_CHECKING or lazy is None:
177
176
  dump_json,
178
177
  dump_jsonl,
179
178
  dump_pickle,
179
+ dump_yaml,
180
180
  dumps_csv,
181
181
  dumps_json,
182
182
  dumps_jsonl,
183
183
  dumps_pickle,
184
+ dumps_yaml,
184
185
  load_csv,
185
186
  load_json,
186
187
  load_jsonl,
187
188
  load_pickle,
189
+ load_yaml,
188
190
  loads_csv,
189
191
  loads_json,
190
192
  loads_jsonl,
191
193
  loads_pickle,
194
+ loads_yaml,
192
195
  read_csv,
193
196
  read_json,
194
197
  read_jsonl,
195
198
  read_pickle,
199
+ read_yaml,
196
200
  save_csv,
197
201
  save_json,
198
202
  save_jsonl,
199
203
  save_pickle,
204
+ save_yaml,
200
205
  )
201
206
  from .time import Ticker
202
207
  from .typing import (
@@ -373,6 +378,12 @@ else:
373
378
  "save_json",
374
379
  "save_jsonl",
375
380
  "save_pickle",
381
+ "dump_yaml",
382
+ "dumps_yaml",
383
+ "load_yaml",
384
+ "loads_yaml",
385
+ "read_yaml",
386
+ "save_yaml",
376
387
  ],
377
388
  "dataclasses": ["add_dict_methods", "dataclassdict", "get_defaults_values"],
378
389
  "datetime": ["get_now", "get_now_iso8601"],
@@ -387,7 +398,7 @@ else:
387
398
  "identity",
388
399
  "repeat_fn",
389
400
  ],
390
- "hashlib": ["hash_file"],
401
+ "hashlib": ["hash_file", "hash_bytes", "hash_bytes_buffer", "hash_str"],
391
402
  "importlib": [
392
403
  "import_if_available",
393
404
  "ModulePlaceholder",
pythonwrench/__main__.py CHANGED
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
2
 
4
3
  from .entrypoints.info import main_info
5
4
 
pythonwrench/_core.py CHANGED
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
2
 
4
3
  import warnings
5
4
  from functools import lru_cache, partial, wraps
@@ -42,7 +41,7 @@ class Predicate(Protocol[T_PredicateArg]):
42
41
 
43
42
  def return_none(*args, **kwargs) -> None:
44
43
  """Return None function placeholder."""
45
- return None
44
+ return
46
45
 
47
46
 
48
47
  def function_alias(
@@ -162,18 +161,17 @@ class _FunctionRegistry(Generic[T_Output]):
162
161
  unk_mode: UnkMode = "error",
163
162
  **kwargs,
164
163
  ) -> T_Output:
165
- """Perform the apply operation."""
164
+ def wrapped_isinstance(x: Any, target_cls_or_tuple: Any) -> bool:
165
+ return isinstance_fn(x, target_cls_or_tuple)
166
+
166
167
  for fn, (class_or_tuple, custom_predicate, _) in self.fns.items():
167
168
  if custom_predicate is not None:
168
169
  predicate = custom_predicate
169
170
 
170
171
  elif class_or_tuple is not None:
171
-
172
- def target_isinstance_fn_wrap(x: Any) -> bool:
173
- """Perform the target isinstance fn wrap operation."""
174
- return isinstance_fn(x, class_or_tuple) # type: ignore
175
-
176
- predicate = target_isinstance_fn_wrap
172
+ predicate = partial(
173
+ wrapped_isinstance, target_cls_or_tuple=class_or_tuple
174
+ )
177
175
  else:
178
176
  msg = f"Invalid function registered. (found {class_or_tuple=} and {custom_predicate=})"
179
177
  raise TypeError(msg)
@@ -229,7 +227,6 @@ def warn_once(
229
227
  source: Any = None,
230
228
  ) -> None:
231
229
  """Warn message once using warnings module."""
232
- ...
233
230
 
234
231
 
235
232
  @overload
@@ -240,7 +237,6 @@ def warn_once(
240
237
  source: Any = None,
241
238
  ) -> None:
242
239
  """Warn message once using warnings module."""
243
- ...
244
240
 
245
241
 
246
242
  @lru_cache(maxsize=None)
@@ -285,7 +281,6 @@ def deprecated_function(
285
281
  warn_fn: Callable[[str], Any] = partial(warn_once, category=DeprecationWarning),
286
282
  ) -> Callable[[T_Function], T_Function]:
287
283
  """Perform the deprecated function operation."""
288
- ...
289
284
 
290
285
 
291
286
  @overload
@@ -297,7 +292,6 @@ def deprecated_function(
297
292
  warn_fn: Callable[[str], Any] = partial(warn_once, category=DeprecationWarning),
298
293
  ) -> T_Function:
299
294
  """Perform the deprecated function operation."""
300
- ...
301
295
 
302
296
 
303
297
  def deprecated_function(
pythonwrench/abc.py CHANGED
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
2
 
4
3
  from typing import Any, ClassVar, Dict, Type
5
4
 
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
2
 
4
3
  from typing import TYPE_CHECKING
5
4
 
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
2
 
4
3
  import copy
5
4
  from argparse import ArgumentParser, Namespace
@@ -21,9 +20,9 @@ from typing import (
21
20
 
22
21
  from pythonwrench.argparse.parsers import (
23
22
  ListParsing,
23
+ _has_literal_origin,
24
+ _has_union_origin,
24
25
  _is_iterable_type_like,
25
- _is_literal_type,
26
- _is_union_type,
27
26
  _search_parse_fn,
28
27
  )
29
28
  from pythonwrench.functools import filter_and_call
@@ -56,13 +55,14 @@ BoolAction = Union[_BoolActionName, Type[BooleanOptionalAction]]
56
55
  def parse_args_using_dataclass(
57
56
  dataclass_type: Type[DataclassInstance],
58
57
  *dataclass_types: Type[DataclassInstance],
59
- args: Optional[Iterable[str]] = None,
58
+ argv: Optional[Iterable[str]] = None,
60
59
  parser: Optional[ArgumentParser] = None,
61
60
  list_parsing: ListParsing = "argparse",
62
61
  bool_action: BoolAction = "store",
63
62
  add_dashed_arg: bool = True,
64
63
  return_single_namespace: Literal[True],
65
64
  posonly_for_required: bool = False,
65
+ positionals: Iterable[str] = (),
66
66
  ) -> Namespace: ...
67
67
 
68
68
 
@@ -70,13 +70,14 @@ def parse_args_using_dataclass(
70
70
  def parse_args_using_dataclass(
71
71
  dataclass_type: Type[T_DataclassInstance],
72
72
  *,
73
- args: Optional[Iterable[str]] = None,
73
+ argv: Optional[Iterable[str]] = None,
74
74
  parser: Optional[ArgumentParser] = None,
75
75
  list_parsing: ListParsing = "argparse",
76
76
  bool_action: BoolAction = "store",
77
77
  add_dashed_arg: bool = True,
78
78
  return_single_namespace: Literal[False] = False,
79
79
  posonly_for_required: bool = False,
80
+ positionals: Iterable[str] = (),
80
81
  ) -> T_DataclassInstance: ...
81
82
 
82
83
 
@@ -86,13 +87,14 @@ def parse_args_using_dataclass(
86
87
  dataclass_type_2: Type[T_DataclassInstance_2],
87
88
  /,
88
89
  *,
89
- args: Optional[Iterable[str]] = None,
90
+ argv: Optional[Iterable[str]] = None,
90
91
  parser: Optional[ArgumentParser] = None,
91
92
  list_parsing: ListParsing = "argparse",
92
93
  bool_action: BoolAction = "store",
93
94
  add_dashed_arg: bool = True,
94
95
  return_single_namespace: Literal[False] = False,
95
96
  posonly_for_required: bool = False,
97
+ positionals: Iterable[str] = (),
96
98
  ) -> Tuple[
97
99
  T_DataclassInstance,
98
100
  T_DataclassInstance_2,
@@ -106,13 +108,14 @@ def parse_args_using_dataclass(
106
108
  dataclass_type_3: Type[T_DataclassInstance_3],
107
109
  /,
108
110
  *,
109
- args: Optional[Iterable[str]] = None,
111
+ argv: Optional[Iterable[str]] = None,
110
112
  parser: Optional[ArgumentParser] = None,
111
113
  list_parsing: ListParsing = "argparse",
112
114
  bool_action: BoolAction = "store",
113
115
  add_dashed_arg: bool = True,
114
116
  return_single_namespace: Literal[False] = False,
115
117
  posonly_for_required: bool = False,
118
+ positionals: Iterable[str] = (),
116
119
  ) -> Tuple[
117
120
  T_DataclassInstance,
118
121
  T_DataclassInstance_2,
@@ -128,13 +131,14 @@ def parse_args_using_dataclass(
128
131
  dataclass_type_4: Type[T_DataclassInstance_4],
129
132
  /,
130
133
  *,
131
- args: Optional[Iterable[str]] = None,
134
+ argv: Optional[Iterable[str]] = None,
132
135
  parser: Optional[ArgumentParser] = None,
133
136
  list_parsing: ListParsing = "argparse",
134
137
  bool_action: BoolAction = "store",
135
138
  add_dashed_arg: bool = True,
136
139
  return_single_namespace: Literal[False] = False,
137
140
  posonly_for_required: bool = False,
141
+ positionals: Iterable[str] = (),
138
142
  ) -> Tuple[
139
143
  T_DataclassInstance,
140
144
  T_DataclassInstance_2,
@@ -152,13 +156,14 @@ def parse_args_using_dataclass(
152
156
  dataclass_type_5: Type[T_DataclassInstance_5],
153
157
  /,
154
158
  *,
155
- args: Optional[Iterable[str]] = None,
159
+ argv: Optional[Iterable[str]] = None,
156
160
  parser: Optional[ArgumentParser] = None,
157
161
  list_parsing: ListParsing = "argparse",
158
162
  bool_action: BoolAction = "store",
159
163
  add_dashed_arg: bool = True,
160
164
  return_single_namespace: Literal[False] = False,
161
165
  posonly_for_required: bool = False,
166
+ positionals: Iterable[str] = (),
162
167
  ) -> Tuple[
163
168
  T_DataclassInstance,
164
169
  T_DataclassInstance_2,
@@ -171,13 +176,14 @@ def parse_args_using_dataclass(
171
176
  def parse_args_using_dataclass(
172
177
  dataclass_type: Dataclass,
173
178
  *dataclass_types: Dataclass,
174
- args: Optional[Iterable[str]] = None,
179
+ argv: Optional[Iterable[str]] = None,
175
180
  parser: Optional[ArgumentParser] = None,
176
181
  list_parsing: ListParsing = "argparse",
177
182
  bool_action: BoolAction = "store",
178
183
  add_dashed_arg: bool = True,
179
184
  return_single_namespace: bool = False,
180
185
  posonly_for_required: bool = False,
186
+ positionals: Iterable[str] = (),
181
187
  ) -> Union[
182
188
  DataclassInstance,
183
189
  Tuple[DataclassInstance, ...],
@@ -186,6 +192,22 @@ def parse_args_using_dataclass(
186
192
  """Converts prog args to a typed dataclass using argparse.
187
193
 
188
194
  Currently only supports dataclasses that contains only builtin scalars: str, int, float, None, bool OR list of builtin scalars.
195
+
196
+ Args:
197
+ dataclass_type: Target dataclass type to convert to.
198
+ *dataclass_type: Others dataclass types.
199
+ argv: Arguments value given. If None, use sys.argv arguments.
200
+ parser: Optional ArgumentParser to update and parse.
201
+ list_parsing: Defines how Iterables should be handled.
202
+ If "argparse", it uses nargs="*", which follows argparse convention.
203
+ If "brackets", it excepts 1 arguments surrounded by squared brackets e.g. [a,b]
204
+ bool_action: Define how boolean are handled.
205
+ "store": boolean value mlust be explicitely provided with "--arg true" or "--arg false".
206
+ add_dashed_arg: If argument name has underscore, add dashed variant.
207
+ e.g. --arg_number/--arg-number.
208
+ return_single_namespace: If True, avoid parsing argv to dataclass and return argparse.Namespace instance.
209
+ posonly_for_required: If True, make all required arguments positional by default.
210
+ positionals: List of fields considered as positional.
189
211
  """
190
212
  init_parser = parser
191
213
  dataclass_types = (dataclass_type,) + dataclass_types
@@ -199,12 +221,13 @@ def parse_args_using_dataclass(
199
221
  bool_action=bool_action,
200
222
  add_dashed_arg=add_dashed_arg,
201
223
  posonly_for_required=posonly_for_required,
224
+ positionals=positionals,
202
225
  )
203
226
  assert parser is not None
204
227
 
205
- parsed, other_args = parser.parse_known_args(args)
228
+ parsed, other_args = parser.parse_known_args(argv)
206
229
  if len(other_args) > 0:
207
- msg = f"Found {len(other_args)} unknown arguments: {other_args}. (with source {args=})"
230
+ msg = f"Found {len(other_args)} unknown arguments: {other_args}. (with source {argv=})"
208
231
  raise ValueError(msg)
209
232
 
210
233
  if return_single_namespace:
@@ -259,7 +282,7 @@ def _instantiate_dataclasses(
259
282
  # it must not replace the dataclass field's default/default_factory.
260
283
  continue
261
284
 
262
- if _is_union_type(dcls_union):
285
+ if _has_union_origin(dcls_union):
263
286
  dclasses = get_args(dcls_union)
264
287
  dclasses_names = {dcls.__name__: dcls for dcls in dclasses}
265
288
  try:
@@ -300,41 +323,44 @@ def _instantiate_dataclasses(
300
323
  def add_dataclass_fields_to_parser(
301
324
  dataclass_type: Type[T_DataclassInstance],
302
325
  *,
303
- parser: Optional[ArgumentParser],
326
+ parser: Optional[ArgumentParser] = None,
304
327
  list_parsing: ListParsing = "argparse",
305
328
  bool_action: BoolAction = "store",
306
329
  add_dashed_arg: bool = True,
307
330
  posonly_for_required: bool = False,
308
331
  argprefix: str = "",
332
+ positionals: Iterable[str] = (),
309
333
  ) -> ArgumentParser:
310
334
  """Perform the add dataclass fields to parser operation."""
311
- if posonly_for_required:
312
- prefix = argprefix
313
- else:
314
- prefix = f"--{argprefix}"
315
-
316
335
  if parser is None:
317
336
  parser = ArgumentParser()
337
+ positionals = dict.fromkeys(positionals)
318
338
 
319
339
  for field in fields(dataclass_type):
320
- kwds = {}
321
- posargs = [f"{prefix}{field.name}"]
322
- if add_dashed_arg and "_" in field.name:
323
- dashed_arg_name = field.name.replace("_", "-")
324
- posargs.append(f"{prefix}{dashed_arg_name}")
340
+ help = getattr(field, "doc", None)
341
+ if help is None and isinstance(field.metadata.get("doc"), str):
342
+ help = field.metadata["doc"]
343
+
344
+ kwds: dict[str, Any] = {
345
+ "help": help,
346
+ }
325
347
 
326
348
  if field.default is MISSING and field.default_factory is MISSING:
327
349
  if bool_action != "store" and field.type is bool:
328
350
  msg = f"Invalid arguments: boolean '{field.name}' without default value is incompatible with {bool_action=}."
329
351
  raise RuntimeError(msg)
330
- kwds["required"] = True
352
+ required = True
331
353
 
332
354
  elif field.default is not MISSING:
333
355
  kwds["default"] = field.default
356
+ required = False
357
+
334
358
  elif field.default_factory is not MISSING:
335
359
  kwds["default"] = field.default_factory()
360
+ required = False
361
+
336
362
  else:
337
- msg = f"Invalid field '{field.name}': found values for default and default_factory."
363
+ msg = f"Invalid field '{field.name}': found values for default and default_factory for the same field."
338
364
  raise ValueError(msg)
339
365
 
340
366
  if is_dataclass_type(field.type):
@@ -344,10 +370,47 @@ def add_dataclass_fields_to_parser(
344
370
  else:
345
371
  subclasses = None
346
372
 
347
- if subclasses is not None:
373
+ is_positional_only = (required and posonly_for_required) or (
374
+ field.name in positionals
375
+ )
376
+
377
+ kw_only = getattr(field, "kw_only", False)
378
+ if is_positional_only and kw_only:
379
+ msg = f"Incompatible arguments for '{field.name}': found {required=}, {posonly_for_required=} but {kw_only=}."
380
+ raise ValueError(msg)
381
+
382
+ if not is_positional_only:
383
+ # note: required option is invalid for posonly arg
384
+ kwds["required"] = required
385
+
386
+ if subclasses is None:
387
+ if is_positional_only:
388
+ prefix = argprefix
389
+ else:
390
+ prefix = f"--{argprefix}"
391
+
392
+ posargs = [f"{prefix}{field.name}"]
393
+ if add_dashed_arg and "_" in field.name:
394
+ dashed_arg_name = field.name.replace("_", "-")
395
+ posargs.append(f"{prefix}{dashed_arg_name}")
396
+
397
+ try:
398
+ inner_kwds = _get_kwds_for_type(field.type, list_parsing, bool_action)
399
+ kwds.update(inner_kwds)
400
+ except (ValueError, TypeError, RuntimeError) as err:
401
+ msg = f"Invalid field '{field.name}': field type '{field.type}' is not supported."
402
+ raise type(err)(msg) from err
403
+
404
+ if is_positional_only and "nargs" not in kwds and not required:
405
+ kwds["nargs"] = "?"
406
+
407
+ parser.add_argument(*posargs, **kwds)
408
+
409
+ else:
348
410
  subparsers = parser.add_subparsers(
349
411
  dest=f"{argprefix}{field.name}",
350
412
  required=kwds.get("required", False),
413
+ help=kwds.get("help", None),
351
414
  )
352
415
  for subcls in subclasses:
353
416
  subparser = subparsers.add_parser(subcls.__name__)
@@ -359,19 +422,9 @@ def add_dataclass_fields_to_parser(
359
422
  add_dashed_arg=add_dashed_arg,
360
423
  posonly_for_required=posonly_for_required,
361
424
  argprefix=f"{argprefix}{field.name}.",
425
+ positionals=positionals,
362
426
  )
363
427
 
364
- else:
365
- try:
366
- inner_kwds = _get_kwds_for_type(field.type, list_parsing, bool_action)
367
- except (ValueError, TypeError, RuntimeError) as err:
368
- msg = f"Invalid field '{field.name}': field type '{field.type}' is not supported."
369
- raise type(err)(msg) from err
370
-
371
- kwds.update(inner_kwds)
372
-
373
- parser.add_argument(*posargs, **kwds)
374
-
375
428
  return parser
376
429
 
377
430
 
@@ -403,7 +456,7 @@ def _get_kwds_for_type(
403
456
 
404
457
  if parse_fn is not None:
405
458
  kwds["type"] = parse_fn
406
- if _is_literal_type(field_type):
459
+ if _has_literal_origin(field_type):
407
460
  kwds["choices"] = get_args(field_type)
408
461
  return kwds
409
462
 
@@ -419,7 +472,7 @@ def _is_dataclass_or_union_of_dataclasses(x: Any) -> bool:
419
472
 
420
473
 
421
474
  def _is_union_of_dataclasses(x: Any) -> bool:
422
- return _is_union_type(x) and all(
475
+ return _has_union_origin(x) and all(
423
476
  is_dataclass_type(subcls) for subcls in get_args(x)
424
477
  )
425
478
 
@@ -428,4 +481,3 @@ def _is_union_of_dataclasses(x: Any) -> bool:
428
481
  @deprecated_alias(add_dataclass_fields_to_parser)
429
482
  def new_parser_from_dataclass(*args, **kwargs):
430
483
  """Perform the new parser from dataclass operation."""
431
- ...
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env python
2
- # -*- coding: utf-8 -*-
3
2
 
4
3
  import re
5
4
  from collections.abc import Iterable as _RuntimeIterable
@@ -23,6 +22,7 @@ from typing import (
23
22
  )
24
23
 
25
24
  from pythonwrench._core import Predicate
25
+ from pythonwrench.typing.checks import _is_union_type
26
26
  from pythonwrench.typing.classes import NoneType, UnionType
27
27
  from pythonwrench.warnings import deprecated_alias
28
28
 
@@ -52,7 +52,6 @@ def register_parser_fn(
52
52
  fn: None = None,
53
53
  ) -> Callable[[T_Callable], T_Callable]:
54
54
  """Perform the register parser fn operation."""
55
- ...
56
55
 
57
56
 
58
57
  @overload
@@ -61,7 +60,6 @@ def register_parser_fn(
61
60
  fn: T_Callable,
62
61
  ) -> T_Callable:
63
62
  """Perform the register parser fn operation."""
64
- ...
65
63
 
66
64
 
67
65
  def register_parser_fn(
@@ -69,7 +67,6 @@ def register_parser_fn(
69
67
  fn: Optional[Callable] = None,
70
68
  ) -> Callable:
71
69
  """Perform the register parser fn operation."""
72
- global _PARSER_REGISTRY
73
70
  if type_ is None:
74
71
  type_ = NoneType
75
72
 
@@ -128,14 +125,14 @@ def get_parse_fn(
128
125
 
129
126
  Intended for argparse arguments.
130
127
  """
131
- kwds = dict(
132
- case_sensitive=case_sensitive,
133
- true_values=true_values,
134
- false_values=false_values,
135
- none_values=none_values,
136
- list_parsing=list_parsing,
137
- handle_exception=handle_exception,
138
- )
128
+ kwds = {
129
+ "case_sensitive": case_sensitive,
130
+ "true_values": true_values,
131
+ "false_values": false_values,
132
+ "none_values": none_values,
133
+ "list_parsing": list_parsing,
134
+ "handle_exception": handle_exception,
135
+ }
139
136
  parse_fn = _search_parse_fn(type_, **kwds)
140
137
 
141
138
  if parse_fn is None:
@@ -161,7 +158,7 @@ def _search_parse_fn(type_: TargetType[T], **kwds) -> Optional[Callable[[str], T
161
158
  break
162
159
  else:
163
160
  msg = f"Invalid argument {type_or_pred_i=}. (excepted type or predicate function)"
164
- raise ValueError(msg)
161
+ raise TypeError(msg)
165
162
 
166
163
  if parse_fn is not None:
167
164
  parse_fn = partial(parse_fn, **kwds)
@@ -251,6 +248,18 @@ def _parse_to_str(x: str, **kwds) -> str:
251
248
  return x
252
249
 
253
250
 
251
+ def _has_literal_origin(x: Any) -> bool:
252
+ """Perform the is literal type operation."""
253
+ origin = get_origin(x)
254
+ return origin is Literal
255
+
256
+
257
+ def _has_union_origin(x: Any) -> bool:
258
+ """Perform the is union type operation."""
259
+ origin = get_origin(x)
260
+ return _is_union_type(origin)
261
+
262
+
254
263
  def _is_enum_type(x: Any, **kwds) -> bool:
255
264
  """Perform the is enum type operation."""
256
265
  return isinstance(x, type) and issubclass(x, Enum)
@@ -261,26 +270,11 @@ def _is_iterable_type_like(x: Any) -> bool:
261
270
  return any(xi in (list, Iterable, _RuntimeIterable) for xi in (x, get_origin(x)))
262
271
 
263
272
 
264
- def _is_literal_type(x: Any) -> bool:
265
- """Perform the is literal type operation."""
266
- origin = get_origin(x)
267
- return origin is Literal
268
-
269
-
270
273
  def _is_optional_type(x: Any) -> bool:
271
274
  """Perform the is optional type operation."""
272
275
  return getattr(x, "__name__", None) == "Optional"
273
276
 
274
277
 
275
- def _is_union_type(x: Any) -> bool:
276
- """Perform the is union type operation."""
277
- origin = get_origin(x)
278
- return origin == Union or getattr(origin, "__name__", None) in (
279
- "Union",
280
- "UnionType",
281
- )
282
-
283
-
284
278
  def _is_enum_for_parsing(x: Any, **kwds) -> bool:
285
279
  """Perform the is enum for parsing operation."""
286
280
  return _is_enum_type(x)
@@ -298,7 +292,7 @@ def _is_iterable_type_like_for_parsing(
298
292
 
299
293
  def _is_literal_for_parsing(x: Any, **kwds) -> bool:
300
294
  """Perform the is literal for parsing operation."""
301
- return _is_literal_type(x)
295
+ return _has_literal_origin(x)
302
296
 
303
297
 
304
298
  def _is_optional_for_parsing(x: Any, **kwds) -> bool:
@@ -308,7 +302,7 @@ def _is_optional_for_parsing(x: Any, **kwds) -> bool:
308
302
 
309
303
  def _is_union_for_parsing(x: Any, **kwds) -> bool:
310
304
  """Perform the is union for parsing operation."""
311
- return _is_union_type(x)
305
+ return _has_union_origin(x)
312
306
 
313
307
 
314
308
  @register_parser_fn(_is_enum_for_parsing)
@@ -562,58 +556,48 @@ def _str_in(
562
556
  @deprecated_alias(get_parse_fn)
563
557
  def parse_to(*args, **kwds):
564
558
  """Parse to."""
565
- ...
566
559
 
567
560
 
568
561
  @deprecated_alias(parse_to_type)
569
562
  def str_to_type(*args, **kwds):
570
563
  """Perform the str to type operation."""
571
- ...
572
564
 
573
565
 
574
566
  @deprecated_alias(parse_to_bool)
575
567
  def str_to_bool(*args, **kwds):
576
568
  """Perform the str to bool operation."""
577
- ...
578
569
 
579
570
 
580
571
  @deprecated_alias(parse_to_float)
581
572
  def str_to_float(*args, **kwds):
582
573
  """Perform the str to float operation."""
583
- ...
584
574
 
585
575
 
586
576
  @deprecated_alias(parse_to_int)
587
577
  def str_to_int(*args, **kwds):
588
578
  """Perform the str to int operation."""
589
- ...
590
579
 
591
580
 
592
581
  @deprecated_alias(parse_to_none)
593
582
  def str_to_none(*args, **kwds):
594
583
  """Perform the str to none operation."""
595
- ...
596
584
 
597
585
 
598
586
  @deprecated_alias(parse_to_optional_bool)
599
587
  def str_to_optional_bool(*args, **kwds):
600
588
  """Perform the str to optional bool operation."""
601
- ...
602
589
 
603
590
 
604
591
  @deprecated_alias(parse_to_optional_float)
605
592
  def str_to_optional_float(*args, **kwds):
606
593
  """Perform the str to optional float operation."""
607
- ...
608
594
 
609
595
 
610
596
  @deprecated_alias(parse_to_optional_int)
611
597
  def str_to_optional_int(*args, **kwds):
612
598
  """Perform the str to optional int operation."""
613
- ...
614
599
 
615
600
 
616
601
  @deprecated_alias(parse_to_optional_str)
617
602
  def str_to_optional_str(*args, **kwds):
618
603
  """Perform the str to optional str operation."""
619
- ...