Skip to content

Commit 691b63f

Browse files
ahilgerfacebook-github-bot
authored andcommitted
make isinstance Container work
Summary: A follow-up to previous diff fixing `isinstance` `List`, `Map`, `Set`, for their parent class `Container`. In auto-migrat,e the control flow of `isinstance(obj, py3.types.Container)` should not change. Reviewed By: fried Differential Revision: D76142665 fbshipit-source-id: a7e834f63cb55421e98d380551c41bb38dede986
1 parent ee1fbbd commit 691b63f

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

third-party/thrift/src/thrift/lib/py3/test/auto_migrate/lists.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ def test_list_from_iterable(self) -> None:
224224
[Color.red, Color.blue, Color.green],
225225
)
226226

227-
@brokenInAutoMigrate()
228227
def test_is_container(self) -> None:
229228
self.assertIsInstance(int_list, Container)
230229
self.assertIsInstance(I32List([1, 2, 3]), Container)

third-party/thrift/src/thrift/lib/py3/test/auto_migrate/maps.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ def test_equality(self) -> None:
182182
self.assertEqual(x, x)
183183
self.assertEqual(y, y)
184184

185-
@brokenInAutoMigrate()
186185
def test_is_container(self) -> None:
187186
self.assertIsInstance(LocationMap, Container)
188187
self.assertIsInstance(StrI32ListMap(), Container)

third-party/thrift/src/thrift/lib/py3/test/auto_migrate/sets.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ def test_set_op_return_type(self) -> None:
251251
self.assertIsInstance(x.__rxor__(y), expected_type)
252252
self.assertIsInstance(x.__rsub__(y), expected_type)
253253

254-
@brokenInAutoMigrate()
255254
def test_is_container(self) -> None:
256255
self.assertIsInstance(SetI32Lists(), Container)
257256
self.assertIsInstance(SetSetI32Lists(), Container)

third-party/thrift/src/thrift/lib/py3/types.pyx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ from thrift.python.types cimport (
3434
)
3535
from apache.thrift.metadata.types_auto_migrated import _fbthrift__is_py3_auto_migrated
3636
from thrift.python.types import (
37+
Container as _fbthrift_python_Container,
3738
Enum as _fbthrift_python_Enum,
3839
EnumMeta as _fbthrift_python_EnumMeta,
3940
Flag as _fbthrift_python_Flag,
@@ -392,6 +393,19 @@ cdef class Container:
392393
def __len__(self):
393394
raise NotImplementedError()
394395

396+
class ContainerMeta(type):
397+
def __instancecheck__(cls, inst):
398+
if isinstance(inst, _fbthrift_python_Container):
399+
return _fbthrift__is_py3_auto_migrated
400+
return super().__instancecheck__(inst)
401+
402+
def __subclasscheck__(cls, sub):
403+
if issubclass(sub, _fbthrift_python_Container):
404+
return _fbthrift__is_py3_auto_migrated
405+
return super().__subclasscheck__(sub)
406+
407+
SetMetaClass(<PyTypeObject*> Container, <PyTypeObject*> ContainerMeta)
408+
395409
cdef class _ListPrivateCtorToken:
396410
pass
397411

0 commit comments

Comments
 (0)