[BUG] Misleading error message for function signature mismatch [Py -> C++]
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Issue description
When you pass a function from python to C++ and the expected signature does not match, TypeError is raised correctly.
However the explanation in the error message is misleading and shows the signature of the passed function, instead of the expected signature.
Reproducible example code
https://github.com/pybind/pybind11/blob/f61855b9d8821e2576960109a2a67379a6c2366f/tests/test_callbacks.py#L106-L108
print(excinfo.value) produces the following output:
dummy_function2(): incompatible function arguments. The following argument types are supported:
1. (arg0: int, arg1: int) -> int
Invoked with: 1
Although test_dummy_funciton accepts functions with the signature std::function<int(int)>
https://github.com/pybind/pybind11/blob/f61855b9d8821e2576960109a2a67379a6c2366f/tests/test_callbacks.cpp#L108
So the 'Invoked with: 1' bit is correct, but that is not what is supported.
Extended effects
Click to expand!
This problem propagates further and causes more confusion than in the above simple example when you have multiple transitions between C++ and Python:
Say you provide a worker function (python) to a job scheduler (C++) and then invoke the job from python.
job = Job(worker=lambda x: x)
job.run() #(python) calls _job system_ (C++) calls _worker lambda_ (python)
If the function signature mismatches for the worker lambda, the TypeError is only raised once the job is run (which is understandable, because only then the lambda is accessed). But it then also complains about the "incompatible function arguments" and lists the signature of job.run which is completely unrelated to the signature mismatch.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked cases in tests/test_callbacks.py and tests/test_callbacks.cpp, especially the referenced lines, and reproduce the TypeError for the mismatched function signature. Trace where the error text is assembled and add regression coverage so the message reports the expected callable signature while retaining the invoked arguments.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100