chiark / gitweb /
documentation/python: support string annotations.
authorVladimír Vondruš <mosra@centrum.cz>
Tue, 7 May 2019 16:27:17 +0000 (18:27 +0200)
committerVladimír Vondruš <mosra@centrum.cz>
Tue, 21 May 2019 14:51:51 +0000 (16:51 +0200)
documentation/python.py
documentation/test_python/inspect_annotations/inspect_annotations.Foo.html
documentation/test_python/inspect_annotations/inspect_annotations.py

index d16a2021886a5de317f1a4f414acb6a2016fff59..ce109b21fd3b96ddc4672a006dbfb47750a7c4c2 100755 (executable)
@@ -316,6 +316,9 @@ def extract_annotation(annotation) -> str:
     # TODO: why this is not None directly?
     if annotation is inspect.Signature.empty: return None
 
+    # Annotations can be strings, also https://stackoverflow.com/a/33533514
+    if type(annotation) == str: return annotation
+
     # To avoid getting <class 'foo.bar'> for types (and getting foo.bar
     # instead) but getting the actual type for types annotated with e.g.
     # List[int], we need to check if the annotation is actually from the
index 6954a785d052dd3de9a706a6b01aee99339b7b36..7a1735dc15d9144ceed62cc2989295295e9d8082 100644 (file)
             <li>
               Reference
               <ul>
+                <li><a href="#methods">Methods</a></li>
                 <li><a href="#properties">Properties</a></li>
               </ul>
             </li>
           </ul>
         </div>
+        <section id="methods">
+          <h2><a href="#methods">Methods</a></h2>
+          <dl class="m-doc">
+            <dt>
+              <span class="m-doc-wrap-bumper">def <a href="" class="m-doc-self">string_annotation</a>(</span><span class="m-doc-wrap">self: inspect_annotations.Foo)</span>
+            </dt>
+            <dd>String annotations</dd>
+          </dl>
+        </section>
         <section id="properties">
           <h2><a href="#properties">Properties</a></h2>
           <dl class="m-doc">
index f2e2052bc88bdcbd642e2a269972b27e072cb284..1f645227829a5638e5183ceccb8d1f676ad9357a 100644 (file)
@@ -8,6 +8,12 @@ class Foo:
         """A property with a type annotation"""
         pass
 
+    # Self-reference is only possible with a string in Py3
+    # https://stackoverflow.com/a/33533514
+    def string_annotation(self: 'inspect_annotations.Foo'):
+        """String annotations"""
+        pass
+
 def annotation(param: List[int], another: bool, third: str = "hello") -> Foo:
     """An annotated function"""
     pass