Skip to content

Commit ef19ae6

Browse files
committed
Improve speed of inspect.formatannotation by replacing re
1 parent 2e8044a commit ef19ae6

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

Lib/inspect.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@
153153
import itertools
154154
import linecache
155155
import os
156-
import re
157156
import sys
158157
import tokenize
159158
import token
@@ -1331,10 +1330,10 @@ def formatannotation(annotation, base_module=None, *, quote_annotation_strings=T
13311330
if not quote_annotation_strings and isinstance(annotation, str):
13321331
return annotation
13331332
if getattr(annotation, '__module__', None) == 'typing':
1334-
def repl(match):
1335-
text = match.group()
1336-
return text.removeprefix('typing.')
1337-
return re.sub(r'[\w\.]+', repl, repr(annotation))
1333+
return (repr(annotation)
1334+
.replace(".typing.", "@TYPING@")
1335+
.replace("typing.", "")
1336+
.replace("@TYPING@", ".typing."))
13381337
if isinstance(annotation, types.GenericAlias):
13391338
return str(annotation)
13401339
if isinstance(annotation, type):

0 commit comments

Comments
 (0)