GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Lib/inspect.py: Avoid wild replacement in formatannotation · Issue #96073 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
Lib/inspect.py: Avoid wild replacement in formatannotation#96073
Copy link
Assignees
JelleZijlstra
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@iyume
iyume
opened on Aug 18, 2022
Issue body actions

Bug report

Source:

cpython/Lib/inspect.py

Lines 1449 to 1458 in 91afe66

def formatannotation(annotation, base_module=None):
if getattr(annotation, '__module__', None) == 'typing':
return repr(annotation).replace('typing.', '')
if isinstance(annotation, types.GenericAlias):
return str(annotation)
if isinstance(annotation, type):
if annotation.__module__ in ('builtins', base_module):
return annotation.__qualname__
return annotation.__module__+'.'+annotation.__qualname__
return repr(annotation)

An error replacement happens in:

repr(annotation).replace('typing.', '')

Suppose that I define a class or Protocol under foo/typing.py:

class A:
    pass

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:

        def repl(match):
            text = match.group()
            if text.startswith('typing.'):
                    return text[len('typing.'):]
            return text
        return re.sub(r'[\w\.]+', repl, repr(annotation))

Activity

  1. iyume
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Aug 18, 2022
  2. iyume
    added a commit that references this issue on Aug 18, 2022

    pythonGH-96073: Fix wild replacement in formatannotation

    f14fcb4
  3. JelleZijlstra
    added a commit that references this issue on Oct 7, 2022

    GH-96073: Fix wild replacement in inspect.formatannotation (#96074)

    d5fea01
  4. miss-islington
    added 2 commits that reference this issue on Oct 7, 2022

    pythonGH-96073: Fix wild replacement in inspect.formatannotation (pyt…

    ec569d1

    pythonGH-96073: Fix wild replacement in inspect.formatannotation (pyt…

    d58c82e
  5. JelleZijlstra
    self-assigned this
    on Oct 7, 2022
  6. miss-islington
    added 2 commits that reference this issue on Oct 7, 2022

    GH-96073: Fix wild replacement in inspect.formatannotation (GH-96074)

    107ba92

    GH-96073: Fix wild replacement in inspect.formatannotation (GH-96074)

    a3a3701
  7. JelleZijlstra
    added a commit that references this issue on Oct 7, 2022

    pythongh-96073: fix backticks in NEWS entry

    2209cc4
  8. miss-islington
    added 5 commits that reference this issue on Oct 7, 2022

    gh-96073: fix backticks in NEWS entry (GH-98056)

    0f111f5

    pythongh-96073: fix backticks in NEWS entry (pythonGH-98056)

    550c7ad

    pythongh-96073: fix backticks in NEWS entry (pythonGH-98056)

    5710f6c

    gh-96073: fix backticks in NEWS entry (GH-98056)

    8c81d33

    gh-96073: fix backticks in NEWS entry (GH-98056)

    9dea4e2
  9. JelleZijlstra
    added 3 commits that reference this issue on Oct 7, 2022

    [3.10] gh-96073: Fix installed tests by adding to Makefile.pre.in

    957c796

    [3.10] gh-96073: Fix installed tests by adding to Makefile.pre.in (#9…

    f50e3a7

    [3.11] gh-96073: Fix installed tests by adding to Makefile.pre.in (#9…

    52dab90
  10. carljm
    added a commit that references this issue on Oct 8, 2022

    Merge branch 'main' into typewatch

    27a4980
  11. mpage
    added 2 commits that reference this issue on Oct 11, 2022

    pythonGH-96073: Fix wild replacement in inspect.formatannotation (pyt…

    41b8172

    pythongh-96073: fix backticks in NEWS entry (pythonGH-98056)

    875841a
  12. hauntsaninja commented on Oct 11, 2022

    @hauntsaninja
    hauntsaninja
    on Oct 11, 2022
    Contributor
    More actions

    Thanks, looks like this has been completed

  13. hauntsaninja
    closed this as completedon Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @JelleZijlstra
    JelleZijlstra

Labels

type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.