From f14fac963b8b74cfcaa9baad81666cdbeb50c11a Mon Sep 17 00:00:00 2001 From: Satish Kumar Date: Tue, 26 Nov 2024 12:22:33 -0800 Subject: [PATCH] Add tests for unions derived from abstract unions Summary: ... and remove unnecessary tests to reduce verbosity. # Rationale {D66151439} above this one changes the implementation of unions in the presence of abstract types and there were no tests that would catch regression. Introduce the test first to ensure it works correctly with the current implementation. It will then serve as the safety net when D66151439 makes its change. # Action Items Generate fixtures with ``` buck2 run fbcode//thrift/compiler/test:build_fixtures ``` Reviewed By: ahilger Differential Revision: D66214545 fbshipit-source-id: f58ce5811bfc708c774e97021f10ff8b26026350 --- .../test/thrift-python/abstract_types_test.py | 95 +++++++------------ 1 file changed, 34 insertions(+), 61 deletions(-) diff --git a/thrift/test/thrift-python/abstract_types_test.py b/thrift/test/thrift-python/abstract_types_test.py index eee96eb622f..ce4585e5f54 100644 --- a/thrift/test/thrift-python/abstract_types_test.py +++ b/thrift/test/thrift-python/abstract_types_test.py @@ -87,7 +87,9 @@ def setUp(self) -> None: # Disable maximum printed diff length. self.maxDiff = None - def test_fn_call_with_read_only_abstract_base_class_with_immutable(self) -> None: + def test_fn_call_with_read_only_abstract_base_class_with_immutable_struct( + self, + ) -> None: """ type-check will fail with the error below if TestStructImmutable does not inherit from TestStructAbstract. @@ -98,48 +100,15 @@ def test_fn_call_with_read_only_abstract_base_class_with_immutable(self) -> None # GIVEN def library_fn(ts: TestStructAbstract) -> None: # THEN - self.assertEqual(ts.unqualified_string, "hello") - self.assertIsNone(ts.optional_string) + self.assertIsInstance(ts, TestStructAbstract) # WHEN library_fn(TestStructImmutable(unqualified_string="hello")) - - def test_type_hint_with_read_only_abstract_base_class_with_immutable(self) -> None: - """ - type-check will fail with the error below if - TestStructImmutable does not inherit from TestStructAbstract. - - Incompatible variable type [9]: ts is declared to have type `TestStructAbstract` but is used as type `TestStructImmutable`. - """ - # GIVEN - # TestStructAbstract - # TestStructImmutable - - # WHEN - ts: TestStructAbstract = TestStructImmutable(unqualified_string="hello") - - # THEN - self.assertEqual(ts.unqualified_string, "hello") - self.assertIsNone(ts.optional_string) - - def test_isinstance_with_read_only_abstract_base_class_with_immutable(self) -> None: - """ """ - # GIVEN - # TestStructAbstract - # TestStructImmutable - - # WHEN - # This assignment is here to validate type-checking. - ts: TestStructAbstract = TestStructImmutable(unqualified_string="hello") - - # THEN - self.assertIsInstance(ts, TestStructAbstract) - - def test_issubclass_with_read_only_abstract_base_class_with_immutable(self) -> None: - """ """ self.assertTrue(issubclass(TestStructImmutable, TestStructAbstract)) - def test_fn_call_with_read_only_abstract_base_class_with_mutable(self) -> None: + def test_fn_call_with_read_only_abstract_base_class_with_mutable_struct( + self, + ) -> None: """ type-check will fail with the error below if TestStructMutable does not inherit from TestStructAbstract. @@ -150,45 +119,49 @@ def test_fn_call_with_read_only_abstract_base_class_with_mutable(self) -> None: # GIVEN def library_fn(ts: TestStructAbstract) -> None: # THEN - self.assertEqual(ts.unqualified_string, "hello") - self.assertIsNone(ts.optional_string) + self.assertIsInstance(ts, TestStructAbstract) # WHEN library_fn(TestStructMutable(unqualified_string="hello")) + self.assertTrue(issubclass(TestStructMutable, TestStructAbstract)) - def test_type_hint_with_read_only_abstract_base_class_with_mutable(self) -> None: + def test_fn_call_with_read_only_abstract_base_class_with_immutable_union( + self, + ) -> None: """ type-check will fail with the error below if - TestStructMutable does not inherit from TestStructAbstract. + TestUnionImmutable does not inherit from TestUnionAbstract. - Incompatible variable type [9]: ts is declared to have type `TestStructAbstract` but is used as type `TestStructMutable`. + Incompatible parameter type [6]: In call `ThriftPythonAbstractTypesTest.test_fn_call_with_read_only_abstract_base_class_with_immutable.library_fn`, for 1st positional argument, expected `TestUnionAbstract` but got `TestUnionImmutable`. """ + # GIVEN - # TestStructAbstract - # TestStructMutable + def library_fn(ts: TestUnionAbstract) -> None: + # THEN + self.assertIsInstance(ts, TestUnionAbstract) # WHEN - ts: TestStructAbstract = TestStructMutable(unqualified_string="hello") + library_fn(TestUnionImmutable(string_field="hello")) + self.assertTrue(issubclass(TestUnionImmutable, TestUnionAbstract)) - # THEN - self.assertEqual(ts.unqualified_string, "hello") - self.assertIsNone(ts.optional_string) + def test_fn_call_with_read_only_abstract_base_class_with_mutable_union( + self, + ) -> None: + """ + type-check will fail with the error below if + TestUnionMutable does not inherit from TestUnionAbstract. + + Incompatible parameter type [6]: In call `ThriftPythonAbstractTypesTest.test_fn_call_with_read_only_abstract_base_class_with_mutable.library_fn`, for 1st positional argument, expected `TestUnionAbstract` but got `TestUnionMutable`. + """ - def test_isinstance_with_read_only_abstract_base_class_with_mutable(self) -> None: - """ """ # GIVEN - # TestStructAbstract - # TestStructMutable + def library_fn(ts: TestUnionAbstract) -> None: + # THEN + self.assertIsInstance(ts, TestUnionAbstract) # WHEN - ts: TestStructAbstract = TestStructMutable(unqualified_string="hello") - - # THEN - self.assertIsInstance(ts, TestStructAbstract) - - def test_issubclass_with_read_only_abstract_base_class_with_mutable(self) -> None: - """ """ - self.assertTrue(issubclass(TestStructMutable, TestStructAbstract)) + library_fn(TestUnionMutable(string_field="hello")) + self.assertTrue(issubclass(TestUnionMutable, TestUnionAbstract)) @parameterized.expand( [