Skip to content

Commit

Permalink
Merge branch 'sh_merge_master' into pybind11k_merge_sh
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Jun 27, 2024
2 parents 06bc2e8 + 08c83b8 commit 279e608
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
14 changes: 14 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ IN DEVELOPMENT

Changes will be summarized here periodically.

Version 2.13.1 (June 26, 2024)
------------------------------

New Features:

* Add support for ``Typing.Callable[..., T]``.
`#5202 <https://github.com/pybind/pybind11/pull/5202>`_

Bug fixes:

* Avoid aligned allocation in free-threaded build in order to support macOS
versions before 10.14.
`#5200 <https://github.com/pybind/pybind11/pull/5200>`_

Version 2.13.0 (June 25, 2024)
------------------------------

Expand Down
10 changes: 3 additions & 7 deletions include/pybind11/detail/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,14 @@ struct override_hash {

using instance_map = std::unordered_multimap<const void *, instance *>;

// ignore: structure was padded due to alignment specifier
PYBIND11_WARNING_PUSH
PYBIND11_WARNING_DISABLE_MSVC(4324)

// Instance map shards are used to reduce mutex contention in free-threaded Python.
struct alignas(64) instance_map_shard {
struct instance_map_shard {
std::mutex mutex;
instance_map registered_instances;
// alignas(64) would be better, but causes compile errors in macOS before 10.14 (see #5200)
char padding[64 - (sizeof(std::mutex) + sizeof(instance_map)) % 64];
};

PYBIND11_WARNING_POP

/// Internal data structure used to track registered instances and types.
/// Whenever binary incompatible changes are made to this structure,
/// `PYBIND11_INTERNALS_VERSION` must be incremented.
Expand Down
8 changes: 8 additions & 0 deletions include/pybind11/typing.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ struct handle_type_name<typing::Callable<Return(Args...)>> {
+ const_name("], ") + make_caster<retval_type>::name + const_name("]");
};

template <typename Return>
struct handle_type_name<typing::Callable<Return(ellipsis)>> {
// PEP 484 specifies this syntax for defining only return types of callables
using retval_type = conditional_t<std::is_same<Return, void>::value, void_type, Return>;
static constexpr auto name
= const_name("Callable[..., ") + make_caster<retval_type>::name + const_name("]");
};

template <typename T>
struct handle_type_name<typing::Type<T>> {
static constexpr auto name = const_name("type[") + make_caster<T>::name + const_name("]");
Expand Down
1 change: 1 addition & 0 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,7 @@ TEST_SUBMODULE(pytypes, m) {
m.def("annotate_fn",
[](const py::typing::Callable<int(py::typing::List<py::str>, py::str)> &) {});

m.def("annotate_fn_only_return", [](const py::typing::Callable<int(py::ellipsis)> &) {});
m.def("annotate_type", [](const py::typing::Type<int> &t) -> py::type { return t; });

m.def("annotate_union",
Expand Down
7 changes: 7 additions & 0 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,13 @@ def test_fn_annotations(doc):
)


def test_fn_return_only(doc):
assert (
doc(m.annotate_fn_only_return)
== "annotate_fn_only_return(arg0: Callable[..., int]) -> None"
)


def test_type_annotation(doc):
assert doc(m.annotate_type) == "annotate_type(arg0: type[int]) -> type"

Expand Down

0 comments on commit 279e608

Please sign in to comment.