You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
StdPairConverter does not support delegation for nested containers #250
When using std::vector<std::pair<T1, T2>> where T1 or T2 is a wrapped C++ class, Cython fails with:
Cannot convert 'vector[pair[String,AASequence]]' to Python object
Root Cause
StdPairConverter does not override supports_delegation(), so it returns False (the base class default). This means when StdVectorConverter processes vector<pair<...>>:
pair is not in names_of_wrapper_classes (it's an STL type)
_has_delegating_converter(tt) returns False because StdPairConverter.supports_delegation() is False
Code falls through to the else branch that relies on Cython's automatic conversion
Cython can't automatically convert pair<String, AASequence> because AASequence is a wrapped class
Reproduction
# In .pxd file:
libcpp_vector[libcpp_pair[String, AASequence]] getVariantSequences() except+ nogil
This generates code that Cython cannot compile.
Proposed Solution
Have StdPairConverter override supports_delegation() to return True:
This would allow StdVectorConverter to delegate to StdPairConverter.output_conversion() which already handles wrapped classes correctly (lines 801-826 in ConversionProvider.py).
Workaround
Currently using # wrap-ignore and implementing the method manually in a Python addon file.
#216 - Add lookup/membership tests for STL containers and fix variable name collision bug [merged] #227 - Add support for std::hash in wrap-hash directive [merged] #228 - Fix ArrayWrapper inlining to go to pyx only, not pxd [merged] #237 - Fix cross-module enum class imports for type assertions [merged] #243 - Design conversion provider delegation system [merged]
🧪 Issue enrichment is currently in open beta.
You can configure auto-planning by selecting labels in the issue_enrichment configuration.
To disable automatic issue enrichment, add the following to your .coderabbit.yaml:
issue_enrichment:
auto_enrich:
enabled: false
💬 Have feedback or questions? Drop into our discord!
Enable StdPairConverter to support delegation when nested inside container types like std::vector, allowing vector<pair<WrappedClass, T>> to compile correctly by having the vector converter delegate element conversion to the pair converter's existing wrapped-class handling logic.
Observations
The autowrap codebase uses a converter architecture where TypeConverterBase provides a supports_delegation() method (default returns False) that container converters like StdVectorConverter check via _has_delegating_converter() to determine whether to manually iterate and delegate element conversion or rely on Cython's automatic conversion. StdPairConverter already has complete implementations for input_conversion() and output_conversion() that properly handle wrapped classes, but it doesn't override supports_delegation(), causing container converters to skip delegation and fall back to Cython's automatic conversion which fails for wrapped classes.
Implementation Steps
Phase 1: Enable Delegation in StdPairConverter
Add the delegation support method to StdPairConverter so that container converters like StdVectorConverter will properly delegate to it when processing nested pairs containing wrapped classes.
Add supports_delegation() Override to StdPairConverter
Add a method override that enables delegation for the pair converter, allowing it to be invoked by container converters when pairs are nested inside vectors, sets, maps, etc.
Update autowrap/ConversionProvider.py in the StdPairConverter class to add a supports_delegation() method that returns True
Place the method alongside other overridden methods in the class (after matching_python_type_full() or near other short methods)
The method should follow the same pattern as StdStringUnicodeConverter.supports_delegation() (simple one-liner returning True)
🤖 Prompt for AI agents
Enable delegation support in StdPairConverter so that container converters will
properly delegate to it when processing nested pairs containing wrapped classes.
In `autowrap/ConversionProvider.py`, locate the `StdPairConverter` class and add
a `supports_delegation()` method override:
- The method should return `True` to enable delegation
- Follow the same pattern as `StdStringUnicodeConverter.supports_delegation()`
(simple one-liner)
- Place the method alongside other overridden methods in the class (after
`matching_python_type_full()` or near other short methods)
This allows container converters like `StdVectorConverter` to check via
`_has_delegating_converter()` and properly delegate element conversion to the
pair converter instead of falling back to Cython's automatic conversion.
Phase 2: Add Test Coverage
Create tests to verify that vector<pair<WrappedClass, T>> scenarios now work correctly for both input and output conversions.
Task 1: Add C++ Test Declarations
Declare test methods in the Cython declaration file that exercise vector-of-pair with wrapped classes in various configurations.
Update tests/test_files/libcpp_test.pxd to add method declarations for:
A method returning libcpp_vector[libcpp_pair[LibCppTest, int]] (wrapped class as first element)
A method returning libcpp_vector[libcpp_pair[int, LibCppTest]] (wrapped class as second element)
A method accepting libcpp_vector[libcpp_pair[LibCppTest, int]] as input parameter
Follow the existing naming convention (e.g., process215, process216, etc.) and declaration patterns from process3-process6
Task 2: Add C++ Test Implementations
Implement the C++ methods that the test declarations reference.
Update tests/test_files/libcpp_test.hpp to add implementations for the declared methods
For return methods: create and populate a vector of pairs with test data, return it
For input methods: iterate over the input vector, perform some operation on the wrapped class elements (e.g., call gett() or modify values), return a result that can be verified
Follow existing patterns from process3-process6 implementations
Task 3: Add Python Test Cases
Write Python tests that verify the end-to-end functionality of vector-of-pair with wrapped classes.
Update tests/test_code_generator_libcpp.py to add test cases that:
Call the return methods and verify the returned list contains properly wrapped objects
Verify wrapped class methods can be called on elements (e.g., result[0][0].gett())
Call the input method with a Python list of [WrappedClass, int] pairs and verify correct behavior
Test both first and second positions of the pair containing wrapped classes
Follow existing test patterns from the process3-process6 test cases
🤖 Prompt for AI agents
Add comprehensive test coverage for `vector<pair<WrappedClass, T>>` scenarios to
verify both input and output conversions work correctly.
**C++ Test Declarations (`tests/test_files/libcpp_test.pxd`):**
- Add method declarations following the existing naming convention (e.g.,
`process215`, `process216`, etc.) and patterns from `process3`-`process6`
- Declare a method returning `libcpp_vector[libcpp_pair[LibCppTest, int]]`
(wrapped class as first element)
- Declare a method returning `libcpp_vector[libcpp_pair[int, LibCppTest]]`
(wrapped class as second element)
- Declare a method accepting `libcpp_vector[libcpp_pair[LibCppTest, int]]` as
input parameter
**C++ Test Implementations (`tests/test_files/libcpp_test.hpp`):**
- Follow existing patterns from `process3`-`process6` implementations
- For return methods: create and populate a vector of pairs with test data, then
return it
- For input methods: iterate over the input vector, perform operations on
wrapped class elements (e.g., call `gett()` or modify values), return a
verifiable result
**Python Test Cases (`tests/test_code_generator_libcpp.py`):**
- Follow existing test patterns from `process3`-`process6` test cases
- Call the return methods and verify the returned list contains properly wrapped
objects
- Verify wrapped class methods can be called on elements (e.g.,
`result[0][0].gett()`)
- Call the input method with a Python list of `[WrappedClass, int]` pairs and
verify correct behavior
- Test both first and second positions of the pair containing wrapped classes
🚀 Next Steps
🤖 All AI agent prompts combined
Task: 1
Enable delegation support in StdPairConverter so that container converters will
properly delegate to it when processing nested pairs containing wrapped classes.
In `autowrap/ConversionProvider.py`, locate the `StdPairConverter` class and add
a `supports_delegation()` method override:
- The method should return `True` to enable delegation
- Follow the same pattern as `StdStringUnicodeConverter.supports_delegation()`
(simple one-liner)
- Place the method alongside other overridden methods in the class (after
`matching_python_type_full()` or near other short methods)
This allows container converters like `StdVectorConverter` to check via
`_has_delegating_converter()` and properly delegate element conversion to the
pair converter instead of falling back to Cython's automatic conversion.
===============================================================================
Task: 2
Add comprehensive test coverage for `vector<pair<WrappedClass, T>>` scenarios to
verify both input and output conversions work correctly.
**C++ Test Declarations (`tests/test_files/libcpp_test.pxd`):**
- Add method declarations following the existing naming convention (e.g.,
`process215`, `process216`, etc.) and patterns from `process3`-`process6`
- Declare a method returning `libcpp_vector[libcpp_pair[LibCppTest, int]]`
(wrapped class as first element)
- Declare a method returning `libcpp_vector[libcpp_pair[int, LibCppTest]]`
(wrapped class as second element)
- Declare a method accepting `libcpp_vector[libcpp_pair[LibCppTest, int]]` as
input parameter
**C++ Test Implementations (`tests/test_files/libcpp_test.hpp`):**
- Follow existing patterns from `process3`-`process6` implementations
- For return methods: create and populate a vector of pairs with test data, then
return it
- For input methods: iterate over the input vector, perform operations on
wrapped class elements (e.g., call `gett()` or modify values), return a
verifiable result
**Python Test Cases (`tests/test_code_generator_libcpp.py`):**
- Follow existing test patterns from `process3`-`process6` test cases
- Call the return methods and verify the returned list contains properly wrapped
objects
- Verify wrapped class methods can be called on elements (e.g.,
`result[0][0].gett()`)
- Call the input method with a Python list of `[WrappedClass, int]` pairs and
verify correct behavior
- Test both first and second positions of the pair containing wrapped classes
💡 Iterate on the plan with:
@coderabbitai <feedback>
Example Feedback
- `@coderabbitai` You can skip phase 3. Add a simple unit test case for phase 2.
- `@coderabbitai` For assumption 1 go ahead with option 3 and replan.
💬 Have feedback or questions? Drop into our discord!
You have to be a bit careful that your delegation method does not clash with the nested special handling in the libcpp_vector converter (and maybe others?) which already handles some cases.
I guess ideally all conversion providers should support delegation and maybe accept a nestedness level during construction to potentially adapt the code that they emit.
But this can be a bit complex because we are currently building three parts of code blocks at the same time: setup, actual conversion and cleanup.
I believe it might be possible to get rid of special cases if you can do the recursion by calling nested conversion providers recursively.
Activity
coderabbitai commentedon Jan 25, 2026
📝 CodeRabbit Plan Mode
Generate an implementation plan and prompts that you can use with your favorite coding agent.
✅ Successfully generated implementation plan.
💡 Use
@coderabbitai <feedback>to replanExamples
🔗 Related PRs
#216 - Add lookup/membership tests for STL containers and fix variable name collision bug [merged]
#227 - Add support for std::hash in wrap-hash directive [merged]
#228 - Fix ArrayWrapper inlining to go to pyx only, not pxd [merged]
#237 - Fix cross-module enum class imports for type assertions [merged]
#243 - Design conversion provider delegation system [merged]
🧪 Issue enrichment is currently in open beta.
You can configure auto-planning by selecting labels in the issue_enrichment configuration.
To disable automatic issue enrichment, add the following to your
.coderabbit.yaml:💬 Have feedback or questions? Drop into our discord!
coderabbitai commentedon Jan 25, 2026
Coding Plan
Approach
Enable
StdPairConverterto support delegation when nested inside container types likestd::vector, allowingvector<pair<WrappedClass, T>>to compile correctly by having the vector converter delegate element conversion to the pair converter's existing wrapped-class handling logic.Observations
The autowrap codebase uses a converter architecture where
TypeConverterBaseprovides asupports_delegation()method (default returnsFalse) that container converters likeStdVectorConvertercheck via_has_delegating_converter()to determine whether to manually iterate and delegate element conversion or rely on Cython's automatic conversion.StdPairConverteralready has complete implementations forinput_conversion()andoutput_conversion()that properly handle wrapped classes, but it doesn't overridesupports_delegation(), causing container converters to skip delegation and fall back to Cython's automatic conversion which fails for wrapped classes.Implementation Steps
Phase 1: Enable Delegation in StdPairConverter
Add the delegation support method to StdPairConverter so that container converters like StdVectorConverter will properly delegate to it when processing nested pairs containing wrapped classes.
Add supports_delegation() Override to StdPairConverter
Add a method override that enables delegation for the pair converter, allowing it to be invoked by container converters when pairs are nested inside vectors, sets, maps, etc.
autowrap/ConversionProvider.pyin theStdPairConverterclass to add asupports_delegation()method that returnsTruematching_python_type_full()or near other short methods)StdStringUnicodeConverter.supports_delegation()(simple one-liner returningTrue)🤖 Prompt for AI agents
Phase 2: Add Test Coverage
Create tests to verify that
vector<pair<WrappedClass, T>>scenarios now work correctly for both input and output conversions.Task 1: Add C++ Test Declarations
Declare test methods in the Cython declaration file that exercise vector-of-pair with wrapped classes in various configurations.
tests/test_files/libcpp_test.pxdto add method declarations for:libcpp_vector[libcpp_pair[LibCppTest, int]](wrapped class as first element)libcpp_vector[libcpp_pair[int, LibCppTest]](wrapped class as second element)libcpp_vector[libcpp_pair[LibCppTest, int]]as input parameterprocess215,process216, etc.) and declaration patterns fromprocess3-process6Task 2: Add C++ Test Implementations
Implement the C++ methods that the test declarations reference.
tests/test_files/libcpp_test.hppto add implementations for the declared methodsgett()or modify values), return a result that can be verifiedprocess3-process6implementationsTask 3: Add Python Test Cases
Write Python tests that verify the end-to-end functionality of vector-of-pair with wrapped classes.
tests/test_code_generator_libcpp.pyto add test cases that:result[0][0].gett())[WrappedClass, int]pairs and verify correct behaviorprocess3-process6test cases🤖 Prompt for AI agents
🚀 Next Steps
🤖 All AI agent prompts combined
💡 Iterate on the plan with:
@coderabbitai <feedback>💬 Have feedback or questions? Drop into our discord!
jpfeuffer commentedon Jan 25, 2026
You have to be a bit careful that your delegation method does not clash with the nested special handling in the libcpp_vector converter (and maybe others?) which already handles some cases.
I guess ideally all conversion providers should support delegation and maybe accept a nestedness level during construction to potentially adapt the code that they emit.
But this can be a bit complex because we are currently building three parts of code blocks at the same time: setup, actual conversion and cleanup.
I believe it might be possible to get rid of special cases if you can do the recursion by calling nested conversion providers recursively.