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

Incorrect behavior of inspect.signature(f).bind #87106

Closed
slavkostrov mannequin opened this issue Jan 16, 2021 · 2 comments · Fixed by #103404
Closed

Incorrect behavior of inspect.signature(f).bind #87106

slavkostrov mannequin opened this issue Jan 16, 2021 · 2 comments · Fixed by #103404
Labels
3.8 (EOL) end of life 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@slavkostrov
Copy link
Mannequin

slavkostrov mannequin commented Jan 16, 2021

BPO 42940

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2021-01-16.16:30:52.922>
labels = ['type-bug', '3.8', '3.9']
title = 'Incorrect behavior of inspect.signature(f).bind'
updated_at = <Date 2021-01-16.16:30:52.922>
user = 'https://bugs.python.org/slavkostrov'

bugs.python.org fields:

activity = <Date 2021-01-16.16:30:52.922>
actor = 'slavkostrov'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = []
creation = <Date 2021-01-16.16:30:52.922>
creator = 'slavkostrov'
dependencies = []
files = []
hgrepos = []
issue_num = 42940
keywords = []
message_count = 1.0
messages = ['385143']
nosy_count = 1.0
nosy_names = ['slavkostrov']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue42940'
versions = ['Python 3.8', 'Python 3.9']

Linked PRs

@slavkostrov
Copy link
Mannequin Author

slavkostrov mannequin commented Jan 16, 2021

>>> def foo(a, /, b=1, **kwargs): pass
...
>>> foo(a=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: foo() missing 1 required positional argument: 'a'

>>> inspect.signature(foo).bind(a=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "\Python\Python39\lib\inspect.py", line 3062, in bind
    return self._bind(args, kwargs)
  File "\Python\Python39\lib\inspect.py", line 2958, in _bind
    raise TypeError(msg) from None
TypeError: 'a' parameter is positional only, but was passed as a keyword

There is a different behaviour between a simple function call and inspect.signature(foo).bind - different TypeError messages.

I think inspect should check for VARKEYWORDS before throwing an error and throw only if there are not.

@jacobtylerwalls
Copy link
Contributor

In another case, inspect.signature(foo).bind gives a TypeError when no TypeError is even raised in the actual call:

>>> def foo(a=0, /, b=1, **kwargs): pass
...
>>> foo(a=1)
>>> inspect.signature(foo).bind(a=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/inspect.py", line 3037, in bind
    return self._bind(args, kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/inspect.py", line 2933, in _bind
    raise TypeError(msg) from None
TypeError: 'a' parameter is positional only, but was passed as a keyword

jacobtylerwalls added a commit to jacobtylerwalls/cpython that referenced this issue Apr 10, 2023
@iritkatriel iritkatriel added the stdlib Python modules in the Lib dir label Nov 23, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 13, 2024
…nly arguments with **kwargs (pythonGH-103404)

(cherry picked from commit 9c15202)

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 13, 2024
…nly arguments with **kwargs (pythonGH-103404)

(cherry picked from commit 9c15202)

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
serhiy-storchaka pushed a commit that referenced this issue May 13, 2024
…only arguments with **kwargs (GH-103404) (GH-118984)

(cherry picked from commit 9c15202)

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
serhiy-storchaka pushed a commit that referenced this issue May 13, 2024
…only arguments with **kwargs (GH-103404) (#118985)

(cherry picked from commit 9c15202)

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 (EOL) end of life 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
3 participants
@iritkatriel @jacobtylerwalls and others