Skip to content

Commit

Permalink
format type name in docs
Browse files Browse the repository at this point in the history
Signed-off-by: Jinzhe Zeng <[email protected]>
  • Loading branch information
njzjz committed Oct 23, 2023
1 parent 60a0248 commit fe3e2f8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions dargs/dargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def __eq__(self, other: "Argument") -> bool:
)

def __repr__(self) -> str:
return f"<Argument {self.name}: {' | '.join(dd.__name__ for dd in self.dtype)}>"
return f"<Argument {self.name}: {' | '.join(self._get_type_name(dd) for dd in self.dtype)}>"

def __getitem__(self, key: str) -> "Argument":
key = key.lstrip("/")
Expand Down Expand Up @@ -431,7 +431,7 @@ def _check_data(self, value: Any, path=None):
raise ArgumentTypeError(
path,
f"key `{self.name}` gets wrong value type, "
f"requires <{'|'.join(str(dd) if isinstance(get_origin(dd), type) else dd.__name__ for dd in self.dtype)}> "
f"requires <{'|'.join(self._get_type_name(dd) for dd in self.dtype)}> "
f"but " + str(e),
) from e
if self.extra_check is not None and not self.extra_check(value):
Expand Down Expand Up @@ -596,7 +596,7 @@ def gen_doc(self, path: Optional[List[str]] = None, **kwargs) -> str:
return "\n".join(filter(None, doc_list))

def gen_doc_head(self, path: Optional[List[str]] = None, **kwargs) -> str:
typesig = "| type: " + " | ".join([f"``{dt.__name__}``" for dt in self.dtype])
typesig = "| type: " + " | ".join([f"``{self._get_type_name(dt)}``" for dt in self.dtype])
if self.optional:
typesig += ", optional"
if self.default == "":
Expand Down Expand Up @@ -642,6 +642,10 @@ def gen_doc_body(self, path: Optional[List[str]] = None, **kwargs) -> str:
body = "\n".join(body_list)
return body

def _get_type_name(self, dd) -> str:
"""Get type name for doc/message generation."""
return str(dd) if isinstance(get_origin(dd), type) else dd.__name__


class Variant:
"""Define multiple choices of possible argument sets.
Expand Down
2 changes: 1 addition & 1 deletion dargs/sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,5 @@ def _test_arguments() -> List[Argument]:
return [
Argument(name="test1", dtype=int, doc="Argument 1"),
Argument(name="test2", dtype=[float, None], doc="Argument 2"),
Argument(name="test3", dtype=list, doc="Argument 3"),
Argument(name="test3", dtype=List[str], doc="Argument 3"),
]

0 comments on commit fe3e2f8

Please sign in to comment.