Skip to content

StdPairConverter does not support delegation for nested containers #250

Open

Description

@timosachsenberg

Problem

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<...>>:

  1. pair is not in names_of_wrapper_classes (it's an STL type)
  2. _has_delegating_converter(tt) returns False because StdPairConverter.supports_delegation() is False
  3. Code falls through to the else branch that relies on Cython's automatic conversion
  4. 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:

class StdPairConverter(TypeConverterBase):
    # ...
    
    def supports_delegation(self) -> bool:
        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.

Environment

  • autowrap version: 0.26.0 (from pyOpenMS)
  • Cython version: 3.x
  • Python: 3.10+

Related Code

  • StdPairConverter (line 661)
  • StdPairConverter.output_conversion() (line 781) - already handles wrapped classes
  • StdVectorConverter._has_delegating_converter() check (line 2089)
  • TypeConverterBase.supports_delegation() default (line 137)

Activity

  1. coderabbitai commented on Jan 25, 2026

    @coderabbitai
    Contributor

    📝 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 replan

    Examples

    🔗 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:

    issue_enrichment:
      auto_enrich:
        enabled: false

    💬 Have feedback or questions? Drop into our discord!

  2. coderabbitai commented on Jan 25, 2026

    @coderabbitai
    Contributor

    Coding Plan

    Approach

    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!

  3. jpfeuffer commented on Jan 25, 2026

    @jpfeuffer
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions