replbase 0.0.61__tar.gz → 0.0.63__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: replbase
3
- Version: 0.0.61
3
+ Version: 0.0.63
4
4
  Summary: "Combination of other REPL tools into a reusable class that generates a REPL"
5
5
  License: MIT
6
6
  Author: Joseph Bochinski
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
4
4
 
5
5
  [tool.poetry]
6
6
  name = "replbase"
7
- version = "0.0.61"
7
+ version = "0.0.63"
8
8
  description = "\"Combination of other REPL tools into a reusable class that generates a REPL\""
9
9
  authors = [ "Joseph Bochinski <stirgejr@gmail.com>",]
10
10
  license = "MIT"
@@ -7,7 +7,44 @@ import re
7
7
  from collections import defaultdict
8
8
  from collections.abc import Callable
9
9
  from dataclasses import dataclass, field
10
- from typing import get_type_hints
10
+ from types import GenericAlias
11
+ from typing import Any, get_type_hints
12
+
13
+ AUTO_ARG_TYPES = [
14
+ int,
15
+ float,
16
+ ascii,
17
+ ord,
18
+ ]
19
+
20
+
21
+ def extract_type_info(annotation: Any) -> tuple[type, type]:
22
+ """Extract type info from an inspect.Paramater annotation
23
+
24
+ Args:
25
+ annotation (Any): Parameter annotation value
26
+
27
+ Returns:
28
+ tuple[type,type]: the annotation/main type, followed by sub/element type
29
+ """
30
+
31
+ # Check if the annotation is an instance of GenericAlias
32
+ if isinstance(annotation, GenericAlias):
33
+ # Get the main type, for example `list` from `list[str]`
34
+ main_type = annotation.__origin__
35
+ # Get the type arguments, for example `[str]` from `list[str]`
36
+ type_args = annotation.__args__
37
+
38
+ # Since we're dealing with a single type argument, like list[str],
39
+ # we expect one element in type_args
40
+ if len(type_args) == 1:
41
+ element_type = type_args[0]
42
+ else:
43
+ element_type = None
44
+
45
+ return main_type, element_type
46
+ else:
47
+ return annotation, None
11
48
 
12
49
 
13
50
  @dataclass
@@ -225,17 +262,21 @@ class CommandMeta:
225
262
  "store_false" if parm.default is True else "store_true"
226
263
  )
227
264
 
228
- if arg_type not in [str, bool]:
265
+ if arg_type in AUTO_ARG_TYPES:
229
266
  cmd_init["type"] = arg_type
230
267
 
268
+ if isinstance(arg_type, GenericAlias):
269
+ main_type, sub_type = extract_type_info(parm.annotation)
270
+ if main_type is list:
271
+ cmd_init["nargs"] = "+"
272
+ if sub_type in AUTO_ARG_TYPES:
273
+ cmd_init["type"] = sub_type
274
+
231
275
  if isinstance(arg_type, type) and isinstance(
232
- parm.default, arg_type
276
+ parm.default, (arg_type, str)
233
277
  ):
234
278
  cmd_init["default"] = parm.default
235
279
 
236
- if isinstance(arg_type, list):
237
- cmd_init["nargs"] = "+"
238
-
239
280
  if cmd_init:
240
281
  repl_cmd.parser.add_argument(flag_name, **cmd_init)
241
282
 
@@ -273,9 +314,9 @@ class CommandMeta:
273
314
  return
274
315
 
275
316
  for arg in self.args:
276
- flag = arg.replace("_", "-")
317
+ flag = arg
277
318
  if self.is_arg_optional(arg):
278
- flag = "--" + flag
319
+ flag = "--" + flag.replace("_", "-")
279
320
 
280
321
  self.flag_names[arg] = flag
281
322
 
File without changes
File without changes