Skip to content

Commit c35ee9d

Browse files
authored
Use upstream Literal rendering (#11)
1 parent b6241c1 commit c35ee9d

File tree

3 files changed

+6
-21
lines changed

3 files changed

+6
-21
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ test = [
3131
'typing_extensions; python_version<"3.8"',
3232
]
3333
doc = [
34-
'sphinx-autodoc-typehints>=1.8',
34+
'sphinx-autodoc-typehints>=1.9',
3535
]
3636

3737
[tool.black]

scanpydoc/elegant_typehints/formatting.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,13 @@
2323
from docutils.utils import SystemMessage, unescape
2424

2525

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-
3626
def _format_full(annotation: Type[Any], fully_qualified: bool = False):
3727
if inspect.isclass(annotation) and annotation.__module__ == "builtins":
3828
return _format_orig(annotation, fully_qualified)
3929

4030
origin = getattr(annotation, "__origin__", None)
4131
tilde = "" if fully_qualified else "~"
4232

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-
4833
annotation_cls = annotation if inspect.isclass(annotation) else type(annotation)
4934
if annotation_cls.__module__ == "typing":
5035
return _format_orig(annotation, fully_qualified)
@@ -82,9 +67,9 @@ def _format_terse(annotation: Type[Any], fully_qualified: bool = False) -> str:
8267
k, v = annotation.__args__
8368
return f"{{{_format_terse(k, fully_qualified)}: {_format_terse(v, fully_qualified)}}}"
8469

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))}}}"
8873

8974
return _format_full(annotation, fully_qualified)
9075

tests/test_elegant_typehints.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ def test_dict(app):
131131

132132

133133
def test_literal(app):
134-
assert _format_terse(Literal["str", 1, None]) == "{``'str'``, ``1``, ``None``}"
134+
assert _format_terse(Literal["str", 1, None]) == "{'str', 1, None}"
135135
assert _format_full(Literal["str", 1, None]) == (
136-
r":py:data:`~typing.Literal`\[``'str'``, ``1``, ``None``]"
136+
r":py:data:`~typing.Literal`\['str', 1, None]"
137137
)
138138

139139

0 commit comments

Comments
 (0)