Skip to content

Commit

Permalink
Introduce is_container() to the TypeInfo classes
Browse files Browse the repository at this point in the history
Summary: Introduce the `is_container()` to utilise it for mutable container type checking (for nested containers)

Reviewed By: ahilger

Differential Revision: D64568358

fbshipit-source-id: 75e543b6482fefa0dc1814d2c89c232094bff796
  • Loading branch information
yoney authored and facebook-github-bot committed Oct 18, 2024
1 parent 1f5f248 commit d43da36
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
9 changes: 9 additions & 0 deletions thrift/lib/python/mutable_typeinfos.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ cdef class MutableListTypeInfo(TypeInfoBase):

return self.val_info.same_as((<MutableListTypeInfo>other).val_info)

def is_container(self):
return True

def __reduce__(self):
return (MutableListTypeInfo, (self.val_info,))

Expand Down Expand Up @@ -257,6 +260,9 @@ cdef class MutableSetTypeInfo(TypeInfoBase):

return self.val_info.same_as((<MutableSetTypeInfo>other).val_info)

def is_container(self):
return True

def __reduce__(self):
return (MutableSetTypeInfo, (self.val_info,))

Expand Down Expand Up @@ -334,5 +340,8 @@ cdef class MutableMapTypeInfo(TypeInfoBase):
return (self.key_info.same_as((<MutableMapTypeInfo>other).key_info) and
self.val_info.same_as((<MutableMapTypeInfo>other).val_info))

def is_container(self):
return True

def __reduce__(self):
return (MutableMapTypeInfo, (self.key_info, self.val_info))
16 changes: 16 additions & 0 deletions thrift/lib/python/test/typeinfo_test.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ cdef class TypeInfoTests():
"Use the 'same_as' method for comparing TypeInfoBase instances."):
list_type_info == list_type_info

self.ut.assertTrue(list_type_info.is_container())

def test_ListTypeInfo_nested(self) -> None:
element_type_info = ListTypeInfo(typeinfo_i64)
list_type_info = ListTypeInfo(element_type_info)
Expand Down Expand Up @@ -125,6 +127,7 @@ cdef class TypeInfoTests():
self.ut.assertTrue(set_type_info.same_as(set_type_info))
self.ut.assertTrue(SetTypeInfo(typeinfo_i64).same_as(set_type_info))
self.ut.assertFalse(SetTypeInfo(typeinfo_i32).same_as(set_type_info))
self.ut.assertTrue(set_type_info.is_container())

def test_SetTypeInfo_nested(self) -> None:
element_type_info = SetTypeInfo(typeinfo_i64)
Expand Down Expand Up @@ -156,6 +159,7 @@ cdef class TypeInfoTests():

self.ut.assertTrue(typeinfo_i64.same_as(typeinfo_i64))
self.ut.assertFalse(typeinfo_i32.same_as(typeinfo_i64))
self.ut.assertFalse(typeinfo_i32.is_container())

def test_StringTypeInfo(self) -> None:
self.ut.assertIsInstance(typeinfo_string, TypeInfoBase)
Expand All @@ -175,6 +179,7 @@ cdef class TypeInfoTests():

self.ut.assertTrue(typeinfo_string.same_as(typeinfo_string))
self.ut.assertFalse(typeinfo_i32.same_as(typeinfo_string))
self.ut.assertFalse(typeinfo_string.is_container())

def test_TypeInfo(self) -> None:
# `typeinfo_{bool,float,double}` are instances of `TypeInfo`
Expand All @@ -197,6 +202,7 @@ cdef class TypeInfoTests():
self.ut.assertFalse(typeinfo_float.same_as(typeinfo_double))
self.ut.assertFalse(typeinfo_double.same_as(typeinfo_float))
self.ut.assertFalse(typeinfo_bool.same_as(typeinfo_float))
self.ut.assertFalse(typeinfo_bool.is_container())

def test_StructTypeInfo(self) -> None:
struct_type_info = StructTypeInfo(Foo)
Expand All @@ -218,6 +224,7 @@ cdef class TypeInfoTests():
self.ut.assertTrue(struct_type_info.same_as(struct_type_info))
self.ut.assertTrue(StructTypeInfo(Foo).same_as(struct_type_info))
self.ut.assertFalse(StructTypeInfo(OtherFoo).same_as(struct_type_info))
self.ut.assertFalse(struct_type_info.is_container())

def test_EnumTypeInfo(self) -> None:
enum_type_info = EnumTypeInfo(Bar)
Expand All @@ -240,6 +247,7 @@ cdef class TypeInfoTests():
self.ut.assertTrue(enum_type_info.same_as(enum_type_info))
self.ut.assertTrue(EnumTypeInfo(Bar).same_as(enum_type_info))
self.ut.assertFalse(EnumTypeInfo(OtherBar).same_as(enum_type_info))
self.ut.assertFalse(enum_type_info.is_container())

def test_AdaptedTypeInfo(self) -> None:
adapted_type_info = AdaptedTypeInfo(typeinfo_string, AtoiAdapter, lambda: None)
Expand All @@ -265,6 +273,7 @@ cdef class TypeInfoTests():
self.ut.assertTrue(adapted_type_info.same_as(adapted_type_info))
self.ut.assertTrue(AdaptedTypeInfo(typeinfo_string, AtoiAdapter, lambda: None).same_as(adapted_type_info))
self.ut.assertFalse(AdaptedTypeInfo(typeinfo_string, DatetimeAdapter, lambda: None).same_as(adapted_type_info))
self.ut.assertFalse(adapted_type_info.is_container())

def test_IOBufTypeInfo(self) -> None:
self.ut.assertIsInstance(typeinfo_iobuf, TypeInfoBase)
Expand All @@ -284,6 +293,7 @@ cdef class TypeInfoTests():

self.ut.assertTrue(typeinfo_iobuf.same_as(typeinfo_iobuf))
self.ut.assertFalse(typeinfo_string.same_as(typeinfo_iobuf))
self.ut.assertFalse(typeinfo_iobuf.is_container())

def test_MapTypeInfo(self) -> None:
map_type_info = MapTypeInfo(typeinfo_string, typeinfo_i64)
Expand All @@ -306,6 +316,7 @@ cdef class TypeInfoTests():
self.ut.assertTrue(MapTypeInfo(typeinfo_string, typeinfo_i64).same_as(map_type_info))
self.ut.assertFalse(MapTypeInfo(typeinfo_string, typeinfo_i32).same_as(map_type_info))
self.ut.assertFalse(MapTypeInfo(typeinfo_i32, typeinfo_i64).same_as(map_type_info))
self.ut.assertTrue(map_type_info.is_container())

def test_MapTypeInfo_nested(self) -> None:
# Map[str, Map[int, List[int]]]
Expand Down Expand Up @@ -346,6 +357,8 @@ cdef class TypeInfoTests():
"Use the 'same_as' method for comparing TypeInfoBase instances."):
list_type_info == list_type_info

self.ut.assertTrue(list_type_info.is_container())

def test_MutableListTypeInfo_nested(self) -> None:
element_type_info = MutableListTypeInfo(typeinfo_i64)
list_type_info = MutableListTypeInfo(element_type_info)
Expand Down Expand Up @@ -377,6 +390,7 @@ cdef class TypeInfoTests():
self.ut.assertTrue(set_type_info.same_as(set_type_info))
self.ut.assertTrue(MutableSetTypeInfo(typeinfo_i64).same_as(set_type_info))
self.ut.assertFalse(MutableSetTypeInfo(typeinfo_i32).same_as(set_type_info))
self.ut.assertTrue(set_type_info.is_container())

def test_MutableMapTypeInfo(self) -> None:
map_type_info = MutableMapTypeInfo(typeinfo_string, typeinfo_i64)
Expand All @@ -399,6 +413,7 @@ cdef class TypeInfoTests():
self.ut.assertTrue(MutableMapTypeInfo(typeinfo_string, typeinfo_i64).same_as(map_type_info))
self.ut.assertFalse(MutableMapTypeInfo(typeinfo_string, typeinfo_i32).same_as(map_type_info))
self.ut.assertFalse(MutableMapTypeInfo(typeinfo_i32, typeinfo_i64).same_as(map_type_info))
self.ut.assertTrue(map_type_info.is_container())

def test_MutableStructTypeInfo(self) -> None:
struct_type_info = MutableStructTypeInfo(FooMutable)
Expand All @@ -420,3 +435,4 @@ cdef class TypeInfoTests():
self.ut.assertTrue(struct_type_info.same_as(struct_type_info))
self.ut.assertTrue(MutableStructTypeInfo(FooMutable).same_as(struct_type_info))
self.ut.assertFalse(MutableStructTypeInfo(OtherFooMutable).same_as(struct_type_info))
self.ut.assertFalse(struct_type_info.is_container())
16 changes: 16 additions & 0 deletions thrift/lib/python/types.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ cdef class TypeInfoBase:
"""
raise NotImplementedError("Not implemented on base TypeInfoBase class")

def is_container(self):
"""
Return `True` if it is one of the immutable or mutable `TypeInfo` classes
for list, map or set.
"""
return False

@cython.final
cdef class TypeInfo(TypeInfoBase):
@staticmethod
Expand Down Expand Up @@ -622,6 +629,9 @@ cdef class ListTypeInfo(TypeInfoBase):

return self.val_info.same_as((<ListTypeInfo>other).val_info)

def is_container(self):
return True

def __reduce__(self):
return (ListTypeInfo, (self.val_info,))

Expand Down Expand Up @@ -673,6 +683,9 @@ cdef class SetTypeInfo(TypeInfoBase):

return self.val_info.same_as((<SetTypeInfo>other).val_info)

def is_container(self):
return True

def __reduce__(self):
return (SetTypeInfo, (self.val_info,))

Expand Down Expand Up @@ -740,6 +753,9 @@ cdef class MapTypeInfo(TypeInfoBase):
return (self.key_info.same_as((<MapTypeInfo>other).key_info) and
self.val_info.same_as((<MapTypeInfo>other).val_info))

def is_container(self):
return True

def __reduce__(self):
return (MapTypeInfo, (self.key_info, self.val_info))

Expand Down

0 comments on commit d43da36

Please sign in to comment.