Skip to content

help() output of lambda with manually set __annotations__ is one char off #107155

Closed

Description

@Dutcho

Bug report

help() output of lambda with manually set __annotations__ is slightly scrambled.

f = lambda a, b, c: 0              # lambdas cannot have annotations
f.__annotations__['return'] = int  # but you can set them manually

help(f)                            # superfluous ")" and missed last char
# <lambda> lambda a, b, c) -> in

import inspect
print(inspect.signature(f))        # correctly displays
# (a, b, c) -> int

Note

This is a minor glitch for an atypical (perhaps even unsupported) use of annotations.

Hypothesis

Perhaps help() uses (the same logic as) inspect.signature() and (to reflect lambda syntax) wants to drop the brackets "(...)", but then has a one-off error for its text slice (picking ") -> in" instead of " -> int")

Environment

  • CPython versions tested on: 3.6.8 - 3.11.4, 3.12.0b4
  • Operating system and architecture: Windows 11

Linked PRs

Activity

  1. added
    type-bug
    on Jul 23, 2023
  2. added
    stdlib
    on Jul 23, 2023
  3. Eclips4 commented on Jul 25, 2023

    @Eclips4
    Member

    Looks like a known issue :)

    cpython/Lib/pydoc.py

    Lines 1503 to 1506 in f443b54

    # XXX lambda's won't usually have func_annotations['return']
    # since the syntax doesn't support but it is possible.
    # So removing parentheses isn't truly safe.
    argspec = argspec[1:-1] # remove parentheses

  4. Eclips4 commented on Jul 25, 2023

    @Eclips4
    Member

    Probably, we can just replace boundaries, argspec.replace("(", "").replace(")", ""). Can there be any parenthesis in signature of lambda?

  5. added a commit that references this issue on Jul 28, 2023
  6. AA-Turner commented on Jul 30, 2023

    @AA-Turner
    Member
    f = lambda a, b: (a, b)
    f.__annotations__['return'] = (int, int)
    help(f)
    # Help on function <lambda> in module __main__:
    #
    # <lambda> lambda a, b) -> (<class 'int'>, <class 'int'>

    This isn't currently a legal type annotation, but using tuple literals was proposed in PEP-677 and something similar could come up again -- a cleaner fix could be to handle the string conversion manually. I've suggested something on the PR.

    A

  7. added
    3.13
    3.11
    3.12
    on Jul 30, 2023
  8. serhiy-storchaka commented on Jan 7, 2024

    @serhiy-storchaka
    Member

    The parentheses were removed because lambda (a, b, c) is not a valid Python syntax. But neither lambda a, b, c -> int is. So why simply not keep parentheses if there are annotations? lambda (a, b, c) -> int. It will also be less ambiguous if parameters are annotated: lambda (a: int, b: int, c: int) -> int.

  9. added a commit that references this issue on Feb 17, 2024
  10. added a commit that references this issue on Feb 17, 2024
  11. added a commit that references this issue on Feb 17, 2024
  12. added a commit that references this issue on Feb 17, 2024
  13. added a commit that references this issue on Mar 4, 2024
  14. added a commit that references this issue on Apr 17, 2024
  15. added a commit that references this issue on Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.113.123.13stdlibtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions