Skip to content

feat(types): Use typing.SupportsInt and typing.SupportsFloat and fix other typing based bugs. #5540

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

Merged
merged 26 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f8a6238
init
InvincibleRMC Feb 21, 2025
6ccb3b3
remove import
InvincibleRMC Feb 21, 2025
13dd537
remove uneeded function
InvincibleRMC Feb 21, 2025
bfdee07
style: pre-commit fixes
pre-commit-ci[bot] Feb 21, 2025
91765e5
Add missing import
InvincibleRMC Feb 21, 2025
3f805a9
style: pre-commit fixes
pre-commit-ci[bot] Feb 21, 2025
dbc5814
Fix type behind detailed_message_enabled flag
InvincibleRMC Feb 21, 2025
2d22451
Fix type behind detailed_message_enabled flag
InvincibleRMC Feb 21, 2025
df53695
Add io_name comment
InvincibleRMC Feb 22, 2025
4e85837
Extra loops to single function
InvincibleRMC Feb 22, 2025
89d505f
style: pre-commit fixes
pre-commit-ci[bot] Feb 22, 2025
241a426
Remove unneeded forward declaration
InvincibleRMC Feb 22, 2025
36e65b6
Switch variable name away from macro
InvincibleRMC Feb 22, 2025
ecb8450
Switch variable name away from macro
InvincibleRMC Feb 22, 2025
8270cef
Switch variable name away from macro
InvincibleRMC Feb 22, 2025
c0cd2c3
clang-tidy
InvincibleRMC Feb 22, 2025
145fe8d
remove stack import
InvincibleRMC Feb 22, 2025
f75b750
Fix bug in std::function Callable type
InvincibleRMC Feb 22, 2025
5a9755e
style: pre-commit fixes
pre-commit-ci[bot] Feb 22, 2025
d42e865
Merge branch 'pybind:master' into typing.supports-blank
InvincibleRMC Mar 6, 2025
b915a20
remove is_annotation argument
InvincibleRMC Mar 10, 2025
d0ca993
style: pre-commit fixes
pre-commit-ci[bot] Mar 10, 2025
47b3676
Merge branch 'master' into typing.supports-blank
InvincibleRMC Mar 10, 2025
7defc3b
Merge branch 'master' into typing.supports-blank
InvincibleRMC Mar 13, 2025
652ea9d
Update function name and arg names
InvincibleRMC Mar 17, 2025
05a788e
style: pre-commit fixes
pre-commit-ci[bot] Mar 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value && !is_std_char_t
return PyLong_FromUnsignedLongLong((unsigned long long) src);
}

PYBIND11_TYPE_CASTER(T, const_name<std::is_integral<T>::value>("int", "float"));
PYBIND11_TYPE_CASTER(T,
io_name<std::is_integral<T>::value>(
"typing.SupportsInt", "int", "typing.SupportsFloat", "float"));
};

template <typename T>
Expand Down Expand Up @@ -1231,7 +1233,7 @@ struct handle_type_name<buffer> {
};
template <>
struct handle_type_name<int_> {
static constexpr auto name = const_name("int");
static constexpr auto name = io_name("typing.SupportsInt", "int");
};
template <>
struct handle_type_name<iterable> {
Expand All @@ -1243,7 +1245,7 @@ struct handle_type_name<iterator> {
};
template <>
struct handle_type_name<float_> {
static constexpr auto name = const_name("float");
static constexpr auto name = io_name("typing.SupportsFloat", "float");
};
template <>
struct handle_type_name<function> {
Expand Down Expand Up @@ -1604,6 +1606,16 @@ inline void object::cast() && {

PYBIND11_NAMESPACE_BEGIN(detail)

// forward declaration (definition in attr.h)
struct function_record;

// forward declaration (definition in pybind11.h)
std::string generate_function_signature(const char *type_caster_name_field,
function_record *func_rec,
const std::type_info *const *types,
size_t &type_index,
size_t &arg_index);

// Declared in pytypes.h:
template <typename T, enable_if_t<!is_pyobject<T>::value, int>>
object object_or_cast(T &&o) {
Expand All @@ -1624,7 +1636,11 @@ str_attr_accessor object_api<D>::attr_with_type_hint(const char *key) const {
if (ann.contains(key)) {
throw std::runtime_error("__annotations__[\"" + std::string(key) + "\"] was set already.");
}
ann[key] = make_caster<T>::name.text;

const char *text = make_caster<T>::name.text;

size_t unused = 0;
ann[key] = generate_function_signature(text, nullptr, nullptr, unused, unused);
return {derived(), key};
}

Expand Down
13 changes: 13 additions & 0 deletions include/pybind11/detail/descr.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ constexpr descr<N1 + N2 + 1> io_name(char const (&text1)[N1], char const (&text2
+ const_name("@");
}

// Ternary description for io_name (like the numeric type_caster)
template <bool B, size_t N1, size_t N2, size_t N3, size_t N4>
constexpr enable_if_t<B, descr<N1 + N2 + 1>>
io_name(char const (&text1)[N1], char const (&text2)[N2], char const (&)[N3], char const (&)[N4]) {
return io_name(text1, text2);
}

template <bool B, size_t N1, size_t N2, size_t N3, size_t N4>
constexpr enable_if_t<!B, descr<N3 + N4 + 1>>
io_name(char const (&)[N1], char const (&)[N2], char const (&text3)[N3], char const (&text4)[N4]) {
return io_name(text3, text4);
}

// If "_" is defined as a macro, py::detail::_ cannot be provided.
// It is therefore best to use py::detail::const_name universally.
// This block is for backward compatibility only.
Expand Down
11 changes: 6 additions & 5 deletions include/pybind11/functional.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ struct type_caster<std::function<Return(Args...)>> {
return cpp_function(std::forward<Func>(f_), policy).release();
}

PYBIND11_TYPE_CASTER(type,
const_name("Callable[[")
+ ::pybind11::detail::concat(make_caster<Args>::name...)
+ const_name("], ") + make_caster<retval_type>::name
+ const_name("]"));
PYBIND11_TYPE_CASTER(
type,
const_name("Callable[[")
+ ::pybind11::detail::concat(::pybind11::detail::arg_descr(make_caster<Args>::name)...)
+ const_name("], ") + ::pybind11::detail::return_descr(make_caster<retval_type>::name)
+ const_name("]"));
};

PYBIND11_NAMESPACE_END(detail)
Expand Down
247 changes: 130 additions & 117 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,134 @@ inline std::string replace_newlines_and_squash(const char *text) {
return result.substr(str_begin, str_range);
}

/* Generate a proper function signature */
inline std::string generate_function_signature(const char *type_caster_name_field,
detail::function_record *func_rec,
const std::type_info *const *types,
size_t &type_index,
size_t &arg_index) {
std::string signature;
bool is_starred = false;
bool is_annotation = func_rec == nullptr;
// `is_return_value.top()` is true if we are currently inside the return type of the
// signature. Using `@^`/`@$` we can force types to be arg/return types while `@!` pops
// back to the previous state.
std::stack<bool> is_return_value({false});
// The following characters have special meaning in the signature parsing. Literals
// containing these are escaped with `!`.
std::string special_chars("!@%{}-");
for (const auto *pc = type_caster_name_field; *pc != '\0'; ++pc) {
const auto c = *pc;
if (c == '{') {
// Write arg name for everything except *args and **kwargs.
is_starred = *(pc + 1) == '*';
if (is_starred) {
continue;
}
// Separator for keyword-only arguments, placed before the kw
// arguments start (unless we are already putting an *args)
if (!func_rec->has_args && arg_index == func_rec->nargs_pos) {
signature += "*, ";
}
if (arg_index < func_rec->args.size() && func_rec->args[arg_index].name) {
signature += func_rec->args[arg_index].name;
} else if (arg_index == 0 && func_rec->is_method) {
signature += "self";
} else {
signature += "arg" + std::to_string(arg_index - (func_rec->is_method ? 1 : 0));
}
signature += ": ";
} else if (c == '}') {
// Write default value if available.
if (!is_starred && arg_index < func_rec->args.size()
&& func_rec->args[arg_index].descr) {
signature += " = ";
signature += detail::replace_newlines_and_squash(func_rec->args[arg_index].descr);
}
// Separator for positional-only arguments (placed after the
// argument, rather than before like *
if (func_rec->nargs_pos_only > 0 && (arg_index + 1) == func_rec->nargs_pos_only) {
signature += ", /";
}
if (!is_starred) {
arg_index++;
}
} else if (c == '%') {
const std::type_info *t = types[type_index++];
if (!t) {
pybind11_fail("Internal error while parsing type signature (1)");
}
if (auto *tinfo = detail::get_type_info(*t)) {
handle th((PyObject *) tinfo->type);
signature += th.attr("__module__").cast<std::string>() + "."
+ th.attr("__qualname__").cast<std::string>();
} else if (func_rec->is_new_style_constructor && arg_index == 0) {
// A new-style `__init__` takes `self` as `value_and_holder`.
// Rewrite it to the proper class type.
signature += func_rec->scope.attr("__module__").cast<std::string>() + "."
+ func_rec->scope.attr("__qualname__").cast<std::string>();
} else {
signature += detail::quote_cpp_type_name(detail::clean_type_id(t->name()));
}
} else if (c == '!' && special_chars.find(*(pc + 1)) != std::string::npos) {
// typing::Literal escapes special characters with !
signature += *++pc;
} else if (c == '@') {
// `@^ ... @!` and `@$ ... @!` are used to force arg/return value type (see
// typing::Callable/detail::arg_descr/detail::return_descr)
if (*(pc + 1) == '^') {
is_return_value.emplace(false);
++pc;
continue;
}
if (*(pc + 1) == '$') {
is_return_value.emplace(true);
++pc;
continue;
}
if (*(pc + 1) == '!') {
is_return_value.pop();
++pc;
continue;
}
// Handle types that differ depending on whether they appear
// in an argument or a return value position (see io_name<text1, text2>).
// For named arguments (py::arg()) with noconvert set, return value type is used.
++pc;
if (!is_return_value.top()
&& (is_annotation
|| !(arg_index < func_rec->args.size()
&& !func_rec->args[arg_index].convert))) {
while (*pc != '\0' && *pc != '@') {
signature += *pc++;
}
if (*pc == '@') {
++pc;
}
while (*pc != '\0' && *pc != '@') {
++pc;
}
} else {
while (*pc != '\0' && *pc != '@') {
++pc;
}
if (*pc == '@') {
++pc;
}
while (*pc != '\0' && *pc != '@') {
signature += *pc++;
}
}
} else {
if (c == '-' && *(pc + 1) == '>') {
is_return_value.emplace(true);
}
signature += c;
}
}
return signature;
}

#if defined(_MSC_VER)
# define PYBIND11_COMPAT_STRDUP _strdup
#else
Expand Down Expand Up @@ -439,124 +567,9 @@ class cpp_function : public function {
}
#endif

/* Generate a proper function signature */
std::string signature;
size_t type_index = 0, arg_index = 0;
bool is_starred = false;
// `is_return_value.top()` is true if we are currently inside the return type of the
// signature. Using `@^`/`@$` we can force types to be arg/return types while `@!` pops
// back to the previous state.
std::stack<bool> is_return_value({false});
// The following characters have special meaning in the signature parsing. Literals
// containing these are escaped with `!`.
std::string special_chars("!@%{}-");
for (const auto *pc = text; *pc != '\0'; ++pc) {
const auto c = *pc;

if (c == '{') {
// Write arg name for everything except *args and **kwargs.
is_starred = *(pc + 1) == '*';
if (is_starred) {
continue;
}
// Separator for keyword-only arguments, placed before the kw
// arguments start (unless we are already putting an *args)
if (!rec->has_args && arg_index == rec->nargs_pos) {
signature += "*, ";
}
if (arg_index < rec->args.size() && rec->args[arg_index].name) {
signature += rec->args[arg_index].name;
} else if (arg_index == 0 && rec->is_method) {
signature += "self";
} else {
signature += "arg" + std::to_string(arg_index - (rec->is_method ? 1 : 0));
}
signature += ": ";
} else if (c == '}') {
// Write default value if available.
if (!is_starred && arg_index < rec->args.size() && rec->args[arg_index].descr) {
signature += " = ";
signature += detail::replace_newlines_and_squash(rec->args[arg_index].descr);
}
// Separator for positional-only arguments (placed after the
// argument, rather than before like *
if (rec->nargs_pos_only > 0 && (arg_index + 1) == rec->nargs_pos_only) {
signature += ", /";
}
if (!is_starred) {
arg_index++;
}
} else if (c == '%') {
const std::type_info *t = types[type_index++];
if (!t) {
pybind11_fail("Internal error while parsing type signature (1)");
}
if (auto *tinfo = detail::get_type_info(*t)) {
handle th((PyObject *) tinfo->type);
signature += th.attr("__module__").cast<std::string>() + "."
+ th.attr("__qualname__").cast<std::string>();
} else if (rec->is_new_style_constructor && arg_index == 0) {
// A new-style `__init__` takes `self` as `value_and_holder`.
// Rewrite it to the proper class type.
signature += rec->scope.attr("__module__").cast<std::string>() + "."
+ rec->scope.attr("__qualname__").cast<std::string>();
} else {
signature += detail::quote_cpp_type_name(detail::clean_type_id(t->name()));
}
} else if (c == '!' && special_chars.find(*(pc + 1)) != std::string::npos) {
// typing::Literal escapes special characters with !
signature += *++pc;
} else if (c == '@') {
// `@^ ... @!` and `@$ ... @!` are used to force arg/return value type (see
// typing::Callable/detail::arg_descr/detail::return_descr)
if (*(pc + 1) == '^') {
is_return_value.emplace(false);
++pc;
continue;
}
if (*(pc + 1) == '$') {
is_return_value.emplace(true);
++pc;
continue;
}
if (*(pc + 1) == '!') {
is_return_value.pop();
++pc;
continue;
}
// Handle types that differ depending on whether they appear
// in an argument or a return value position (see io_name<text1, text2>).
// For named arguments (py::arg()) with noconvert set, return value type is used.
++pc;
if (!is_return_value.top()
&& !(arg_index < rec->args.size() && !rec->args[arg_index].convert)) {
while (*pc != '\0' && *pc != '@') {
signature += *pc++;
}
if (*pc == '@') {
++pc;
}
while (*pc != '\0' && *pc != '@') {
++pc;
}
} else {
while (*pc != '\0' && *pc != '@') {
++pc;
}
if (*pc == '@') {
++pc;
}
while (*pc != '\0' && *pc != '@') {
signature += *pc++;
}
}
} else {
if (c == '-' && *(pc + 1) == '>') {
is_return_value.emplace(true);
}
signature += c;
}
}
std::string signature
= detail::generate_function_signature(text, rec, types, type_index, arg_index);

if (arg_index != args - rec->has_args - rec->has_kwargs || types[type_index] != nullptr) {
pybind11_fail("Internal error while parsing type signature (2)");
Expand Down
4 changes: 3 additions & 1 deletion include/pybind11/typing.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ struct handle_type_name<typing::Optional<T>> {

template <typename T>
struct handle_type_name<typing::Final<T>> {
static constexpr auto name = const_name("Final[") + make_caster<T>::name + const_name("]");
static constexpr auto name = const_name("Final[")
+ ::pybind11::detail::return_descr(make_caster<T>::name)
+ const_name("]");
};

template <typename T>
Expand Down
4 changes: 4 additions & 0 deletions tests/test_builtin_casters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ TEST_SUBMODULE(builtin_casters, m) {
m.def("int_passthrough", [](int arg) { return arg; });
m.def("int_passthrough_noconvert", [](int arg) { return arg; }, py::arg{}.noconvert());

// test_float_convert
m.def("float_passthrough", [](float arg) { return arg; });
m.def("float_passthrough_noconvert", [](float arg) { return arg; }, py::arg{}.noconvert());

// test_tuple
m.def(
"pair_passthrough",
Expand Down
Loading
Loading