From 0b1692802fd3a95daad427b3aced684837fdb3bd Mon Sep 17 00:00:00 2001 From: Filip Francetic Date: Thu, 30 Jan 2025 14:31:14 -0800 Subject: [PATCH] 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 --- thrift/lib/py3/test/auto_migrate/converter.py | 6 ------ thrift/lib/python/exceptions.pyx | 7 +++++++ thrift/lib/python/types.pyx | 6 ++++++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/thrift/lib/py3/test/auto_migrate/converter.py b/thrift/lib/py3/test/auto_migrate/converter.py index 6b72524d4bd..21c90e4ff35 100644 --- a/thrift/lib/py3/test/auto_migrate/converter.py +++ b/thrift/lib/py3/test/auto_migrate/converter.py @@ -309,7 +309,6 @@ class Evil(Enum): evil_cls.__module__, "thrift.lib.py3.test.auto_migrate.converter" ) - @brokenInAutoMigrate() def test_simple_capi(self) -> None: self.assert_simple(py3_types.Simple.from_python(self.make_simple_python())) @@ -358,7 +357,6 @@ def test_nested(self) -> None: nested.colorToSimpleMap[py3_types.Color.BLUE].color, py3_types.Color.BLUE ) - @brokenInAutoMigrate() def test_nested_capi(self) -> None: self.assertEqual( self.make_nested_python()._to_py3(), @@ -370,7 +368,6 @@ def test_simple_union(self) -> None: self.assertEqual(simple_union.type, py3_types.Union.Type.intField) self.assertEqual(simple_union.value, 42) - @brokenInAutoMigrate() def test_simple_union_capi(self) -> None: simple_union = py3_types.Union.from_python(python_types.Union(intField=42)) self.assertEqual(simple_union.type, py3_types.Union.Type.intField) @@ -381,7 +378,6 @@ def test_union_with_py3_name_annotation(self) -> None: self.assertEqual(simple_union.type, py3_types.Union.Type.name_) self.assertEqual(simple_union.value, "myname") - @brokenInAutoMigrate() def test_union_with_py3_name_annotation_capi(self) -> None: simple_union = py3_types.Union.from_python(python_types.Union(name_="myname")) self.assertEqual(simple_union.type, py3_types.Union.Type.name_) @@ -392,7 +388,6 @@ def test_union_with_containers(self) -> None: self.assertEqual(union_with_list.type, py3_types.Union.Type.intList) self.assertEqual(union_with_list.value, [1, 2, 3]) - @brokenInAutoMigrate() def test_union_with_containers_capi(self) -> None: union_with_list = py3_types.Union.from_python( python_types.Union(intList=[1, 2, 3]) @@ -407,7 +402,6 @@ def test_complex_union(self) -> None: self.assertEqual(complex_union.type, py3_types.Union.Type.simple_) self.assertEqual(complex_union.simple_.intField, 42) - @brokenInAutoMigrate() def test_complex_union_capi(self) -> None: complex_union = py3_types.Union.from_python( python_types.Union( diff --git a/thrift/lib/python/exceptions.pyx b/thrift/lib/python/exceptions.pyx index 82e6a030117..9821e4ab9aa 100644 --- a/thrift/lib/python/exceptions.pyx +++ b/thrift/lib/python/exceptions.pyx @@ -310,6 +310,13 @@ cdef class GeneratedError(Error): ) self._fbthrift_populate_field_values() + @staticmethod + def from_python(obj: GeneratedError) -> GeneratedError: + if not isinstance(obj, GeneratedError): + raise TypeError(f'value {obj} expected to be a thrift-python Exception, was actually of type ' f'{type(obj)}') + return obj + + @classmethod def _fbthrift_auto_migrate_enabled(cls): return False diff --git a/thrift/lib/python/types.pyx b/thrift/lib/python/types.pyx index 2a9b5d6726c..b6f75f7d661 100644 --- a/thrift/lib/python/types.pyx +++ b/thrift/lib/python/types.pyx @@ -1011,6 +1011,12 @@ cdef class StructOrUnion: cdef _fbthrift_get_field_value(self, int16_t index): raise NotImplementedError("Not implemented on base StructOrUnion class") + @staticmethod + def from_python(obj: StructOrUnion) -> StructOrUnion: + if not isinstance(obj, StructOrUnion): + raise TypeError(f'value {obj} expected to be a thrift-python Struct or Union, was actually of type ' f'{type(obj)}') + return obj + @classmethod def _fbthrift_auto_migrate_enabled(cls): return False