You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
help() output of lambda with manually set __annotations__ is one char off #107155
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
f=lambdaa, 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.
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.
Activity
Eclips4 commentedon Jul 25, 2023
Looks like a known issue :)
cpython/Lib/pydoc.py
Lines 1503 to 1506 in f443b54
Eclips4 commentedon Jul 25, 2023
Probably, we can just replace boundaries,
argspec.replace("(", "").replace(")", ""). Can there be any parenthesis in signature of lambda?pythongh-107155: Fix `help(lambda_func)` when `lambda_func` has `__an…
AA-Turner commentedon Jul 30, 2023
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
serhiy-storchaka commentedon Jan 7, 2024
The parentheses were removed because
lambda (a, b, c)is not a valid Python syntax. But neitherlambda a, b, c -> intis. 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.gh-107155: Fix help() for lambda function with return annotation (GH-…
[3.12] pythongh-107155: Fix help() for lambda function with return an…
[3.12] gh-107155: Fix help() for lambda function with return annotati…
[3.11] gh-107155: Fix help() for lambda function with return annotati…
pythongh-107155: Fix help() for lambda function with return annotation (
pythongh-107155: Fix help() for lambda function with return annotation (
pythongh-107155: Fix help() for lambda function with return annotation (