Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve speed of :func:`inspect.formatannotation` by replacing :mod:`re` with
:func:`replace` method. Patch by Semyon Moroz.
Loading