Skip to content

Commit 543caf6

Browse files
Filip Franceticfacebook-github-bot
Filip Francetic
authored andcommitted
Add a capi conversion no-op method to thrift python structs/unions
Summary: There's a generated python capi method on thrift-py3 structs to initialize from thrift python types. It's a very simple fix to add a no-op method to the base thrift python StructOrUnion class to get these unit tests to start passing now. Since the method is meant to initialize a thrift python struct with a thrift python struct, all that we need to do is return the passed in object (after a basic instance check to make sure no python shenanigans are going on). Reviewed By: ahilger Differential Revision: D68867840 fbshipit-source-id: 817381af3586cf7586532c6f94af676cfc16b241
1 parent 411280d commit 543caf6

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

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

-6
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ class Evil(Enum):
309309
evil_cls.__module__, "thrift.lib.py3.test.auto_migrate.converter"
310310
)
311311

312-
@brokenInAutoMigrate()
313312
def test_simple_capi(self) -> None:
314313
self.assert_simple(py3_types.Simple.from_python(self.make_simple_python()))
315314

@@ -358,7 +357,6 @@ def test_nested(self) -> None:
358357
nested.colorToSimpleMap[py3_types.Color.BLUE].color, py3_types.Color.BLUE
359358
)
360359

361-
@brokenInAutoMigrate()
362360
def test_nested_capi(self) -> None:
363361
self.assertEqual(
364362
self.make_nested_python()._to_py3(),
@@ -370,7 +368,6 @@ def test_simple_union(self) -> None:
370368
self.assertEqual(simple_union.type, py3_types.Union.Type.intField)
371369
self.assertEqual(simple_union.value, 42)
372370

373-
@brokenInAutoMigrate()
374371
def test_simple_union_capi(self) -> None:
375372
simple_union = py3_types.Union.from_python(python_types.Union(intField=42))
376373
self.assertEqual(simple_union.type, py3_types.Union.Type.intField)
@@ -381,7 +378,6 @@ def test_union_with_py3_name_annotation(self) -> None:
381378
self.assertEqual(simple_union.type, py3_types.Union.Type.name_)
382379
self.assertEqual(simple_union.value, "myname")
383380

384-
@brokenInAutoMigrate()
385381
def test_union_with_py3_name_annotation_capi(self) -> None:
386382
simple_union = py3_types.Union.from_python(python_types.Union(name_="myname"))
387383
self.assertEqual(simple_union.type, py3_types.Union.Type.name_)
@@ -392,7 +388,6 @@ def test_union_with_containers(self) -> None:
392388
self.assertEqual(union_with_list.type, py3_types.Union.Type.intList)
393389
self.assertEqual(union_with_list.value, [1, 2, 3])
394390

395-
@brokenInAutoMigrate()
396391
def test_union_with_containers_capi(self) -> None:
397392
union_with_list = py3_types.Union.from_python(
398393
python_types.Union(intList=[1, 2, 3])
@@ -407,7 +402,6 @@ def test_complex_union(self) -> None:
407402
self.assertEqual(complex_union.type, py3_types.Union.Type.simple_)
408403
self.assertEqual(complex_union.simple_.intField, 42)
409404

410-
@brokenInAutoMigrate()
411405
def test_complex_union_capi(self) -> None:
412406
complex_union = py3_types.Union.from_python(
413407
python_types.Union(

third-party/thrift/src/thrift/lib/python/exceptions.pyx

+7
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,13 @@ cdef class GeneratedError(Error):
310310
)
311311
self._fbthrift_populate_field_values()
312312

313+
@staticmethod
314+
def from_python(obj: GeneratedError) -> GeneratedError:
315+
if not isinstance(obj, GeneratedError):
316+
raise TypeError(f'value {obj} expected to be a thrift-python Exception, was actually of type ' f'{type(obj)}')
317+
return obj
318+
319+
313320
@classmethod
314321
def _fbthrift_auto_migrate_enabled(cls):
315322
return False

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

+6
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,12 @@ cdef class StructOrUnion:
10111011
cdef _fbthrift_get_field_value(self, int16_t index):
10121012
raise NotImplementedError("Not implemented on base StructOrUnion class")
10131013

1014+
@staticmethod
1015+
def from_python(obj: StructOrUnion) -> StructOrUnion:
1016+
if not isinstance(obj, StructOrUnion):
1017+
raise TypeError(f'value {obj} expected to be a thrift-python Struct or Union, was actually of type ' f'{type(obj)}')
1018+
return obj
1019+
10141020
@classmethod
10151021
def _fbthrift_auto_migrate_enabled(cls):
10161022
return False

0 commit comments

Comments
 (0)