Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Closed
Dutcho opened this issue Jul 23, 2023 · 4 comments
Closed

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

Dutcho opened this issue Jul 23, 2023 · 4 comments
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@Dutcho
Copy link

Dutcho commented Jul 23, 2023

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

@Dutcho Dutcho added the type-bug An unexpected behavior, bug, or error label Jul 23, 2023
@AlexWaygood AlexWaygood added the stdlib Python modules in the Lib dir label Jul 23, 2023
@Eclips4
Copy link
Member

Eclips4 commented Jul 25, 2023

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

@Eclips4
Copy link
Member

Eclips4 commented Jul 25, 2023

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

Eclips4 added a commit to Eclips4/cpython that referenced this issue Jul 28, 2023
@AA-Turner
Copy link
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

@AA-Turner AA-Turner added 3.13 new features, bugs and security fixes 3.11 only security fixes 3.12 bugs and security fixes labels Jul 30, 2023
@serhiy-storchaka
Copy link
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.

Eclips4 added a commit to Eclips4/cpython that referenced this issue Feb 17, 2024
…notation (pythonGH-107401)

(cherry picked from commit b9a9e3d)

Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
serhiy-storchaka pushed a commit that referenced this issue Feb 17, 2024
serhiy-storchaka pushed a commit that referenced this issue Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 new features, bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

5 participants