From ef19ae67fc2b65bebd0095dfb7b800782254a89d Mon Sep 17 00:00:00 2001 From: donBarbos Date: Mon, 17 Feb 2025 19:10:13 +0400 Subject: [PATCH 1/6] Improve speed of inspect.formatannotation by replacing re --- Lib/inspect.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index facad478103668..bce8fa52461cba 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -153,7 +153,6 @@ import itertools import linecache import os -import re import sys import tokenize import token @@ -1331,10 +1330,10 @@ def formatannotation(annotation, base_module=None, *, quote_annotation_strings=T if not quote_annotation_strings and isinstance(annotation, str): return annotation if getattr(annotation, '__module__', None) == 'typing': - def repl(match): - text = match.group() - return text.removeprefix('typing.') - return re.sub(r'[\w\.]+', repl, repr(annotation)) + return (repr(annotation) + .replace(".typing.", "@TYPING@") + .replace("typing.", "") + .replace("@TYPING@", ".typing.")) if isinstance(annotation, types.GenericAlias): return str(annotation) if isinstance(annotation, type): From d9480c74108943493f7047d5190b67181bba6795 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Tue, 18 Feb 2025 05:05:15 +0400 Subject: [PATCH 2/6] Add blurb --- .../next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst diff --git a/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst b/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst new file mode 100644 index 00000000000000..3ffed3d76c734b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst @@ -0,0 +1,2 @@ +Improve speed of :func:`inspect.formatannotation` by replacing :mod:`re` +with ``.replace()`` method. Patch by Semyon Moroz. From ab852415efe9e6690d75303047e793bf5be9a80a Mon Sep 17 00:00:00 2001 From: donBarbos Date: Tue, 18 Feb 2025 21:54:34 +0400 Subject: [PATCH 3/6] Update NEWS.d entry markup --- .../Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst b/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst index 3ffed3d76c734b..4c3605cc64733c 100644 --- a/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst +++ b/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst @@ -1,2 +1,2 @@ -Improve speed of :func:`inspect.formatannotation` by replacing :mod:`re` -with ``.replace()`` method. Patch by Semyon Moroz. +Improve speed of :func:`inspect.formatannotation` by replacing :mod:`re` with +:func:`replace` method. Patch by Semyon Moroz. From 3c1c4b74205652b59804bf27f81caa6824715b85 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Fri, 28 Mar 2025 01:24:49 +0400 Subject: [PATCH 4/6] Update rst syntax in News entry --- .../next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst b/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst index 4c3605cc64733c..80642c40920ed6 100644 --- a/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst +++ b/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst @@ -1,2 +1,2 @@ Improve speed of :func:`inspect.formatannotation` by replacing :mod:`re` with -:func:`replace` method. Patch by Semyon Moroz. +``replace`` method. Patch by Semyon Moroz. From 1bfbe05fd1057fc5dfa3fcba44332f15db37d90b Mon Sep 17 00:00:00 2001 From: Semyon Moroz Date: Fri, 28 Mar 2025 01:58:06 +0400 Subject: [PATCH 5/6] Update 2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- .../next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst b/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst index 80642c40920ed6..08aab6f8665cb2 100644 --- a/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst +++ b/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst @@ -1,2 +1,2 @@ -Improve speed of :func:`inspect.formatannotation` by replacing :mod:`re` with +Improve speed of :func:`!inspect.formatannotation` by replacing :mod:`re` with ``replace`` method. Patch by Semyon Moroz. From 0ecf16f063afc6760cc625d7270f4381d72248c4 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Thu, 1 May 2025 00:52:21 +0400 Subject: [PATCH 6/6] Remove implementation detail from News entry --- .../Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst b/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst index 08aab6f8665cb2..60fa91246f8aca 100644 --- a/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst +++ b/Misc/NEWS.d/next/Library/2025-02-18-05-04-13.gh-issue-130167.aOgAz_.rst @@ -1,2 +1 @@ -Improve speed of :func:`!inspect.formatannotation` by replacing :mod:`re` with -``replace`` method. Patch by Semyon Moroz. +Improve speed of :func:`!inspect.formatannotation`. Patch by Semyon Moroz.