From b08bdcbfe97edb51392009d432897c61ba1804c6 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 7 Feb 2020 17:58:17 -0500 Subject: [PATCH] Vendor inspect.formatannotation 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 | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/alembic/util/compat.py b/alembic/util/compat.py index 78a93fe9..c9c2a588 100644 --- a/alembic/util/compat.py +++ b/alembic/util/compat.py @@ -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. -- 2.47.2