From: Vladimír Vondruš Date: Tue, 7 May 2019 16:27:17 +0000 (+0200) Subject: documentation/python: support string annotations. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=3d3a1b1ac388d1b3cd22adf54c0a3ecc992e3af0;p=blog.git documentation/python: support string annotations. --- diff --git a/documentation/python.py b/documentation/python.py index d16a2021..ce109b21 100755 --- a/documentation/python.py +++ b/documentation/python.py @@ -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 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 diff --git a/documentation/test_python/inspect_annotations/inspect_annotations.Foo.html b/documentation/test_python/inspect_annotations/inspect_annotations.Foo.html index 6954a785..7a1735dc 100644 --- a/documentation/test_python/inspect_annotations/inspect_annotations.Foo.html +++ b/documentation/test_python/inspect_annotations/inspect_annotations.Foo.html @@ -29,11 +29,21 @@
  • Reference
  • +
    +

    Methods

    +
    +
    + def string_annotation(self: inspect_annotations.Foo) +
    +
    String annotations
    +
    +

    Properties

    diff --git a/documentation/test_python/inspect_annotations/inspect_annotations.py b/documentation/test_python/inspect_annotations/inspect_annotations.py index f2e2052b..1f645227 100644 --- a/documentation/test_python/inspect_annotations/inspect_annotations.py +++ b/documentation/test_python/inspect_annotations/inspect_annotations.py @@ -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