Skip to content
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

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

Open
3 tasks done
meghprkh opened this issue Jan 22, 2025 · 0 comments
Open
3 tasks done

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

meghprkh opened this issue Jan 22, 2025 · 0 comments
Labels
triage New bug, unverified

Comments

@meghprkh
Copy link

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

@meghprkh meghprkh added the triage New bug, unverified label Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage New bug, unverified
Projects
None yet
Development

No branches or pull requests

1 participant