chiark / gitweb /
documentation/python: stay compatible with Python 3.6.
authorVladimír Vondruš <mosra@centrum.cz>
Sat, 13 Jul 2019 14:10:43 +0000 (16:10 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Sun, 14 Jul 2019 17:11:08 +0000 (19:11 +0200)
documentation/python.py
documentation/test_python/inspect_recursive/inspect_recursive/second.py

index 75815e3917ddf87d4827707bd1b0a83d31228e32..8632af8f3c1f0266632e12f569ac01addc072d83 100755 (executable)
@@ -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)))
index 2b96dc19c7dddbcc193ecb43a4e3d8979d2261bb..f654d77ecc8aef42063686e7b7fbe2282bc7810a 100644 (file)
@@ -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