-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix(types): updated STL casters and py::buffer to use collections.abc #5566
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
872f9ff
Updated STL type hints use support collections.abc
timohl 80a9086
Updated array_caster to match numpy/eigen typing.Annotated stlye
timohl caa2277
Added support for Mapping, Set and Sequence derived from collections.…
timohl 71c7846
Fixed merge of typing.SupportsInt in new tests
timohl f5e48f1
Integrated collections.abc checks into convertible check functions.
timohl 776cd07
Merge branch 'master' into stl-typing
timohl 3d1a335
Changed type hint of py::buffer to collections.abc.Buffer
timohl d6e1fb0
Changed convertible check function names
timohl 68768f6
Added comments to convertible check functions
timohl 5f771da
Removed checks for methods that are already required by the abstract …
timohl e21b66b
Merge branch 'master' into stl-typing
timohl 6fc2ab3
Improved mapping caster test using more compact a1b2c3 variable
timohl e06dc33
Renamed and refactored sequence, mapping and set test classes to reus…
timohl ee270ab
Added tests for mapping and set casters for noconvert mode
timohl db17f2d
Added tests for sequence caster for noconvert mode
timohl 383dcb5
Merge branch 'master' into stl-typing
timohl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,10 @@ def test_vector(doc): | |
| assert m.load_bool_vector((True, False)) | ||
|
|
||
| assert doc(m.cast_vector) == "cast_vector() -> list[int]" | ||
| assert doc(m.load_vector) == "load_vector(arg0: list[typing.SupportsInt]) -> bool" | ||
| assert ( | ||
| doc(m.load_vector) | ||
| == "load_vector(arg0: collections.abc.Sequence[typing.SupportsInt]) -> bool" | ||
| ) | ||
|
|
||
| # Test regression caused by 936: pointers to stl containers weren't castable | ||
| assert m.cast_ptr_vector() == ["lvalue", "lvalue"] | ||
|
|
@@ -42,10 +45,13 @@ def test_array(doc): | |
| assert m.load_array(lst) | ||
| assert m.load_array(tuple(lst)) | ||
|
|
||
| assert doc(m.cast_array) == "cast_array() -> Annotated[list[int], FixedSize(2)]" | ||
| assert ( | ||
| doc(m.cast_array) | ||
| == 'cast_array() -> typing.Annotated[list[int], "FixedSize(2)"]' | ||
| ) | ||
| assert ( | ||
| doc(m.load_array) | ||
| == "load_array(arg0: Annotated[list[typing.SupportsInt], FixedSize(2)]) -> bool" | ||
| == 'load_array(arg0: typing.Annotated[collections.abc.Sequence[typing.SupportsInt], "FixedSize(2)"]) -> bool' | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -65,7 +71,8 @@ def test_valarray(doc): | |
|
|
||
| assert doc(m.cast_valarray) == "cast_valarray() -> list[int]" | ||
| assert ( | ||
| doc(m.load_valarray) == "load_valarray(arg0: list[typing.SupportsInt]) -> bool" | ||
| doc(m.load_valarray) | ||
| == "load_valarray(arg0: collections.abc.Sequence[typing.SupportsInt]) -> bool" | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -79,7 +86,9 @@ def test_map(doc): | |
| assert m.load_map(d) | ||
|
|
||
| assert doc(m.cast_map) == "cast_map() -> dict[str, str]" | ||
| assert doc(m.load_map) == "load_map(arg0: dict[str, str]) -> bool" | ||
| assert ( | ||
| doc(m.load_map) == "load_map(arg0: collections.abc.Mapping[str, str]) -> bool" | ||
| ) | ||
|
|
||
|
|
||
| def test_set(doc): | ||
|
|
@@ -91,7 +100,7 @@ def test_set(doc): | |
| assert m.load_set(frozenset(s)) | ||
|
|
||
| assert doc(m.cast_set) == "cast_set() -> set[str]" | ||
| assert doc(m.load_set) == "load_set(arg0: set[str]) -> bool" | ||
| assert doc(m.load_set) == "load_set(arg0: collections.abc.Set[str]) -> bool" | ||
|
|
||
|
|
||
| def test_recursive_casting(): | ||
|
|
@@ -273,7 +282,7 @@ def __fspath__(self): | |
| assert m.parent_paths(["foo/bar", "foo/baz"]) == [Path("foo"), Path("foo")] | ||
| assert ( | ||
| doc(m.parent_paths) | ||
| == "parent_paths(arg0: list[Union[os.PathLike, str, bytes]]) -> list[pathlib.Path]" | ||
| == "parent_paths(arg0: collections.abc.Sequence[Union[os.PathLike, str, bytes]]) -> list[pathlib.Path]" | ||
| ) | ||
| # py::typing::List | ||
| assert m.parent_paths_list(["foo/bar", "foo/baz"]) == [Path("foo"), Path("foo")] | ||
|
|
@@ -364,7 +373,7 @@ def test_stl_pass_by_pointer(msg): | |
| msg(excinfo.value) | ||
| == """ | ||
| stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported: | ||
| 1. (v: list[typing.SupportsInt] = None) -> list[int] | ||
| 1. (v: collections.abc.Sequence[typing.SupportsInt] = None) -> list[int] | ||
|
|
||
| Invoked with: | ||
| """ | ||
|
|
@@ -376,7 +385,7 @@ def test_stl_pass_by_pointer(msg): | |
| msg(excinfo.value) | ||
| == """ | ||
| stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported: | ||
| 1. (v: list[typing.SupportsInt] = None) -> list[int] | ||
| 1. (v: collections.abc.Sequence[typing.SupportsInt] = None) -> list[int] | ||
|
|
||
| Invoked with: None | ||
| """ | ||
|
|
@@ -567,3 +576,129 @@ def gen_invalid(): | |
| with pytest.raises(expected_exception): | ||
| m.pass_std_map_int(FakePyMappingGenObj(gen_obj)) | ||
| assert not tuple(gen_obj) | ||
|
|
||
|
|
||
| def test_sequence_caster_protocol(doc): | ||
| from collections.abc import Sequence | ||
|
|
||
| class SequenceLike(Sequence): | ||
| def __init__(self, *args): | ||
| self.data = tuple(args) | ||
|
|
||
| def __len__(self): | ||
| return len(self.data) | ||
|
|
||
| def __getitem__(self, index): | ||
| return self.data[index] | ||
|
|
||
| class FakeSequenceLike: | ||
| def __init__(self, *args): | ||
| self.data = tuple(args) | ||
|
|
||
| def __len__(self): | ||
| return len(self.data) | ||
|
|
||
| def __getitem__(self, index): | ||
| return self.data[index] | ||
timohl marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| assert ( | ||
| doc(m.roundtrip_std_vector_int) | ||
| == "roundtrip_std_vector_int(arg0: collections.abc.Sequence[typing.SupportsInt]) -> list[int]" | ||
| ) | ||
| assert m.roundtrip_std_vector_int([1, 2, 3]) == [1, 2, 3] | ||
| assert m.roundtrip_std_vector_int((1, 2, 3)) == [1, 2, 3] | ||
| assert m.roundtrip_std_vector_int(SequenceLike(1, 2, 3)) == [1, 2, 3] | ||
| assert m.roundtrip_std_vector_int(FakeSequenceLike(1, 2, 3)) == [1, 2, 3] | ||
| assert m.roundtrip_std_vector_int([]) == [] | ||
| assert m.roundtrip_std_vector_int(()) == [] | ||
| assert m.roundtrip_std_vector_int(FakeSequenceLike()) == [] | ||
|
|
||
|
|
||
| def test_mapping_caster_protocol(doc): | ||
| from collections.abc import Mapping | ||
|
|
||
| class MappingLike(Mapping): | ||
| def __init__(self, **kwargs): | ||
| self.data = dict(kwargs) | ||
|
|
||
| def __len__(self): | ||
| return len(self.data) | ||
|
|
||
| def __getitem__(self, key): | ||
| return self.data[key] | ||
|
|
||
| def __iter__(self): | ||
| yield from self.data | ||
|
|
||
| class FakeMappingLike: | ||
| def __init__(self, **kwargs): | ||
| self.data = dict(kwargs) | ||
|
|
||
| def __len__(self): | ||
| return len(self.data) | ||
|
|
||
| def __getitem__(self, key): | ||
| return self.data[key] | ||
|
|
||
| def __iter__(self): | ||
| yield from self.data | ||
|
|
||
| assert ( | ||
| doc(m.roundtrip_std_map_str_int) | ||
| == "roundtrip_std_map_str_int(arg0: collections.abc.Mapping[str, typing.SupportsInt]) -> dict[str, int]" | ||
| ) | ||
| assert m.roundtrip_std_map_str_int({"a": 1, "b": 2, "c": 3}) == { | ||
| "a": 1, | ||
| "b": 2, | ||
| "c": 3, | ||
| } | ||
|
||
| assert m.roundtrip_std_map_str_int(MappingLike(a=1, b=2, c=3)) == { | ||
| "a": 1, | ||
| "b": 2, | ||
| "c": 3, | ||
| } | ||
| assert m.roundtrip_std_map_str_int({}) == {} | ||
| assert m.roundtrip_std_map_str_int(MappingLike()) == {} | ||
| with pytest.raises(TypeError): | ||
| m.roundtrip_std_map_str_int(FakeMappingLike(a=1, b=2, c=3)) | ||
|
|
||
|
|
||
| def test_set_caster_protocol(doc): | ||
| from collections.abc import Set | ||
|
|
||
| class SetLike(Set): | ||
| def __init__(self, *args): | ||
| self.data = set(args) | ||
|
|
||
| def __len__(self): | ||
| return len(self.data) | ||
|
|
||
| def __contains__(self, item): | ||
| return item in self.data | ||
|
|
||
| def __iter__(self): | ||
| yield from self.data | ||
|
|
||
| class FakeSetLike: | ||
| def __init__(self, *args): | ||
| self.data = set(args) | ||
|
|
||
| def __len__(self): | ||
| return len(self.data) | ||
|
|
||
| def __contains__(self, item): | ||
| return item in self.data | ||
|
|
||
| def __iter__(self): | ||
| yield from self.data | ||
|
|
||
| assert ( | ||
| doc(m.roundtrip_std_set_int) | ||
| == "roundtrip_std_set_int(arg0: collections.abc.Set[typing.SupportsInt]) -> set[int]" | ||
| ) | ||
| assert m.roundtrip_std_set_int({1, 2, 3}) == {1, 2, 3} | ||
| assert m.roundtrip_std_set_int(SetLike(1, 2, 3)) == {1, 2, 3} | ||
| assert m.roundtrip_std_set_int(set()) == set() | ||
| assert m.roundtrip_std_set_int(SetLike()) == set() | ||
| with pytest.raises(TypeError): | ||
| m.roundtrip_std_set_int(FakeSetLike(1, 2, 3)) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.