Skip to content

Commit

Permalink
Fix variables output under Python 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
domdfcoding committed Sep 30, 2020
1 parent 8817581 commit 16daa93
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
5 changes: 4 additions & 1 deletion sphinx_toolbox/more_autodoc/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ def get_variable_type(documenter: Documenter) -> str:
if annotation.isidentifier() and annotation in module_dict:
return format_annotation(module_dict[annotation])
else:
return format_annotation(ForwardRef(annotation)._evaluate(module_dict, module_dict))
if sys.version_info < (3, 9):
return format_annotation(ForwardRef(annotation)._evaluate(module_dict, module_dict))
else:
return format_annotation(ForwardRef(annotation)._evaluate(module_dict, module_dict, set()))

except (NameError, TypeError, ValueError, AttributeError):
return annotation
Expand Down
75 changes: 75 additions & 0 deletions tests/test_autodoc_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# stdlib
from typing import Any, Dict, List, NamedTuple, Sequence, Type, Union, no_type_check

# 3rd party
from domdf_python_tools.secrets import Secret

# this package
from sphinx_toolbox.more_autodoc.variables import get_variable_type


class Foo:
a: str
b: int
c: float
d: "float"
e: "List"


class Bar:
a: str
b: int
c: float
d: "float"
e: "List"
f: "Set" # type: ignore

def __init__(self):
self.g: Secret = Secret("")
self.h: "Secret" = Secret("")


class Analyzer(NamedTuple):
annotations: Dict[Sequence[str], Any]


class Documenter(NamedTuple):
parent: Type
objpath: List[str]
analyzer: Analyzer


@no_type_check
def test_get_variable_type():
assert get_variable_type(Documenter(Foo, ["Foo", "a"], Analyzer({}))) == ":py:class:`str`"
assert get_variable_type(Documenter(Foo, ["Foo", "b"], Analyzer({}))) == ":py:class:`int`"
assert get_variable_type(Documenter(Foo, ["Foo", "c"], Analyzer({}))) == ":py:class:`float`"
assert get_variable_type(Documenter(Foo, ["Foo", "d"], Analyzer({}))) == ":py:class:`float`"
assert get_variable_type(Documenter(Foo, ["Foo", "e"], Analyzer({}))) == ":py:class:`~typing.List`"

assert get_variable_type(Documenter(Bar, ["Bar", "a"], Analyzer({}))) == ":py:class:`str`"
assert get_variable_type(Documenter(Bar, ["Bar", "b"], Analyzer({}))) == ":py:class:`int`"
assert get_variable_type(Documenter(Bar, ["Bar", "c"], Analyzer({}))) == ":py:class:`float`"
# Failed forward reference throws everything else out of whack
assert get_variable_type(Documenter(Bar, ["Bar", "d"], Analyzer({}))) == ":py:obj:`~.float`"
assert get_variable_type(Documenter(Bar, ["Bar", "e"], Analyzer({}))) == ":py:obj:`~.List`"
assert get_variable_type(Documenter(Bar, ["Bar", "f"], Analyzer({}))) == ":py:obj:`~.Set`"
assert get_variable_type(Documenter(Bar, ["Bar", "g"], Analyzer({}))) == ''
# assert get_variable_type(Documenter(Bar, ["Bar", "g"], Analyzer({("Bar", "g"): "Secret"}))) == ":py:class:`~domdf_python_tools.secrets.Secret`"
assert get_variable_type(
Documenter(Bar, ["Bar", "h"], Analyzer({("Bar", "h"): "'Secret'"}))
) == ":py:class:`~domdf_python_tools.secrets.Secret`"
assert get_variable_type(
Documenter(Bar, ["Bar", "h"], Analyzer({("Bar", "h"): '"Secret"'}))
) == ":py:class:`~domdf_python_tools.secrets.Secret`"
assert get_variable_type(
Documenter(Bar, ["Bar", "h"], Analyzer({("Bar", "h"): "Union[str, float, int]"}))
) == ":py:data:`~typing.Union`\\[:py:class:`str`, :py:class:`float`, :py:class:`int`]"
assert get_variable_type(
Documenter(Bar, ["Bar", "h"], Analyzer({("Bar", "h"): "'Union[str, float, int]'"}))
) == ":py:data:`~typing.Union`\\[:py:class:`str`, :py:class:`float`, :py:class:`int`]"
assert get_variable_type(
Documenter(Bar, ["Bar", "h"], Analyzer({("Bar", "h"): '"Union[str, float, int]"'}))
) == ":py:data:`~typing.Union`\\[:py:class:`str`, :py:class:`float`, :py:class:`int`]"

assert get_variable_type(Documenter('Bar', ["Bar", "f"], Analyzer({}))) == ''
8 changes: 7 additions & 1 deletion tests/test_output/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ def test_formatting_html_output(page: BeautifulSoup, file_regression: FileRegres
"autoprotocol.html",
"typevars.html",
"overloads.html",
"instancevar.html",
pytest.param(
"instancevar.html",
marks=pytest.mark.skipif(
condition=sys.version_info < (3, 7),
reason="Output differs on Python 3.6",
),
),
],
indirect=True
)
Expand Down

0 comments on commit 16daa93

Please sign in to comment.