Skip to content

Commit

Permalink
Adapt MutableList methods to to_thrift_list
Browse files Browse the repository at this point in the history
Summary: Adapt the `to_thrift_list()` changes for `MutableList` and add tests to `MutableList` to cover the element is a container.

Reviewed By: aristidisp

Differential Revision: D65029990

fbshipit-source-id: 8b9c8cafdc634dd201f4595be63879b2ac46600d
  • Loading branch information
yoney authored and facebook-github-bot committed Oct 30, 2024
1 parent 7a50223 commit e61db5b
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ from thrift.python.types cimport TypeInfoBase
cdef class MutableList:
cdef TypeInfoBase _val_typeinfo
cdef list _list_data
cdef bint _value_type_is_container
cdef _value_to_internal_data(self, object)

cdef class MutableSet:
cdef TypeInfoBase _val_typeinfo
Expand Down
34 changes: 28 additions & 6 deletions third-party/thrift/src/thrift/lib/python/mutable_containers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ cdef class MutableList:
def __cinit__(self, TypeInfoBase value_typeinfo, list list_data):
self._val_typeinfo = value_typeinfo
self._list_data = list_data
self._value_type_is_container = value_typeinfo.is_container()

def __getitem__(self, object index_obj):
if isinstance(index_obj, slice):
Expand All @@ -73,7 +74,7 @@ cdef class MutableList:

def append(self, value):
internal_value = self._val_typeinfo.to_internal_data(value)
self._list_data.append(value)
self._list_data.append(internal_value)

def extend(self, values):
for value in values:
Expand Down Expand Up @@ -123,7 +124,7 @@ cdef class MutableList:
return False

try:
internal_value = self._val_typeinfo.to_internal_data(value)
internal_value = self._value_to_internal_data(value)
except (TypeError, OverflowError):
return False

Expand All @@ -142,22 +143,22 @@ cdef class MutableList:

def count(self, value):
try:
internal_value = self._val_typeinfo.to_internal_data(value)
internal_value = self._value_to_internal_data(value)
except (TypeError, OverflowError):
return 0

return self._list_data.count(internal_value)

def index(self, value, start=0, stop=None):
try:
internal_value = self._val_typeinfo.to_internal_data(value)
internal_value = self._value_to_internal_data(value)
except (TypeError, OverflowError):
raise ValueError

if stop is None:
return self._list_data.index(value, start)
return self._list_data.index(internal_value, start)
else:
return self._list_data.index(value, start, stop)
return self._list_data.index(internal_value, start, stop)

@classmethod
def __class_getitem__(cls, _):
Expand All @@ -167,6 +168,27 @@ cdef class MutableList:
"""
return cls

cdef _value_to_internal_data(self, value):
"""
The `_value_to_internal_data()` method is internal and used to wrap the
value when it is a container. This should be done implicitly in some cases.
For example, for a given list field (list<list<int>>), the user must use
`to_thrift_list()` for assignment:
s.list_of_list_field = to_thrift_map([[1], [2]])
However, for `in` check, it is implicit:
[1] in s.list_of_list_field
This method is called when an implicit wrapper is needed.
"""
value = (_ThriftContainerWrapper(value)
if self._value_type_is_container
else value)
return self._val_typeinfo.to_internal_data(value)


MutableSequence.register(MutableList)


Expand Down
Loading

0 comments on commit e61db5b

Please sign in to comment.