Bug report
Source:
| defformatannotation(annotation, base_module=None): |
| ifgetattr(annotation, '__module__', None) =='typing': |
| returnrepr(annotation).replace('typing.', '') |
| ifisinstance(annotation, types.GenericAlias): |
| returnstr(annotation) |
| ifisinstance(annotation, type): |
| ifannotation.__module__in ('builtins', base_module): |
| returnannotation.__qualname__ |
| returnannotation.__module__+'.'+annotation.__qualname__ |
| returnrepr(annotation) |
An error replacement happens in:
repr(annotation).replace('typing.', '')Suppose that I define a class or Protocol under foo/typing.py:
Then Union[int, A] will turn to Union[int, foo.A], but expected result is Union[int, foo.typing.A]
Any module with suffix like xxtyping will be replaced, such as nptyping.
Solution
Replace the line to:
defrepl(match):
text=match.group()
iftext.startswith('typing.'):
returntext[len('typing.'):]
returntextreturnre.sub(r'[\w\.]+', repl, repr(annotation))
Bug report
Source:
cpython/Lib/inspect.py
Lines 1449 to 1458 in 91afe66
An error replacement happens in:
Suppose that I define a class or Protocol under
foo/typing.py:Then
Union[int, A]will turn toUnion[int, foo.A], but expected result isUnion[int, foo.typing.A]Any module with suffix like
xxtypingwill be replaced, such asnptyping.Solution
Replace the line to: