Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
iyume opened this issue Aug 18, 2022 · 1 comment
Closed

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

iyume opened this issue Aug 18, 2022 · 1 comment
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@iyume
Copy link
Contributor

iyume commented Aug 18, 2022

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))
@iyume iyume added the type-bug An unexpected behavior, bug, or error label Aug 18, 2022
iyume added a commit to iyume/cpython that referenced this issue Aug 18, 2022
JelleZijlstra added a commit that referenced this issue Oct 7, 2022
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 7, 2022
…honGH-96074)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
(cherry picked from commit d5fea01)

Co-authored-by: Anh71me <iyumelive@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 7, 2022
…honGH-96074)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
(cherry picked from commit d5fea01)

Co-authored-by: Anh71me <iyumelive@gmail.com>
@JelleZijlstra JelleZijlstra self-assigned this Oct 7, 2022
miss-islington added a commit that referenced this issue Oct 7, 2022
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
(cherry picked from commit d5fea01)

Co-authored-by: Anh71me <iyumelive@gmail.com>
miss-islington added a commit that referenced this issue Oct 7, 2022
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
(cherry picked from commit d5fea01)

Co-authored-by: Anh71me <iyumelive@gmail.com>
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this issue Oct 7, 2022
miss-islington pushed a commit that referenced this issue Oct 7, 2022
Automerge-Triggered-By: GH:JelleZijlstra
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 7, 2022
Automerge-Triggered-By: GH:JelleZijlstra
(cherry picked from commit 0f111f5)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 7, 2022
Automerge-Triggered-By: GH:JelleZijlstra
(cherry picked from commit 0f111f5)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
miss-islington added a commit that referenced this issue Oct 7, 2022
Automerge-Triggered-By: GH:JelleZijlstra
(cherry picked from commit 0f111f5)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
miss-islington added a commit that referenced this issue Oct 7, 2022
Automerge-Triggered-By: GH:JelleZijlstra
(cherry picked from commit 0f111f5)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
JelleZijlstra added a commit that referenced this issue Oct 7, 2022
This was broken in #98045 but already fixed on main.
JelleZijlstra added a commit that referenced this issue Oct 8, 2022
JelleZijlstra added a commit that referenced this issue Oct 8, 2022
…8071)

[3.11] Fix installed tests by adding to Makefile.pre.in

This was broken in #98045 but already fixed on main.
carljm added a commit to carljm/cpython that referenced this issue Oct 8, 2022
* main: (38 commits)
  pythongh-92886: make test_ast pass with -O (assertions off) (pythonGH-98058)
  pythongh-92886: make test_coroutines pass with -O (assertions off) (pythonGH-98060)
  pythongh-57179: Add note on symlinks for os.walk (python#94799)
  pythongh-94808: Fix regex on exotic platforms (python#98036)
  pythongh-90085: Remove vestigial -t and -c timeit options (python#94941)
  pythonGH-83901: Improve Signature.bind error message for missing keyword-only params (python#95347)
  pythongh-61105: Add default param, note on using cookiejar subclass (python#95427)
  pythongh-96288: Add a sentence to `os.mkdir`'s docstring. (python#96271)
  pythongh-96073: fix backticks in NEWS entry (pythonGH-98056)
  pythongh-92886: [clinic.py] raise exception on invalid input instead of assertion (pythonGH-98051)
  pythongh-97997: Add col_offset field to tokenizer and use that for AST nodes (python#98000)
  pythonGH-88968: Reject socket that is already used as a transport (python#98010)
  pythongh-96346: Use double caching for re._compile() (python#96347)
  pythongh-91708: Revert params note in urllib.parse.urlparse table (python#96699)
  pythongh-96265: Fix some formatting in faq/design.rst (python#96924)
  pythongh-73196: Add namespace/scope clarification for inheritance section (python#92840)
  pythongh-97646: Change `.js` and `.mjs` files mimetype to conform to RFC 9239 (python#97934)
  pythongh-97923: Always run Ubuntu SSL tests with others in CI (python#97940)
  pythongh-97956: Mention `generate_global_objects.py` in `AC How-To` (python#97957)
  pythongh-96959: Update HTTP links which are redirected to HTTPS (python#98039)
  ...
mpage pushed a commit to mpage/cpython that referenced this issue Oct 11, 2022
…hon#96074)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
mpage pushed a commit to mpage/cpython that referenced this issue Oct 11, 2022
Automerge-Triggered-By: GH:JelleZijlstra
@hauntsaninja
Copy link
Contributor

Thanks, looks like this has been completed

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants