From 4bd8a28bce111d2eec86d8ed4df65b4490947257 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 7 Feb 2020 17:55:07 -0500 Subject: [PATCH] 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) --- doc/build/changelog/unreleased_13/5138.rst | 9 +++++++++ lib/sqlalchemy/util/compat.py | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 doc/build/changelog/unreleased_13/5138.rst 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. -- 2.47.2