From 2be85c604167e4097edab9cd7b4215e85f1ae8d1 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Tue, 25 Jun 2024 22:53:15 -0400 Subject: [PATCH 1/6] feat(types): adds support for TypeGuard and TypeIs (#5194) * Adds support for TypeGuard and TypeIs * style: pre-commit fixes --------- Co-authored-by: Henry Schreiner Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- include/pybind11/typing.h | 22 ++++++++++++++++++++++ tests/test_pytypes.cpp | 7 +++++++ tests/test_pytypes.py | 11 +++++++++++ 3 files changed, 40 insertions(+) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index 55f1f949d..cf70b739e 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -80,6 +80,16 @@ class Optional : public object { using object::object; }; +template +class TypeGuard : public bool_ { + using bool_::bool_; +}; + +template +class TypeIs : public bool_ { + using bool_::bool_; +}; + class NoReturn : public none { using none::none; }; @@ -87,6 +97,7 @@ class NoReturn : public none { class Never : public none { using none::none; }; + #if defined(__cpp_nontype_template_parameter_class) template struct StringLiteral { @@ -183,6 +194,16 @@ struct handle_type_name> { static constexpr auto name = const_name("Optional[") + make_caster::name + const_name("]"); }; +template +struct handle_type_name> { + static constexpr auto name = const_name("TypeGuard[") + make_caster::name + const_name("]"); +}; + +template +struct handle_type_name> { + static constexpr auto name = const_name("TypeIs[") + make_caster::name + const_name("]"); +}; + template <> struct handle_type_name { static constexpr auto name = const_name("NoReturn"); @@ -192,6 +213,7 @@ template <> struct handle_type_name { static constexpr auto name = const_name("Never"); }; + #if defined(__cpp_nontype_template_parameter_class) template struct handle_type_name> { diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index ce6b93326..6b347894b 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -892,8 +892,15 @@ TEST_SUBMODULE(pytypes, m) { return list; }); + m.def("annotate_type_guard", [](py::object &o) -> py::typing::TypeGuard { + return py::isinstance(o); + }); + m.def("annotate_type_is", + [](py::object &o) -> py::typing::TypeIs { return py::isinstance(o); }); + m.def("annotate_no_return", []() -> py::typing::NoReturn { throw 0; }); m.def("annotate_never", []() -> py::typing::Never { throw 0; }); + m.def("annotate_optional_to_object", [](py::typing::Optional &o) -> py::object { return o; }); diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 923cebaf5..92a3a36bf 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -991,6 +991,17 @@ def test_optional_annotations(doc): ) +def test_type_guard_annotations(doc): + assert ( + doc(m.annotate_type_guard) + == "annotate_type_guard(arg0: object) -> TypeGuard[str]" + ) + + +def test_type_is_annotations(doc): + assert doc(m.annotate_type_is) == "annotate_type_is(arg0: object) -> TypeIs[str]" + + def test_no_return_annotation(doc): assert doc(m.annotate_no_return) == "annotate_no_return() -> NoReturn" From 0c69e1eb2177fa8f8580632c7b1f97fdb606ce8f Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 25 Jun 2024 23:51:27 -0400 Subject: [PATCH 2/6] chore: prepare for 2.13.0 (#5198) * chore: prepare for 2.13.0 Signed-off-by: Henry Schreiner * Update changelog.rst --------- Signed-off-by: Henry Schreiner --- .github/workflows/pip.yml | 2 +- docs/changelog.rst | 13 ++++++++++--- include/pybind11/detail/common.h | 4 ++-- noxfile.py | 2 +- pybind11/_version.py | 2 +- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/.github/workflows/pip.yml b/.github/workflows/pip.yml index 6d453eabe..a054ce695 100644 --- a/.github/workflows/pip.yml +++ b/.github/workflows/pip.yml @@ -58,7 +58,7 @@ jobs: - name: Prepare env run: | - python -m pip install -r tests/requirements.txt build twine!=5.1.0 + python -m pip install -r tests/requirements.txt build twine - name: Python Packaging tests run: pytest tests/extra_python_package/ diff --git a/docs/changelog.rst b/docs/changelog.rst index 0a556973e..7135e6531 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -33,11 +33,15 @@ New Features: .. feat(types) -* Support for ``type[T]`` was added to pybind11/typing.h. +* Support for ``Union``, ``Optional``, ``type[T]``, ``typing.TypeGuard``, + ``typing.TypeIs``, ``typing.Never``, ``typing.NoReturn`` and + ``typing.Literal`` was added to ``pybind11/typing.h``. `#5166 `_ - -* ``Union`` and ``Optional`` were added to ``pybind11/typing.h``. `#5165 `_ + `#5194 `_ + `#5193 `_ + `#5192 `_ + .. feat(cmake) @@ -93,6 +97,9 @@ CI: * Use ``macos-13`` (Intel) for CI jobs for now (will drop Python 3.7 soon). `#5109 `_ +* Releases now have artifact attestations, visible at + https://github.com/pybind/pybind11/attestations. + `#5196 `_ Other: diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 698421d84..a83302f13 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -11,11 +11,11 @@ #define PYBIND11_VERSION_MAJOR 2 #define PYBIND11_VERSION_MINOR 13 -#define PYBIND11_VERSION_PATCH 0.dev1 +#define PYBIND11_VERSION_PATCH 0 // Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html // Additional convention: 0xD = dev -#define PYBIND11_VERSION_HEX 0x020D00D1 +#define PYBIND11_VERSION_HEX 0x020D0000 // Define some generic pybind11 helper macros for warning management. // diff --git a/noxfile.py b/noxfile.py index be75def5d..e9a2fa8fe 100644 --- a/noxfile.py +++ b/noxfile.py @@ -45,7 +45,7 @@ def tests_packaging(session: nox.Session) -> None: Run the packaging tests. """ - session.install("-r", "tests/requirements.txt") + session.install("-r", "tests/requirements.txt", "pip") session.run("pytest", "tests/extra_python_package", *session.posargs) diff --git a/pybind11/_version.py b/pybind11/_version.py index f71abbcdb..5795f4406 100644 --- a/pybind11/_version.py +++ b/pybind11/_version.py @@ -8,5 +8,5 @@ def _to_int(s: str) -> int | str: return s -__version__ = "2.13.0.dev1" +__version__ = "2.13.0" version_info = tuple(_to_int(s) for s in __version__.split(".")) From 895e657220c1ff2058a4cfee6b185bd0582b0157 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 26 Jun 2024 00:42:54 -0400 Subject: [PATCH 3/6] chore: back to work Signed-off-by: Henry Schreiner --- include/pybind11/detail/common.h | 6 +++--- pybind11/_version.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index a83302f13..fc66e6f75 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -10,12 +10,12 @@ #pragma once #define PYBIND11_VERSION_MAJOR 2 -#define PYBIND11_VERSION_MINOR 13 -#define PYBIND11_VERSION_PATCH 0 +#define PYBIND11_VERSION_MINOR 14 +#define PYBIND11_VERSION_PATCH 0.dev1 // Similar to Python's convention: https://docs.python.org/3/c-api/apiabiversion.html // Additional convention: 0xD = dev -#define PYBIND11_VERSION_HEX 0x020D0000 +#define PYBIND11_VERSION_HEX 0x020E00D1 // Define some generic pybind11 helper macros for warning management. // diff --git a/pybind11/_version.py b/pybind11/_version.py index 5795f4406..c5cc1a0b0 100644 --- a/pybind11/_version.py +++ b/pybind11/_version.py @@ -8,5 +8,5 @@ def _to_int(s: str) -> int | str: return s -__version__ = "2.13.0" +__version__ = "2.14.0.dev1" version_info = tuple(_to_int(s) for s in __version__.split(".")) From 2e35470cffc47903dd47b7e68c7acf4302fd7057 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Wed, 26 Jun 2024 15:46:46 -0400 Subject: [PATCH 4/6] fix: use manual padding of instance_map_shard (#5200) * Use manual padding of instance_map_shard. The alignas(64) specifier requires aligned allocation, which is not available on macOS when targeting versions before 10.14. * Add 'see #5200' * Update include/pybind11/detail/internals.h * Update include/pybind11/detail/internals.h --------- Co-authored-by: Henry Schreiner --- include/pybind11/detail/internals.h | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 92a851602..e61c1687f 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -148,18 +148,14 @@ struct override_hash { using instance_map = std::unordered_multimap; -// 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. From 4bd538a40a45803b24f95ef4fb3de5d773e2d426 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Wed, 26 Jun 2024 16:34:06 -0400 Subject: [PATCH 5/6] feat(types): add support for Typing.Callable Special Case (#5202) * Add special case * linty --- include/pybind11/typing.h | 8 ++++++++ tests/test_pytypes.cpp | 1 + tests/test_pytypes.py | 7 +++++++ 3 files changed, 16 insertions(+) diff --git a/include/pybind11/typing.h b/include/pybind11/typing.h index cf70b739e..1442cdc7f 100644 --- a/include/pybind11/typing.h +++ b/include/pybind11/typing.h @@ -177,6 +177,14 @@ struct handle_type_name> { + const_name("], ") + make_caster::name + const_name("]"); }; +template +struct handle_type_name> { + // PEP 484 specifies this syntax for defining only return types of callables + using retval_type = conditional_t::value, void_type, Return>; + static constexpr auto name + = const_name("Callable[..., ") + make_caster::name + const_name("]"); +}; + template struct handle_type_name> { static constexpr auto name = const_name("type[") + make_caster::name + const_name("]"); diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 6b347894b..7c30978ce 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -865,6 +865,7 @@ TEST_SUBMODULE(pytypes, m) { m.def("annotate_fn", [](const py::typing::Callable, py::str)> &) {}); + m.def("annotate_fn_only_return", [](const py::typing::Callable &) {}); m.def("annotate_type", [](const py::typing::Type &t) -> py::type { return t; }); m.def("annotate_union", diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 92a3a36bf..30931e0b9 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -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" From 57287b578d58af016cd571f6eacee19300d37f2d Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 26 Jun 2024 18:14:17 -0400 Subject: [PATCH 6/6] docs: prepare for 2.13.1 (#5203) Signed-off-by: Henry Schreiner --- docs/changelog.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 7135e6531..ab6c713c1 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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 `_ + +Bug fixes: + +* Avoid aligned allocation in free-threaded build in order to support macOS + versions before 10.14. + `#5200 `_ + Version 2.13.0 (June 25, 2024) ------------------------------