Skip to content
9 changes: 4 additions & 5 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@
import itertools
import linecache
import os
import re
import sys
import tokenize
import token
Expand Down Expand Up @@ -1334,10 +1333,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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve speed of :func:`!inspect.formatannotation`. Patch by Semyon Moroz.