Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Lib/inspect.py: Avoid wild replacement in formatannotation #96073

Closed
Assignees
JelleZijlstra
Labels
type-bugAn unexpected behavior, bug, or error
@iyume

Description

@iyume

Bug report

Source:

cpython/Lib/inspect.py

Lines 1449 to 1458 in91afe66

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 underfoo/typing.py:

classA:pass

ThenUnion[int, A] will turn toUnion[int, foo.A], but expected result isUnion[int, foo.typing.A]

Any module with suffix likexxtyping will be replaced, such asnptyping.

Solution

Replace the line to:

defrepl(match):text=match.group()iftext.startswith('typing.'):returntext[len('typing.'):]returntextreturnre.sub(r'[\w\.]+',repl,repr(annotation))

Metadata

Metadata

Assignees

Labels

type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp