From: Vladimír Vondruš Date: Sat, 13 Jul 2019 14:10:43 +0000 (+0200) Subject: documentation/python: stay compatible with Python 3.6. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=8e2f131b3d469dbccaee133b668b2fd7ced27f80;p=blog.git documentation/python: stay compatible with Python 3.6. --- diff --git a/documentation/python.py b/documentation/python.py index 75815e39..8632af8f 100755 --- a/documentation/python.py +++ b/documentation/python.py @@ -693,17 +693,23 @@ def extract_annotation(state: State, referrer_path: List[str], annotation) -> st # Or the plain strings might be inside (e.g. List['Foo']), which gets # converted by Python to ForwardRef. Hammer out the actual string and again # leave it as-is, since it's most probably an error. - elif isinstance(annotation, typing.ForwardRef): + elif isinstance(annotation, typing.ForwardRef if sys.version_info >= (3, 7) else typing._ForwardRef): return annotation.__forward_arg__ # If the annotation is from the typing module, it could be a "bracketed" # type, in which case we want to recurse to its types as well. Otherwise # just get its name. elif annotation.__module__ == 'typing': + if sys.version_info >= (3, 7): + name = annotation._name + elif annotation is typing.Any: + name = 'Any' # Any doesn't have __name__ in 3.6 + else: + name = annotation.__name__ if hasattr(annotation, '__args__'): - return 'typing.{}[{}]'.format(annotation._name, ', '.join([extract_annotation(state, referrer_path, i) for i in annotation.__args__])) + return 'typing.{}[{}]'.format(name, ', '.join([extract_annotation(state, referrer_path, i) for i in annotation.__args__])) else: - return 'typing.' + annotation._name + return 'typing.' + name # Otherwise it's a plain type. Turn it into a link. return make_type_link(state, referrer_path, map_name_prefix(state, extract_type(annotation))) diff --git a/documentation/test_python/inspect_recursive/inspect_recursive/second.py b/documentation/test_python/inspect_recursive/inspect_recursive/second.py index 2b96dc19..f654d77e 100644 --- a/documentation/test_python/inspect_recursive/inspect_recursive/second.py +++ b/documentation/test_python/inspect_recursive/inspect_recursive/second.py @@ -1,6 +1,12 @@ """Second module, imported as inspect_recursive.a, with no contents""" import inspect_recursive.first as a -import inspect_recursive.second as b + +import sys + +if sys.version_info >= (3, 7): + # For some reason 3.6 says second doesn't exist yet. I get that, it's a + # cyclic reference, but that works in 3.7. + import inspect_recursive.second as b from inspect_recursive import Foo as Bar