From: Mike Bayer Date: Fri, 7 Feb 2020 22:55:07 +0000 (-0500) Subject: Vendor inspect.formatannotation X-Git-Tag: rel_1_3_14~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4bd8a28bce111d2eec86d8ed4df65b4490947257;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Vendor inspect.formatannotation 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) --- diff --git a/doc/build/changelog/unreleased_13/5138.rst b/doc/build/changelog/unreleased_13/5138.rst new file mode 100644 index 0000000000..e0a04b1b07 --- /dev/null +++ b/doc/build/changelog/unreleased_13/5138.rst @@ -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. + diff --git a/lib/sqlalchemy/util/compat.py b/lib/sqlalchemy/util/compat.py index cc3b40245a..4d234aafd7 100644 --- a/lib/sqlalchemy/util/compat.py +++ b/lib/sqlalchemy/util/compat.py @@ -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.