Skip to content

fix: add support for shared_ptr<const T> in py::init() with smart_holder #5731

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 2 commits into from
Jun 17, 2025

Conversation

rwgk
Copy link
Collaborator

@rwgk rwgk commented Jun 17, 2025

Description

Experiments related to PR #5718 triggered a review of factory constructor support in combination with py::smart_holder.

This PR adds support for std::shared_ptr<const T> in py::init() by introducing a minimal construct() overload with const_pointer_cast.

No change is needed for std::unique_ptr<const T>, which is already supported by existing overloads via template deduction (this just went unnoticed before).

Uncommenting related test code in in test_class_sh_factory_constructors.py is all that's needed to activate corresponding runtime coverage.

Suggested changelog entry:

  • Added support for std::shared_ptr<const T> in py::init() when using py::smart_holder, complementing existing support for std::unique_ptr<const T>.

rwgk added 2 commits June 16, 2025 22:10
….def(py::init(&rtrn_uqcp))` and `.def(py::init(&rtrn_udcp))`, which happen to work already (not sure what change in the past made those work).
@henryiii henryiii requested a review from Copilot June 17, 2025 18:03
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enables std::shared_ptr<const T> support in py::init() when using py::smart_holder, complementing existing std::unique_ptr<const T> support by:

  • Introducing a construct_from_shared_ptr helper and overloads for shared_ptr<const Cpp<Class>> in init.h.
  • Uncommenting and activating related Python tests in test_class_sh_factory_constructors.py.
  • Enabling corresponding C++ factory constructor bindings in test_class_sh_factory_constructors.cpp.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
include/pybind11/detail/init.h Added construct_from_shared_ptr and overload for shared_ptr<const>
tests/test_class_sh_factory_constructors.py Uncommented assertions for shared_ptr<const> and unique_ptr<const>
tests/test_class_sh_factory_constructors.cpp Removed commented-out .def(py::init(...)) lines and enabled constructors
Comments suppressed due to low confidence (3)

tests/test_class_sh_factory_constructors.cpp:110

  • [nitpick] Remove this stale commented-out block to keep the test concise and focused on the enabled constructor.
    py::classh<atyp_shcp>(m, "atyp_shcp")

tests/test_class_sh_factory_constructors.cpp:118

  • [nitpick] This comment is no longer needed once the constructor is enabled; consider deleting it to reduce noise.
    py::classh<atyp_uqcp>(m, "atyp_uqcp")

tests/test_class_sh_factory_constructors.cpp:126

  • [nitpick] Stale commented code can be removed now that the smart_holder-based init is active for this type.
    py::classh<atyp_udcp>(m, "atyp_udcp")

@@ -13,11 +13,11 @@ def test_atyp_factories():
# sert m.atyp_cptr().get_mtxt() == "Cptr"
Copy link
Preview

Copilot AI Jun 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a typo in the comment: change "sert" to "assert" to match the style of the other lines.

Copilot uses AI. Check for mistakes.

@rwgk rwgk merged commit 86e82dd into pybind:master Jun 17, 2025
82 checks passed
@rwgk rwgk deleted the factory_function_shared_ptr_const_T branch June 17, 2025 19:17
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Jun 17, 2025
@henryiii henryiii changed the title Add support for shared_ptr<const T> in py::init() with smart_holder fix: add support for shared_ptr<const T> in py::init() with smart_holder Jun 19, 2025
@rwgk rwgk removed the needs changelog Possibly needs a changelog entry label Jul 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants