-
Notifications
You must be signed in to change notification settings - Fork 748
Unimplemented or broken methods in Python wrappers to standard C# containers #2531
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
Comments
Thank you for the systematic write-up! It will take me some time to go through all of these, but it would be great if you could create a draft PR with the tests that you ran for this analysis. That will make it easier to iterate on the fixes. |
We can not really implement negative indexing in general case because neither Python requires it nor .NET guarantees that negative index values can not be used for other purposes (e.g. it would be a breaking change). |
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.
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.
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.
Sorry for the delay, been busy.
Pity, well we can still fix Another thing, i found out that I'm going on holiday for a couple of weeks, so I won't be available for a while. |
Environment
Details
After #2530, I took the time to test from Python some of the most common C# containers, testing in particular the methods from the standard Python abc's (
Sequence
,MutableSequence
,Mapping
,MutableMapping
, ...).Here are my results, along with a few proposed fixes. Please let me know if there are any mistakes.
Shared setup
Array-like containers
What worked everywhere
__iter__
__reversed__
__contains__
__len__
reverse()
__getitem__
when the index is within boundsindex()
when the element is in the container__setitem__
when the index is within boundsWhat didn't work
__getitem__
when the index is negative or out of boundsPython expects a negative index to be interpreted as the position from the end of the container. Otherwise this breaks the implementation of
pop()
from abc (on top of a common assumption). This translation is only done for Array (here) for some reason.Also, the exception when the index is out of bounds should be
IndexError
, otherwise this breaks the implementation ofSequence.index()
among other things.Possible fix
Add the following method to class
SequenceMixin
:... and make sure
List
doesn't override it?index()
when the element is NOT in the containerThis is caused by the previous point, the exceptions are thrown in
Sequence.index()
.__getitem__
,__setitem__
and__delitem__
with slicesThis is ok, clearly not supported which is fine. Exceptions self-explanatory.
__setitem__
when the index is negative or out of boundsOK for
Array
; forList
, same as__getitem__
: a negative index is not translated, and an out of bounds index raises the wrong exception.Possible fix
Like for
__getitem__
, add the following method to classMutableSequenceMixin
:Array
lengthThis could be fine, to disallow changing an array's length, even though
Array.Resize
andArray.Copy
could in theory allow us to have all of these working and be fairly efficient.But the exceptions have me think that this is another kind of problem:
SystemError
comes from the bowels of CPython, precisely here, so I assume this is unintended behavior;IndexError
is caused byMutableSequenceMixin
not implementinginsert()
; if not supporting this is intended, it could be made more understandable by implementing it (inArray
, notMutableSequenceMixin
) and throwingTypeError
,System.NotSupportedException
or something more explanatory.List
elementsCrash output
Like for
Dictionary
,__delitem__
on aList
causes a hard crash; possibly related to #2530.List
lengthThese should definitely work; it mostly boils down to the previous point, the missing support for negative indexes and
insert()
not being implemented inMutableMappingMixin
.Possible fix
Add the following method to class
MutableSequenceMixin
:And also apply some of the previous proposed fixes, on top of fixing
del
.Dictionary-like containers
What worked everywhere
__contains__
__len__
__getitem__
when the key is in the dictionary__setitem__
keys
values
items
, although maybe it could be achieved without copyingget
clear
update
What didn't work
This crashes with the following output; already tracked in #2530.
Crash output
__iter__
returnsKeyValuePair
s instead of tuplesThis contradicts the Mapping protocol which mandates that the mapping is an iterable of its keys, and breaks the
popitem()
implementation fromcollections.abc
. At least the latter should be fixed.popitem
This is caused by the previous point, the exceptions are thrown in
MutableMapping.popitem()
which expects the dictionary to be an iterable of its keys.Possible fix
Add the following to
MutableMappingMixin
:... on top of fixing
del
.__getitem__
when the key is not in the dictionaryThe exception is not translated to a
ValueError
, which might theoretically break some functions trying to catch it, but it doesn't seem as important here.pop
andsetdefault
for some typesI believe this breaks only with non-nullable value types: the exception is called from the
MutableMappingMixin
implementation ofpop()
, which callsself.TryGetValue(key, None)
here.Possible fix: call
self.TryGetValue(key)
instead? I believe this is supported since commit f69753c.The text was updated successfully, but these errors were encountered: