Skip to content

Commit

Permalink
Merge pull request #21 from explosion/fix/display-type
Browse files Browse the repository at this point in the history
  • Loading branch information
ines committed Mar 9, 2023
2 parents 1518784 + f542280 commit e36d1d9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
8 changes: 8 additions & 0 deletions radicli/tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import typing
from typing import Union, Generic, List, TypeVar
from pathlib import Path
import pathlib
from uuid import UUID
import pytest
import shutil
Expand All @@ -23,6 +25,12 @@ class CustomGeneric(Generic[_KindT]):
(CustomGeneric[str], "CustomGeneric[str]"),
(UUID, "UUID"),
(shutil.rmtree, "rmtree"),
(typing.List[pathlib.Path], "List[Path]"),
(
typing.Dict[Union[str, int], Union[str, pathlib.Path, int]],
"Dict[Union[str, int], Union[str, Path, int]]",
),
(Union[str, typing.Tuple[str, Path]], "Union[str, Tuple[str, Path]]"),
("foo.bar", "foo.bar"),
(None, None),
],
Expand Down
20 changes: 18 additions & 2 deletions radicli/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pathlib import Path
import inspect
import argparse
import re

# We need this Iterable type, which is the type origin of types.Iterable
try:
Expand Down Expand Up @@ -358,8 +359,23 @@ def stringify_type(arg_type: Any) -> Optional[str]:
type_str = f"{type_str}[{', '.join(type_args)}]"
return type_str
type_str = str(arg_type)
split_type = type_str.rsplit(".", 1)
return split_type[1] if len(split_type) == 2 else type_str
return _stringify_type(str(arg_type))


subtype_matcher = re.compile(r"\[(.*)\]")


def _stringify_type(type_str: str) -> str:
parts = []
split_type = subtype_matcher.split(type_str)
first = split_type.pop(0)
split_first = first.rsplit(".", 1)
parts.append(split_first[1] if len(split_first) == 2 else first)
for substr in split_type:
if substr:
objs = [_stringify_type(sub.strip()) for sub in substr.split(",")]
parts.extend(["[", ", ".join(objs), "]"])
return "".join(parts)


def format_type(arg_type: Any) -> Optional[str]:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[metadata]
version = 0.0.16
version = 0.0.17
description = Radically lightweight command-line interfaces
url = https://github.com/explosion/radicli
author = Explosion
Expand Down

0 comments on commit e36d1d9

Please sign in to comment.