pybind / pybind/pybind11

[BUG]: Inaccurate exception message for function/ method that needs unsigned int

Open
#3,562 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
Problem description

If I define a function or method that requires an unsigned value
void takes_unsigned(unsigned int x) {}

and add it to a pybind module
m.def("takes_unsigned", &takes_unsigned);

and call it from Python, but pass it a negative number
mymodule.takes_unsigned(-10)

, I get the following exception

TypeError: takes_unsigned(): incompatible function arguments. The following argument types are supported:
      1. (arg0: int) -> None

Invoked with: -10

It's great that this case throws a TypeError since my argument was invalid. However, the error message says my call is wrong because the supported argument type is "int" and I invoked with -10 (which is an int). That is not really a description of what went wrong.

Reproducible example code
#include <pybind11/pybind11.h>

void takes_unsigned(unsigned int x) {}

PYBIND11_MODULE(mymodule, m) {
  m.def("takes_unsigned", &takes_unsigned);
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the C++ function and Python call shown in the report, starting from pybind11/pybind11.h. Trace the argument-conversion and exception-message path to determine where an unsigned value is described as int. Done means the negative-argument failure explains the unsigned requirement accurately.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.