Skip to content

Commit

Permalink
Add tests for unions derived from abstract unions
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Satish Kumar authored and facebook-github-bot committed Nov 26, 2024
1 parent bedb9d2 commit f14fac9
Showing 1 changed file with 34 additions and 61 deletions.
95 changes: 34 additions & 61 deletions thrift/test/thrift-python/abstract_types_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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(
[
Expand Down

0 comments on commit f14fac9

Please sign in to comment.