From: Mike Bayer Date: Fri, 7 Feb 2020 22:58:17 +0000 (-0500) Subject: Vendor inspect.formatannotation X-Git-Tag: rel_1_4_1~8^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b08bdcbfe97edb51392009d432897c61ba1804c6;p=thirdparty%2Fsqlalchemy%2Falembic.git 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 --- 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.