Add tests for collection mixins (see issue #2531) by DiegoBaldassarMilleuno · Pull Request #2535 · pythonnet/pythonnet · GitHub
Skip to content

Add tests for collection mixins (see issue #2531)#2535

Draft
DiegoBaldassarMilleuno wants to merge 6 commits intopythonnet:masterfrom
DiegoBaldassarMilleuno:fix-collection-mixins
Draft

Add tests for collection mixins (see issue #2531)#2535
DiegoBaldassarMilleuno wants to merge 6 commits intopythonnet:masterfrom
DiegoBaldassarMilleuno:fix-collection-mixins

Conversation

@DiegoBaldassarMilleuno
Copy link
Copy Markdown

As requested in #2531 (comment), submitting a draft pull request testing for compliance to collections.abc protocols by a few standard containers.
Currently many tests fail.

A few notes:

  1. the tests that hard crash are temporarily disabled via pytest.mark.xfail, for convenience;
  2. many requirements I added in these tests are rather pedantic (like supporting values of the wrong type in __contains__ or requiring specific exceptions to be thrown); maintainers feel free to remove any that you believe are outside of contract.
  3. I haven't checked that this works with older versions of Python, it's a quick draft

Adding testing for adherence to collections.abc protocols for the
following classes: Array, List, ImmutableArray, ImmutableList,
Dictionary, ImmutableDictionary and ReadOnlyDictionary
Tests for Python list and dict are also present as a
reference but commented out.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is very hard to read

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert len(self.dct) == 4 if not self.nullable else 5
expected_length = 4 if not self.nullable else 5
assert len(self.dct) == expected_length

Something like this?

Comment on lines +95 to +98
def test_contains(self):
assert self.ktype(10) in self.dct
assert self.ktype(50) not in self.dct
assert 12.34 not in self.dct
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to pay attention here because .NET semantics might be different from Python semantics. In .NET Dictionary is a ICollection of KeyValuePair, so it has different Contains based on that interface.

Basically need a 2x2 explicit asserts: 2 for key vs KVP and 2 for in vs not in

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lostmsu standard Python semantics for mappings want key in mapping to check for keys (equivalent of ContainsKey) and kvp in mapping.items() to check for key-value pairs (as tuples, otherwise analogous to Contains); they also have a mapping be an iterable of its keys, so key in mapping is equivalent to key in list(mapping).
Like you said, this is different from .NET where an IDictionary<TKey,TValue> is an ICollection<KeyValuePair<TKey,TValue>>.
I'm for using Python semantics when using Python operators and .NET semantics when using .NET methods, both for interoperability with existing libraries and to avoid confusion.
That said I could add tests checking that .items() returns an object implementing ItemsView (also see here), including what you said.
Would that be ok?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Address that?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it doesn't crash anymore, will reenable this and test_delitem_raise next commit

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants