|
23 | 23 | from docutils.utils import SystemMessage, unescape
|
24 | 24 |
|
25 | 25 |
|
26 |
| -def _literal_values(origin, annotation): |
27 |
| - if origin is Literal: |
28 |
| - values = annotation.__args__ |
29 |
| - elif hasattr(annotation, "__values__"): |
30 |
| - values = annotation.__values__ |
31 |
| - else: |
32 |
| - return None |
33 |
| - return ", ".join(f"``{a!r}``" for a in values) |
34 |
| - |
35 |
| - |
36 | 26 | def _format_full(annotation: Type[Any], fully_qualified: bool = False):
|
37 | 27 | if inspect.isclass(annotation) and annotation.__module__ == "builtins":
|
38 | 28 | return _format_orig(annotation, fully_qualified)
|
39 | 29 |
|
40 | 30 | origin = getattr(annotation, "__origin__", None)
|
41 | 31 | tilde = "" if fully_qualified else "~"
|
42 | 32 |
|
43 |
| - # Not yet supported by sphinx_autodoc_typehints |
44 |
| - literal_values = _literal_values(origin, annotation) |
45 |
| - if literal_values: |
46 |
| - return f":py:data:`{tilde}typing.Literal`\\[{literal_values}]" |
47 |
| - |
48 | 33 | annotation_cls = annotation if inspect.isclass(annotation) else type(annotation)
|
49 | 34 | if annotation_cls.__module__ == "typing":
|
50 | 35 | return _format_orig(annotation, fully_qualified)
|
@@ -82,9 +67,9 @@ def _format_terse(annotation: Type[Any], fully_qualified: bool = False) -> str:
|
82 | 67 | k, v = annotation.__args__
|
83 | 68 | return f"{{{_format_terse(k, fully_qualified)}: {_format_terse(v, fully_qualified)}}}"
|
84 | 69 |
|
85 |
| - literal_values = _literal_values(origin, annotation) |
86 |
| - if literal_values: |
87 |
| - return f"{{{literal_values}}}" |
| 70 | + if origin is Literal or hasattr(annotation, "__values__"): |
| 71 | + values = getattr(annotation, "__args__", ()) or annotation.__values__ |
| 72 | + return f"{{{', '.join(map(repr, values))}}}" |
88 | 73 |
|
89 | 74 | return _format_full(annotation, fully_qualified)
|
90 | 75 |
|
|
0 commit comments