Skip to content

[BUG]: STL casters should probably type cast from collections.abc #5498

@meghprkh

Description

@meghprkh

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

2.12.0

Problem description

If I have some class inheriting from collections.abc, it would not implicitly cast to corresponding C++ types. Example code shows a reproduction.

This probably makes most sense as nowadays a lot of Python libraries use Mapping/Set as an abstract type instead of dictionary.

Example code with set follows

Reproducible example code

Python class


from collections.abc import MutableSet

class CaselessSet(MutableSet[str]):
    # Using https://stackoverflow.com/a/27531275 as the implementation
    # ... implementation not written for brevity


C++ `mymod`


m.def("set_size", [](set<string>& s) {
  return s.size();
});


`mymod.pyi`:


from collections.abc import Set as AbstractSet
def set_size(set: AbstractSet[str])


usage:


s = CaselessSet('a', 'B')
mymod.set_size(s)

Is this a regression? Put the last known working version here if it is.

Not a regression

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageNew bug, unverified

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions