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

inspect.signature(map) works in Python 3.13, but gives a signature that looks like only one arg is required #120526

Closed
AdamWill opened this issue Jun 14, 2024 · 4 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@AdamWill
Copy link
Contributor

AdamWill commented Jun 14, 2024

Bug report

Bug description:

>>> list(inspect.signature(map).parameters.values())[0].kind
<_ParameterKind.POSITIONAL_ONLY: 0>
>>> list(inspect.signature(map).parameters.values())[1].kind
<_ParameterKind.VAR_POSITIONAL: 2>

Up to Python 3.12, inspect.signature(map) just doesn't work, it raises ValueError. In 3.13, it works, but the signature you get seems like kind of a lie. The toolz library has this code to work out how many arguments a function requires:

    return sum(1 for p in sigspec.parameters.values()
               if p.default is p.empty
               and p.kind in (p.POSITIONAL_OR_KEYWORD, p.POSITIONAL_ONLY))

where sigspec is the result of inspect.signature(function). That code seems sensible to me. But it gives the wrong answer for map, because the signature returned by inspect.signature(map) says the "kind" of the second arg (iterables) is VAR_POSITIONAL, which the docs say "corresponds to a *args parameter in a Python function definition". That does make it seem optional, because of course if you define a function like myfunc(*args), it does not require any arguments. But really, for map, you have to provide at least one iterable, or you get the error "map() must have at least two arguments." from https://github.com/python/cpython/blob/b2e71ff4f8fa5b7d8117dd8125137aee3d01f015/Python/bltinmodule.c#L1322C12-L1322C53 .

It kinda seems to me like maybe the signature of map should show three args, the first one POSITIONAL_ONLY, the second (iterable) POSITIONAL_OR_KEYWORD, and the third (*iterables) VAR_POSITIONAL? This is how it looks in the docs.

toolz can work around this by special-casing map one way or another, but it seemed worth at least reporting upstream.

CPython versions tested on:

3.12, 3.13

Operating systems tested on:

Linux

Linked PRs

@AdamWill AdamWill added the type-bug An unexpected behavior, bug, or error label Jun 14, 2024
@AlexWaygood
Copy link
Member

@serhiy-storchaka, I think you added lots of signatures for builtins in py313?

@sobolevn
Copy link
Member

map(function, iterable, /, *iterables) looks more correct for this case.

@AdamWill
Copy link
Contributor Author

I am sending a PR to fix it, yep.

AdamWill added a commit to AdamWill/cpython that referenced this issue Jun 14, 2024
map requires at least one iterable arg. If you just do `map(map)`
you get an error "map() must have at least two arguments." This
corrects the signature to reflect this, in line with how it's
shown in https://docs.python.org/3/library/functions.html#map .

Signed-off-by: Adam Williamson <[email protected]>
AdamWill added a commit to AdamWill/cpython that referenced this issue Jun 14, 2024
map requires at least one iterable arg. If you just do `map(map)`
you get an error "map() must have at least two arguments." This
corrects the signature to reflect this, in line with how it's
shown in https://docs.python.org/3/library/functions.html#map .

Signed-off-by: Adam Williamson <[email protected]>
AdamWill added a commit to AdamWill/cpython that referenced this issue Jun 14, 2024
map requires at least one iterable arg. If you just do `map(map)`
you get an error "map() must have at least two arguments." This
corrects the signature to reflect this, in line with how it's
shown in https://docs.python.org/3/library/functions.html#map .

Signed-off-by: Adam Williamson <[email protected]>
AdamWill added a commit to AdamWill/cpython that referenced this issue Jun 14, 2024
map requires at least one iterable arg. If you just do `map(map)`
you get an error "map() must have at least two arguments." This
corrects the signature to reflect this, in line with how it's
shown in https://docs.python.org/3/library/functions.html#map .

Signed-off-by: Adam Williamson <[email protected]>
serhiy-storchaka pushed a commit that referenced this issue Jun 15, 2024
map() requires at least one iterable arg.

Signed-off-by: Adam Williamson <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 15, 2024
map() requires at least one iterable arg.

(cherry picked from commit d4039d3)

Co-authored-by: Adam Williamson <[email protected]>
Signed-off-by: Adam Williamson <[email protected]>
@serhiy-storchaka
Copy link
Member

Indeed. Thank you for correction @AdamWill.

serhiy-storchaka pushed a commit that referenced this issue Jun 15, 2024
…120539)

map() requires at least one iterable arg.

(cherry picked from commit d4039d3)

Signed-off-by: Adam Williamson <[email protected]>
Co-authored-by: Adam Williamson <[email protected]>
mrahtz pushed a commit to mrahtz/cpython that referenced this issue Jun 30, 2024
map() requires at least one iterable arg.

Signed-off-by: Adam Williamson <[email protected]>
noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
map() requires at least one iterable arg.

Signed-off-by: Adam Williamson <[email protected]>
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
map() requires at least one iterable arg.

Signed-off-by: Adam Williamson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants