Skip to content

Commit 18fd77f

Browse files
Merge branch 'master' into pep695-generic-docstring
2 parents af80bc3 + af796d0 commit 18fd77f

31 files changed

+325
-126
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ repos:
3232

3333
# Ruff, the Python auto-correcting linter/formatter written in Rust
3434
- repo: https://github.com/astral-sh/ruff-pre-commit
35-
rev: v0.13.3
35+
rev: v0.14.3
3636
hooks:
3737
- id: ruff-check
3838
args: ["--fix", "--show-fixes"]
@@ -135,14 +135,14 @@ repos:
135135

136136
# PyLint has native support - not always usable, but works for us
137137
- repo: https://github.com/PyCQA/pylint
138-
rev: "v3.3.9"
138+
rev: "v4.0.2"
139139
hooks:
140140
- id: pylint
141141
files: ^pybind11
142142

143143
# Check schemas on some of our YAML files
144144
- repo: https://github.com/python-jsonschema/check-jsonschema
145-
rev: 0.34.0
145+
rev: 0.34.1
146146
hooks:
147147
- id: check-readthedocs
148148
- id: check-github-workflows

docs/upgrade.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ C++ enumerations as native Python types — typically standard-library
4848
``enum.Enum`` or related subclasses. This provides improved integration with
4949
Python's enum system, compared to the older (now deprecated) ``py::enum_``.
5050
See `#5555 <https://github.com/pybind/pybind11/pull/5555>`_ for details.
51+
Note that ``#include <pybind11/native_enum.h>`` is not included automatically
52+
and must be added explicitly.
5153

5254
Functions exposed with pybind11 are now pickleable. This removes a
5355
long-standing obstacle when using pybind11-bound functions with Python features

include/pybind11/attr.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,12 @@ struct process_attributes {
764764
}
765765
};
766766

767+
template <typename T>
768+
struct is_keep_alive : std::false_type {};
769+
770+
template <size_t Nurse, size_t Patient>
771+
struct is_keep_alive<keep_alive<Nurse, Patient>> : std::true_type {};
772+
767773
template <typename T>
768774
using is_call_guard = is_instantiation<call_guard, T>;
769775

include/pybind11/cast.h

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,12 @@ struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value && !is_std_char_t
347347
return PyLong_FromUnsignedLongLong((unsigned long long) src);
348348
}
349349

350-
PYBIND11_TYPE_CASTER(T,
351-
io_name<std::is_integral<T>::value>(
352-
"typing.SupportsInt", "int", "typing.SupportsFloat", "float"));
350+
PYBIND11_TYPE_CASTER(
351+
T,
352+
io_name<std::is_integral<T>::value>("typing.SupportsInt | typing.SupportsIndex",
353+
"int",
354+
"typing.SupportsFloat | typing.SupportsIndex",
355+
"float"));
353356
};
354357

355358
template <typename T>
@@ -1465,21 +1468,24 @@ template <>
14651468
struct handle_type_name<weakref> {
14661469
static constexpr auto name = const_name("weakref.ReferenceType");
14671470
};
1471+
// args/Args/kwargs/KWArgs have name as well as typehint included
14681472
template <>
14691473
struct handle_type_name<args> {
1470-
static constexpr auto name = const_name("*args");
1474+
static constexpr auto name = io_name("*args", "tuple");
14711475
};
14721476
template <typename T>
14731477
struct handle_type_name<Args<T>> {
1474-
static constexpr auto name = const_name("*args: ") + make_caster<T>::name;
1478+
static constexpr auto name
1479+
= io_name("*args: ", "tuple[") + make_caster<T>::name + io_name("", ", ...]");
14751480
};
14761481
template <>
14771482
struct handle_type_name<kwargs> {
1478-
static constexpr auto name = const_name("**kwargs");
1483+
static constexpr auto name = io_name("**kwargs", "dict[str, typing.Any]");
14791484
};
14801485
template <typename T>
14811486
struct handle_type_name<KWArgs<T>> {
1482-
static constexpr auto name = const_name("**kwargs: ") + make_caster<T>::name;
1487+
static constexpr auto name
1488+
= io_name("**kwargs: ", "dict[str, ") + make_caster<T>::name + io_name("", "]");
14831489
};
14841490
template <>
14851491
struct handle_type_name<obj_attr_accessor> {
@@ -1905,13 +1911,20 @@ inline cast_error cast_error_unable_to_convert_call_arg(const std::string &name,
19051911
}
19061912
#endif
19071913

1914+
namespace typing {
1915+
template <typename... Types>
1916+
class Tuple : public tuple {
1917+
using tuple::tuple;
1918+
};
1919+
} // namespace typing
1920+
19081921
template <return_value_policy policy = return_value_policy::automatic_reference>
1909-
tuple make_tuple() {
1922+
typing::Tuple<> make_tuple() {
19101923
return tuple(0);
19111924
}
19121925

19131926
template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args>
1914-
tuple make_tuple(Args &&...args_) {
1927+
typing::Tuple<Args...> make_tuple(Args &&...args_) {
19151928
constexpr size_t size = sizeof...(Args);
19161929
std::array<object, size> args{{reinterpret_steal<object>(
19171930
detail::make_caster<Args>::cast(std::forward<Args>(args_), policy, nullptr))...}};
@@ -1930,7 +1943,12 @@ tuple make_tuple(Args &&...args_) {
19301943
for (auto &arg_value : args) {
19311944
PyTuple_SET_ITEM(result.ptr(), counter++, arg_value.release().ptr());
19321945
}
1946+
PYBIND11_WARNING_PUSH
1947+
#ifdef PYBIND11_DETECTED_CLANG_WITH_MISLEADING_CALL_STD_MOVE_EXPLICITLY_WARNING
1948+
PYBIND11_WARNING_DISABLE_CLANG("-Wreturn-std-move")
1949+
#endif
19331950
return result;
1951+
PYBIND11_WARNING_POP
19341952
}
19351953

19361954
/// \ingroup annotations

include/pybind11/complex.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,23 @@ class type_caster<std::complex<T>> {
5454
if (!convert && !PyComplex_Check(src.ptr())) {
5555
return false;
5656
}
57-
Py_complex result = PyComplex_AsCComplex(src.ptr());
57+
handle src_or_index = src;
58+
// PyPy: 7.3.7's 3.8 does not implement PyLong_*'s __index__ calls.
59+
// The same logic is used in numeric_caster for ints and floats
60+
#if defined(PYPY_VERSION)
61+
object index;
62+
if (PYBIND11_INDEX_CHECK(src.ptr())) {
63+
index = reinterpret_steal<object>(PyNumber_Index(src.ptr()));
64+
if (!index) {
65+
PyErr_Clear();
66+
if (!convert)
67+
return false;
68+
} else {
69+
src_or_index = index;
70+
}
71+
}
72+
#endif
73+
Py_complex result = PyComplex_AsCComplex(src_or_index.ptr());
5874
if (result.real == -1.0 && PyErr_Occurred()) {
5975
PyErr_Clear();
6076
return false;
@@ -68,7 +84,10 @@ class type_caster<std::complex<T>> {
6884
return PyComplex_FromDoubles((double) src.real(), (double) src.imag());
6985
}
7086

71-
PYBIND11_TYPE_CASTER(std::complex<T>, const_name("complex"));
87+
PYBIND11_TYPE_CASTER(
88+
std::complex<T>,
89+
io_name("typing.SupportsComplex | typing.SupportsFloat | typing.SupportsIndex",
90+
"complex"));
7291
};
7392
PYBIND11_NAMESPACE_END(detail)
7493
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

include/pybind11/detail/common.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@
103103
# define PYBIND11_DTOR_CONSTEXPR
104104
#endif
105105

106+
#if defined(PYBIND11_CPP20) && defined(__has_include) && __has_include(<barrier>)
107+
# define PYBIND11_HAS_STD_BARRIER 1
108+
#endif
109+
106110
// Compiler version assertions
107111
#if defined(__INTEL_COMPILER)
108112
# if __INTEL_COMPILER < 1800
@@ -322,6 +326,13 @@
322326
#define PYBIND11_BYTES_AS_STRING PyBytes_AsString
323327
#define PYBIND11_BYTES_SIZE PyBytes_Size
324328
#define PYBIND11_LONG_CHECK(o) PyLong_Check(o)
329+
// In PyPy 7.3.3, `PyIndex_Check` is implemented by calling `__index__`,
330+
// while CPython only considers the existence of `nb_index`/`__index__`.
331+
#if !defined(PYPY_VERSION)
332+
# define PYBIND11_INDEX_CHECK(o) PyIndex_Check(o)
333+
#else
334+
# define PYBIND11_INDEX_CHECK(o) hasattr(o, "__index__")
335+
#endif
325336
#define PYBIND11_LONG_AS_LONGLONG(o) PyLong_AsLongLong(o)
326337
#define PYBIND11_LONG_FROM_SIGNED(o) PyLong_FromSsize_t((ssize_t) (o))
327338
#define PYBIND11_LONG_FROM_UNSIGNED(o) PyLong_FromSize_t((size_t) (o))

include/pybind11/detail/init.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,15 @@ template <typename Get,
501501
typename NewInstance,
502502
typename ArgState>
503503
struct pickle_factory<Get, Set, RetState(Self), NewInstance(ArgState)> {
504-
static_assert(std::is_same<intrinsic_t<RetState>, intrinsic_t<ArgState>>::value,
505-
"The type returned by `__getstate__` must be the same "
506-
"as the argument accepted by `__setstate__`");
504+
using Ret = intrinsic_t<RetState>;
505+
using Arg = intrinsic_t<ArgState>;
506+
507+
// Subclasses are now allowed for support between type hint and generic versions of types
508+
// (e.g.) typing::List <--> list
509+
static_assert(std::is_same<Ret, Arg>::value || std::is_base_of<Ret, Arg>::value
510+
|| std::is_base_of<Arg, Ret>::value,
511+
"The type returned by `__getstate__` must be the same or subclass of the "
512+
"argument accepted by `__setstate__`");
507513

508514
remove_reference_t<Get> get;
509515
remove_reference_t<Set> set;

include/pybind11/gil.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ class gil_scoped_acquire {
120120
pybind11_fail("scoped_acquire::dec_ref(): internal error!");
121121
}
122122
# endif
123+
// Make sure that PyThreadState_Clear is not recursively called by finalizers.
124+
// See issue #5827
125+
++tstate->gilstate_counter;
123126
PyThreadState_Clear(tstate);
127+
--tstate->gilstate_counter;
124128
if (active) {
125129
PyThreadState_DeleteCurrent();
126130
}

include/pybind11/pybind11.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ inline std::string generate_function_signature(const char *type_caster_name_fiel
168168
const auto c = *pc;
169169
if (c == '{') {
170170
// Write arg name for everything except *args and **kwargs.
171-
is_starred = *(pc + 1) == '*';
171+
// Detect {@*args...} or {@**kwargs...}
172+
is_starred = *(pc + 1) == '@' && *(pc + 2) == '*';
172173
if (is_starred) {
173174
continue;
174175
}
@@ -2477,6 +2478,12 @@ class class_ : public detail::generic_type {
24772478
const Extra &...extra) {
24782479
static_assert(0 == detail::constexpr_sum(std::is_base_of<arg, Extra>::value...),
24792480
"Argument annotations are not allowed for properties");
2481+
static_assert(0 == detail::constexpr_sum(detail::is_call_guard<Extra>::value...),
2482+
"def_property family does not currently support call_guard. Use a "
2483+
"py::cpp_function instead.");
2484+
static_assert(0 == detail::constexpr_sum(detail::is_keep_alive<Extra>::value...),
2485+
"def_property family does not currently support keep_alive. Use a "
2486+
"py::cpp_function instead.");
24802487
auto rec_fget = get_function_record(fget), rec_fset = get_function_record(fset);
24812488
auto *rec_active = rec_fget;
24822489
if (rec_fget) {

include/pybind11/typing.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ PYBIND11_NAMESPACE_BEGIN(typing)
3434
There is no additional enforcement of types at runtime.
3535
*/
3636

37-
template <typename... Types>
38-
class Tuple : public tuple {
39-
using tuple::tuple;
40-
};
37+
// Tuple type hint defined in cast.h for use in py::make_tuple to avoid circular includes
4138

4239
template <typename K, typename V>
4340
class Dict : public dict {

0 commit comments

Comments
 (0)