replbase 0.0.62__tar.gz → 0.0.64__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.
- {replbase-0.0.62 → replbase-0.0.64}/PKG-INFO +1 -1
- {replbase-0.0.62 → replbase-0.0.64}/pyproject.toml +1 -1
- {replbase-0.0.62 → replbase-0.0.64}/replbase/cmd_meta.py +50 -8
- {replbase-0.0.62 → replbase-0.0.64}/LICENSE +0 -0
- {replbase-0.0.62 → replbase-0.0.64}/README.md +0 -0
- {replbase-0.0.62 → replbase-0.0.64}/replbase/__init__.py +0 -0
- {replbase-0.0.62 → replbase-0.0.64}/replbase/repl_base.py +0 -0
|
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
|
|
|
4
4
|
|
|
5
5
|
[tool.poetry]
|
|
6
6
|
name = "replbase"
|
|
7
|
-
version = "0.0.
|
|
7
|
+
version = "0.0.64"
|
|
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
|
|
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,22 @@ class CommandMeta:
|
|
|
225
262
|
"store_false" if parm.default is True else "store_true"
|
|
226
263
|
)
|
|
227
264
|
|
|
228
|
-
if arg_type
|
|
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
|
+
cmd_init["action"] = "append"
|
|
273
|
+
if sub_type in AUTO_ARG_TYPES:
|
|
274
|
+
cmd_init["type"] = sub_type
|
|
275
|
+
|
|
231
276
|
if isinstance(arg_type, type) and isinstance(
|
|
232
|
-
parm.default, arg_type
|
|
277
|
+
parm.default, (arg_type, str)
|
|
233
278
|
):
|
|
234
279
|
cmd_init["default"] = parm.default
|
|
235
280
|
|
|
236
|
-
if isinstance(arg_type, list):
|
|
237
|
-
cmd_init["nargs"] = "+"
|
|
238
|
-
|
|
239
281
|
if cmd_init:
|
|
240
282
|
repl_cmd.parser.add_argument(flag_name, **cmd_init)
|
|
241
283
|
|
|
@@ -312,7 +354,7 @@ class CommandMeta:
|
|
|
312
354
|
"""Parse the argument strings from the docstring"""
|
|
313
355
|
# pylint: disable=C0301
|
|
314
356
|
arg_re = re.compile(
|
|
315
|
-
r"(?P<arg_name>[a-zA-Z_]\w*)\s*(?P<type_hint>\(
|
|
357
|
+
r"(?P<arg_name>[a-zA-Z_]\w*)\s*(?P<type_hint>\(.+(?:, optional)*\)):(?P<arg_text>\s*.+?$)"
|
|
316
358
|
)
|
|
317
359
|
# pylint: enable=C0301
|
|
318
360
|
|
|
@@ -327,7 +369,7 @@ class CommandMeta:
|
|
|
327
369
|
|
|
328
370
|
if param:
|
|
329
371
|
arg_type = self.get_param_str(param)
|
|
330
|
-
args[cur_arg].append(f"{arg_type}{arg_text}")
|
|
372
|
+
args[cur_arg].append(f"{arg_type}:{arg_text}")
|
|
331
373
|
elif cur_arg:
|
|
332
374
|
args[cur_arg].append(line)
|
|
333
375
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|