]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Vendor inspect.formatannotation
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 7 Feb 2020 22:58:17 +0000 (17:58 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 7 Feb 2020 22:58:44 +0000 (17:58 -0500)
Vendored the ``inspect.formatannotation`` function inside of
``alembic.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.

Change-Id: Ia891131c0776f025cd12d359e03587819881eaee
Fixes: #657
alembic/util/compat.py

index 78a93fe97f269c008cd52798f5e85b39ba793b77..c9c2a588f123390fa20b715548c49b2f8de59e0a 100644 (file)
@@ -89,7 +89,18 @@ else:
     import collections as collections_abc  # noqa
 
 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,
@@ -104,7 +115,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.