]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Vendor inspect.formatannotation
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 7 Feb 2020 22:55:07 +0000 (17:55 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 7 Feb 2020 22:56:29 +0000 (17:56 -0500)
Vendored the ``inspect.formatannotation`` function inside of
``sqlalchemy.util.compat``, which is needed for the vendored version of
``inspect.formatargspec``.  The function is not documented in cPython and
is not guaranteed to be available in future Python versions.

Fixes: #5138
Change-Id: I76bdddc28507fb1b4403f1b718d6f9cc2fb6d93c
(cherry picked from commit 47aa62abde6eba67802f2f7126cc79fbd95b5d1a)

doc/build/changelog/unreleased_13/5138.rst [new file with mode: 0644]
lib/sqlalchemy/util/compat.py

diff --git a/doc/build/changelog/unreleased_13/5138.rst b/doc/build/changelog/unreleased_13/5138.rst
new file mode 100644 (file)
index 0000000..e0a04b1
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, installation
+    :tickets: 5138
+
+    Vendored the ``inspect.formatannotation`` function inside of
+    ``sqlalchemy.util.compat``, which is needed for the vendored version of
+    ``inspect.formatargspec``.  The function is not documented in cPython and
+    is not guaranteed to be available in future Python versions.
+
index cc3b40245a58eef8de297997f4fd7880f0c9360e..4d234aafd7e18f0c9bf242fdbb64e5ca44cfb381 100644 (file)
@@ -267,7 +267,18 @@ else:
 
 
 if py35:
-    from inspect import formatannotation
+
+    def _formatannotation(annotation, base_module=None):
+        """vendored from python 3.7
+        """
+
+        if getattr(annotation, "__module__", None) == "typing":
+            return repr(annotation).replace("typing.", "")
+        if isinstance(annotation, type):
+            if annotation.__module__ in ("builtins", base_module):
+                return annotation.__qualname__
+            return annotation.__module__ + "." + annotation.__qualname__
+        return repr(annotation)
 
     def inspect_formatargspec(
         args,
@@ -282,7 +293,7 @@ if py35:
         formatvarkw=lambda name: "**" + name,
         formatvalue=lambda value: "=" + repr(value),
         formatreturns=lambda text: " -> " + text,
-        formatannotation=formatannotation,
+        formatannotation=_formatannotation,
     ):
         """Copy formatargspec from python 3.7 standard library.