Skip to content

Commit 21f11db

Browse files
danabrlostmsu
authored andcommitted
Make len work for ICollection<> interface objects
1 parent 6d59aea commit 21f11db

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ details about the cause of the failure
3131
- Fix non-delegate types incorrectly appearing as callable.
3232
- Indexers can now be used with interface objects
3333
- Fixed a bug where indexers could not be used if they were inherited
34+
- Made it possible to use `__len__` also on `ICollection<>` interface objects
3435

3536
## [2.5.0][] - 2020-06-14
3637

src/runtime/slots/mp_length.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public static bool CanAssgin(Type clrType)
3636
{
3737
return true;
3838
}
39+
if (clrType.IsInterface && clrType.IsGenericType && clrType.GetGenericTypeDefinition() == typeof(ICollection<>))
40+
{
41+
return true;
42+
}
3943
return false;
4044
}
4145

src/tests/test_mp_length.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,17 @@ def test_custom_generic_collection_explicit___len__():
4747
s.Add(1)
4848
s.Add(10)
4949
assert len(s) == 2
50+
51+
def test_len_through_interface_generic():
52+
"""Test __len__ for ICollection<T>"""
53+
import System.Collections.Generic
54+
l = System.Collections.Generic.List[int]()
55+
coll = System.Collections.Generic.ICollection[int](l)
56+
assert len(coll) == 0
57+
58+
def test_len_through_interface():
59+
"""Test __len__ for ICollection"""
60+
import System.Collections
61+
l = System.Collections.ArrayList()
62+
coll = System.Collections.ICollection(l)
63+
assert len(coll) == 0

0 commit comments

Comments
 (0)