Skip to content

Commit e36d1d9

Browse files
authored
Merge pull request #21 from explosion/fix/display-type
2 parents 1518784 + f542280 commit e36d1d9

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

radicli/tests/test_util.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import typing
12
from typing import Union, Generic, List, TypeVar
23
from pathlib import Path
4+
import pathlib
35
from uuid import UUID
46
import pytest
57
import shutil
@@ -23,6 +25,12 @@ class CustomGeneric(Generic[_KindT]):
2325
(CustomGeneric[str], "CustomGeneric[str]"),
2426
(UUID, "UUID"),
2527
(shutil.rmtree, "rmtree"),
28+
(typing.List[pathlib.Path], "List[Path]"),
29+
(
30+
typing.Dict[Union[str, int], Union[str, pathlib.Path, int]],
31+
"Dict[Union[str, int], Union[str, Path, int]]",
32+
),
33+
(Union[str, typing.Tuple[str, Path]], "Union[str, Tuple[str, Path]]"),
2634
("foo.bar", "foo.bar"),
2735
(None, None),
2836
],

radicli/util.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pathlib import Path
88
import inspect
99
import argparse
10+
import re
1011

1112
# We need this Iterable type, which is the type origin of types.Iterable
1213
try:
@@ -358,8 +359,23 @@ def stringify_type(arg_type: Any) -> Optional[str]:
358359
type_str = f"{type_str}[{', '.join(type_args)}]"
359360
return type_str
360361
type_str = str(arg_type)
361-
split_type = type_str.rsplit(".", 1)
362-
return split_type[1] if len(split_type) == 2 else type_str
362+
return _stringify_type(str(arg_type))
363+
364+
365+
subtype_matcher = re.compile(r"\[(.*)\]")
366+
367+
368+
def _stringify_type(type_str: str) -> str:
369+
parts = []
370+
split_type = subtype_matcher.split(type_str)
371+
first = split_type.pop(0)
372+
split_first = first.rsplit(".", 1)
373+
parts.append(split_first[1] if len(split_first) == 2 else first)
374+
for substr in split_type:
375+
if substr:
376+
objs = [_stringify_type(sub.strip()) for sub in substr.split(",")]
377+
parts.extend(["[", ", ".join(objs), "]"])
378+
return "".join(parts)
363379

364380

365381
def format_type(arg_type: Any) -> Optional[str]:

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[metadata]
2-
version = 0.0.16
2+
version = 0.0.17
33
description = Radically lightweight command-line interfaces
44
url = https://github.com/explosion/radicli
55
author = Explosion

0 commit comments

Comments
 (0)