Repository navigation
Conversation
Add tests for function and method __qualname__ attributes. I believe these tests will fail with the current implementation but they should pass. These are tests for the correct behaviour of pybind#2059 and pybind#5774
CPython computes the __qualname__ of a builtin function from its __self__, which is the function record. The record of a function that is not in a class is now a module subclass, which gives no prefix. The record type for the functions in a class has the __qualname__ of that class. Assisted-by: ClaudeCode:claude-fable-5-2
PyType_FromSpecWithBases accepts a single class only from Python 3.10. Also discard the release() result explicitly for clang-tidy. Assisted-by: ClaudeCode:claude-fable-5-2
Assisted-by: ClaudeCode:claude-opus-5-5
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Experiment.
🤖 AI text below 🤖
Description
Fixes #5774 and #2059. Includes the tests from #5775.
CPython computes the
__qualname__of a builtin function from its__self__, which pybind11 sets to the function record. The result waspybind11_detail_function_record_v1_....name. This PR changes the type of the record object:modulesubclass. CPython adds no prefix if__self__is a module, so the result ismodule_func.__qualname__of the class. The result isNestBase.Nested.fn. The type is in the class__dict__as__pybind11_function_record_type__, so that all extension modules use the same one.The dispatcher has two entry points, one for each record layout, so there is no type check for each call.
Trade-offs to review:
__pybind11_function_record_type__shows invars(cls). A map inlocal_internalswith a weakref cleanup would hide it, with more code.std::functioncaster uses the Python round trip. The class-level records keep the old name and layout.__self__of a module-level function is an empty module-like object without a__dict__. Pickle of this object continues to work.local_internalshas one more pointer.An alternative that does not work: the record in the
PyCMethodclass slot, with the scope as__self__.builtin_method.__doc__is alwaysNoneon CPython 3.12 to 3.15.Tested locally on CPython 3.11 (subset), 3.13, the stable ABI (3.14), abi3t (3.15t), and the precompiled mode. Not tested locally on GraalPy.
Suggested changelog entry:
__qualname__of bound functions and methods is now correct on CPython (for exampleClass.method, notpybind11_detail_function_record_v1_....method).