Skip to content

chore: collect all #define PYBIND11_HAS_... macros in pybind11/detail/common.h #5647

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 1 commit into from
May 9, 2025

Conversation

rwgk
Copy link
Collaborator

@rwgk rwgk commented May 8, 2025

Description

The trigger for working on this PR was seeing complicated code like this.

With the help of my best friend Chat Geepeetee:

This PR consolidates all PYBIND11_HAS_... feature macros into pybind11/detail/common.h.

Benefits:

  • Simplifies user-side compatibility checks:

    • Users can reliably use #ifdef guards on PYBIND11_HAS_... macros without needing to worry about which pybind11 headers have been included.
  • Enables safer header inclusion patterns:

    • Users can conditionally include newer pybind11 headers based on feature availability, regardless of current include state.
  • Improves internal maintainability:

    • Simplifies refactoring within pybind11 itself by eliminating the need to manage include order just to ensure a given PYBIND11_HAS_... macro is visible.

This change promotes cleaner and more robust code, both for users and within pybind11's internals.


Minor details:

  • All PYBIND11_HAS_... macros are now consistently defined as 1.
  • The awkward PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL macro is no longer needed (see changes in include/pybind11/stl/filesystem.h and tests/test_stl.cpp).

Before this PR:

$ git switch master
Switched to branch 'master'
Your branch is up to date with 'upstream/master'.

$ git grep '^# *define  *PYBIND11_HAS_' | grep -v '^include/pybind11/detail/common.h:' | cutniq
include/pybind11/detail/cpp_conduit.h
include/pybind11/detail/internals.h
include/pybind11/detail/native_enum_data.h
include/pybind11/functional.h
include/pybind11/stl/filesystem.h
tests/test_stl.cpp

With this PR:

$ git switch collect_define_pybind11_has
Switched to branch 'collect_define_pybind11_has'

$ git grep '^# *define  *PYBIND11_HAS_' | grep -v '^include/pybind11/detail/common.h:' | cutniq

(no output)

Suggested changelog entry:

Consolidated all ``PYBIND11_HAS_...`` feature macros into ``pybind11/detail/common.h`` to streamline backward compatibility checks and simplify internal refactoring. This change ensures consistent macro availability regardless of header inclusion order.

@rwgk rwgk mentioned this pull request May 8, 2025
@rwgk rwgk marked this pull request as ready for review May 9, 2025 03:53
@rwgk rwgk requested a review from henryiii May 9, 2025 03:53
@rwgk
Copy link
Collaborator Author

rwgk commented May 9, 2025

@XuehaiPan for a second set of eyes

@henryiii
Copy link
Collaborator

henryiii commented May 9, 2025

With the help of my best friend Chat Geepeete

I think you can just click "request Copilot review" and Copilot basically generates this, only as a followup comment.

@henryiii henryiii requested a review from Copilot May 9, 2025 05:32
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 consolidates all feature macros starting with PYBIND11_HAS_ into a single header (pybind11/detail/common.h) to simplify user compatibility checks and improve internal header inclusion order. Key changes include:

  • Removing separate definitions of PYBIND11_HAS_* macros from various files and centralizing them in common.h.
  • Updating filesystem support in both tests and header code to rely on the consolidated filesystem macros.
  • Eliminating legacy or redundant macros such as PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL and PYBIND11_HAS_INTERNALS_WITH_SMART_HOLDER_SUPPORT.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/test_stl.cpp Removed legacy filesystem optional macro and updated include guard logic.
include/pybind11/stl/filesystem.h Refactored conditional filesystem includes to use centralized macros.
include/pybind11/functional.h Removed standalone PYBIND11_HAS_TYPE_CASTER_STD_FUNCTION_SPECIALIZATIONS macro.
include/pybind11/detail/native_enum_data.h Removed legacy native enum macro definition.
include/pybind11/detail/internals.h Removed backward compatibility macro for smart holder support.
include/pybind11/detail/cpp_conduit.h Removed standalone cpp conduit macro definition.
include/pybind11/detail/common.h Centralized all feature macros and updated conditions for newer standards.

&& !defined(PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL)
# error \
"Neither #include <filesystem> nor #include <experimental/filesystem is available. (Use -DPYBIND11_HAS_FILESYSTEM_IS_OPTIONAL to ignore.)"
#if defined(PYBIND11_HAS_FILESYSTEM)
Copy link
Preview

Copilot AI May 9, 2025

Choose a reason for hiding this comment

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

The removal of the PYBIND11_HAS_FILESYSTEM_IS_OPTIONAL branch changes the behavior for environments where neither nor <experimental/filesystem> is available. Consider updating the documentation or migration notes to inform users of this breaking change.

Copilot uses AI. Check for mistakes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I reasoned this out with the help of Chat:

https://chatgpt.com/share/681da436-a5fc-8008-8d13-402e8bcbaa8e

✅ Conclusion
You're preserving — and even clarifying — expected behavior by:

Removing a test-only macro,

Consolidating detection macros in a single header,

Ensuring that #include <pybind11/stl/filesystem.h> fails clearly if the compiler lacks support.

So: you’re not overlooking anything, and the PR is correct as is.

@rwgk
Copy link
Collaborator Author

rwgk commented May 9, 2025

Thanks for the reviews @henryiii and @XuehaiPan!

@rwgk rwgk merged commit c125cc7 into pybind:master May 9, 2025
65 checks passed
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label May 9, 2025
@rwgk rwgk deleted the collect_define_pybind11_has branch May 9, 2025 06:46
@henryiii henryiii changed the title Collect all #define PYBIND11_HAS_... macros in pybind11/detail/common.h chore: collect all #define PYBIND11_HAS_... macros in pybind11/detail/common.h May 17, 2025
@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label May 17, 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.

3 participants