Skip to content

Commit

Permalink
Rename FBTHRIFT_UNION_EMPTY to EMPTY
Browse files Browse the repository at this point in the history
Summary: Rename `FBTHRIFT_UNION_EMPTY`  to `EMPTY` to reduce the disparity between immutable and mutable union APIs.

Reviewed By: vitaut

Differential Revision: D63893953

fbshipit-source-id: 2f64d44868d2a7f1e7cb2b99a12f7908036867ef
  • Loading branch information
yoney authored and facebook-github-bot committed Oct 4, 2024
1 parent 4798f51 commit 6d525f2
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class {{> structs/unadapted_name}}({{!
{{/program:generate_mutable_types}}
{{#program:generate_mutable_types}}
class FbThriftUnionFieldEnum(enum.Enum):
FBTHRIFT_UNION_EMPTY: {{> structs/unadapted_name}}.FbThriftUnionFieldEnum = ...
EMPTY: {{> structs/unadapted_name}}.FbThriftUnionFieldEnum = ...
{{/program:generate_mutable_types}}
{{#struct:fields_ordered_by_id}}
{{field:py_name}}: {{> structs/unadapted_name}}.Type = ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class MyUnion(_fbthrift_python_mutable_types.MutableUnion, _fbthrift_compatible_


class FbThriftUnionFieldEnum(enum.Enum):
FBTHRIFT_UNION_EMPTY: MyUnion.FbThriftUnionFieldEnum = ...
EMPTY: MyUnion.FbThriftUnionFieldEnum = ...
myEnum: MyUnion.Type = ...
myStruct: MyUnion.Type = ...
myDataItem: MyUnion.Type = ...
Expand Down Expand Up @@ -262,7 +262,7 @@ class UnionToBeRenamed(_fbthrift_python_mutable_types.MutableUnion, _fbthrift_co


class FbThriftUnionFieldEnum(enum.Enum):
FBTHRIFT_UNION_EMPTY: UnionToBeRenamed.FbThriftUnionFieldEnum = ...
EMPTY: UnionToBeRenamed.FbThriftUnionFieldEnum = ...
reserved_field: UnionToBeRenamed.Type = ...

fbthrift_current_value: _typing.Final[_typing.Union[None, int]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ class MyUnion(_fbthrift_python_mutable_types.MutableUnion, _fbthrift_compatible_


class FbThriftUnionFieldEnum(enum.Enum):
FBTHRIFT_UNION_EMPTY: MyUnion.FbThriftUnionFieldEnum = ...
EMPTY: MyUnion.FbThriftUnionFieldEnum = ...
myEnum: MyUnion.Type = ...
myStruct: MyUnion.Type = ...
myDataItem: MyUnion.Type = ...
Expand Down Expand Up @@ -437,7 +437,7 @@ class MyUnionFloatFieldThrowExp(_fbthrift_python_mutable_types.MutableUnion, _fb


class FbThriftUnionFieldEnum(enum.Enum):
FBTHRIFT_UNION_EMPTY: MyUnionFloatFieldThrowExp.FbThriftUnionFieldEnum = ...
EMPTY: MyUnionFloatFieldThrowExp.FbThriftUnionFieldEnum = ...
myEnum: MyUnionFloatFieldThrowExp.Type = ...
setFloat: MyUnionFloatFieldThrowExp.Type = ...
myDataItem: MyUnionFloatFieldThrowExp.Type = ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ComplexUnion(_fbthrift_python_mutable_types.MutableUnion, _fbthrift_compat


class FbThriftUnionFieldEnum(enum.Enum):
FBTHRIFT_UNION_EMPTY: ComplexUnion.FbThriftUnionFieldEnum = ...
EMPTY: ComplexUnion.FbThriftUnionFieldEnum = ...
intValue: ComplexUnion.Type = ...
intListValue: ComplexUnion.Type = ...
stringListValue: ComplexUnion.Type = ...
Expand Down Expand Up @@ -85,7 +85,7 @@ class ListUnion(_fbthrift_python_mutable_types.MutableUnion, _fbthrift_compatibl


class FbThriftUnionFieldEnum(enum.Enum):
FBTHRIFT_UNION_EMPTY: ListUnion.FbThriftUnionFieldEnum = ...
EMPTY: ListUnion.FbThriftUnionFieldEnum = ...
intListValue: ListUnion.Type = ...
stringListValue: ListUnion.Type = ...

Expand Down Expand Up @@ -113,7 +113,7 @@ class DataUnion(_fbthrift_python_mutable_types.MutableUnion, _fbthrift_compatibl


class FbThriftUnionFieldEnum(enum.Enum):
FBTHRIFT_UNION_EMPTY: DataUnion.FbThriftUnionFieldEnum = ...
EMPTY: DataUnion.FbThriftUnionFieldEnum = ...
binaryData: DataUnion.Type = ...
stringData: DataUnion.Type = ...

Expand Down Expand Up @@ -169,7 +169,7 @@ class ValUnion(_fbthrift_python_mutable_types.MutableUnion, _fbthrift_compatible


class FbThriftUnionFieldEnum(enum.Enum):
FBTHRIFT_UNION_EMPTY: ValUnion.FbThriftUnionFieldEnum = ...
EMPTY: ValUnion.FbThriftUnionFieldEnum = ...
v1: ValUnion.Type = ...
v2: ValUnion.Type = ...

Expand Down Expand Up @@ -197,7 +197,7 @@ class VirtualComplexUnion(_fbthrift_python_mutable_types.MutableUnion, _fbthrift


class FbThriftUnionFieldEnum(enum.Enum):
FBTHRIFT_UNION_EMPTY: VirtualComplexUnion.FbThriftUnionFieldEnum = ...
EMPTY: VirtualComplexUnion.FbThriftUnionFieldEnum = ...
thingOne: VirtualComplexUnion.Type = ...
thingTwo: VirtualComplexUnion.Type = ...

Expand Down Expand Up @@ -245,7 +245,7 @@ class NonCopyableUnion(_fbthrift_python_mutable_types.MutableUnion, _fbthrift_co


class FbThriftUnionFieldEnum(enum.Enum):
FBTHRIFT_UNION_EMPTY: NonCopyableUnion.FbThriftUnionFieldEnum = ...
EMPTY: NonCopyableUnion.FbThriftUnionFieldEnum = ...
s: NonCopyableUnion.Type = ...

fbthrift_current_value: _typing.Final[_typing.Union[None, NonCopyableStruct]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class union1(_fbthrift_python_mutable_types.MutableUnion, _fbthrift_compatible_w


class FbThriftUnionFieldEnum(enum.Enum):
FBTHRIFT_UNION_EMPTY: union1.FbThriftUnionFieldEnum = ...
EMPTY: union1.FbThriftUnionFieldEnum = ...
i: union1.Type = ...
d: union1.Type = ...

Expand Down Expand Up @@ -283,7 +283,7 @@ class union2(_fbthrift_python_mutable_types.MutableUnion, _fbthrift_compatible_w


class FbThriftUnionFieldEnum(enum.Enum):
FBTHRIFT_UNION_EMPTY: union2.FbThriftUnionFieldEnum = ...
EMPTY: union2.FbThriftUnionFieldEnum = ...
i: union2.Type = ...
d: union2.Type = ...
s: union2.Type = ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ class Onion(_fbthrift_python_mutable_types.MutableUnion, _fbthrift_compatible_wi


class FbThriftUnionFieldEnum(enum.Enum):
FBTHRIFT_UNION_EMPTY: Onion.FbThriftUnionFieldEnum = ...
EMPTY: Onion.FbThriftUnionFieldEnum = ...
myEnum: Onion.Type = ...
myStruct: Onion.Type = ...
myString: Onion.Type = ...
Expand Down
4 changes: 2 additions & 2 deletions thrift/lib/python/mutable_types.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ cdef class MutableUnion(MutableStructOrUnion):
to the underlying Thrift type.
Raises:
AssertionError if `field_id` is 0 (i.e., FBTHRIFT_UNION_EMPTY) but
AssertionError if `field_id` is 0 (i.e., EMPTY) but
`field_python_value` is not `None`.
Exception if the operation cannot be completely successfully.
Expand Down Expand Up @@ -956,7 +956,7 @@ cdef class MutableUnion(MutableStructOrUnion):
return f"{type(self).__name__}({self.fbthrift_current_field.name}={self.fbthrift_current_value!r})"

def _gen_mutable_union_field_enum_members(field_infos):
yield ("FBTHRIFT_UNION_EMPTY", 0)
yield ("EMPTY", 0)
for f in field_infos:
yield (f.py_name, f.id)

Expand Down
3 changes: 1 addition & 2 deletions thrift/lib/python/test/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,8 @@ def test_dir(self) -> None:
self.assertEqual(expected, dir(self.easy))

def test_repr(self) -> None:
union_empty = "EMPTY" if not self.is_mutable_run else "FBTHRIFT_UNION_EMPTY"
self.assertEqual(
f"easy(val=42, val_list=i[], name=None, an_int=Integers({union_empty}=None), py3_hidden=0)",
"easy(val=42, val_list=i[], name=None, an_int=Integers(EMPTY=None), py3_hidden=0)",
repr(self.easy(val=42)),
)

Expand Down
13 changes: 1 addition & 12 deletions thrift/lib/python/test/unions.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,6 @@ def get_enum(self, thrift_union):
else thrift_union.FbThriftUnionFieldEnum
)

# pyre-ignore[2, 3]: no type sepcified
def get_empty(self, union_type_enum):
"""
Returns Type.EMPTY or FbThriftUnionFieldEnum.FBTHRIFT_UNION_EMPTY
"""
return (
union_type_enum.EMPTY
if not self.is_mutable_run
else union_type_enum.FBTHRIFT_UNION_EMPTY
)

def test_constructor(self) -> None:
self.assertEqual(
self.get_type(self.Integers(small=2)),
Expand Down Expand Up @@ -242,7 +231,7 @@ def test_deserialize_empty(self) -> None:
x = self.serializer.deserialize(
self.Integers, self.serializer.serialize_iobuf(self.Integers())
)
self.assertEqual(self.get_type(x), self.get_empty(self.get_enum(self.Integers)))
self.assertEqual(self.get_type(x), self.get_enum(self.Integers).EMPTY)

def test_deserialize_nonempty(self) -> None:
x = self.serializer.deserialize(
Expand Down
14 changes: 7 additions & 7 deletions thrift/test/thrift-python/union_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,14 +453,14 @@ def test_creation(self) -> None:
u = TestUnionMutable()
self.assertIs(
u.fbthrift_current_field,
TestUnionMutable.FbThriftUnionFieldEnum.FBTHRIFT_UNION_EMPTY,
TestUnionMutable.FbThriftUnionFieldEnum.EMPTY,
)
self.assertIsNone(u.fbthrift_current_value)
with self.assertRaisesRegex(
AttributeError,
(
r"Error retrieving Thrift union \(TestUnion\) field: requested "
r"'string_field', but currently holds 'FBTHRIFT_UNION_EMPTY'."
r"'string_field', but currently holds 'EMPTY'."
),
):
u.string_field
Expand Down Expand Up @@ -568,12 +568,12 @@ def test_class_field_enum(self) -> None:
"string_field",
"int_field",
"struct_field",
"FBTHRIFT_UNION_EMPTY",
"EMPTY",
],
fields_enum_type.__members__.keys(),
)

self.assertIs(fields_enum_type(0), fields_enum_type.FBTHRIFT_UNION_EMPTY)
self.assertIs(fields_enum_type(0), fields_enum_type.EMPTY)

enum_names_and_values = {
member.name: member.value
Expand All @@ -583,7 +583,7 @@ def test_class_field_enum(self) -> None:
self.assertEqual(
enum_names_and_values,
{
"FBTHRIFT_UNION_EMPTY": 0,
"EMPTY": 0,
"string_field": 1,
"int_field": 2,
"struct_field": 3,
Expand All @@ -594,7 +594,7 @@ def test_adapted_types(self) -> None:
u1 = TestUnionAdaptedTypesMutable()
self.assertIs(
u1.fbthrift_current_field,
TestUnionAdaptedTypesMutable.FbThriftUnionFieldEnum.FBTHRIFT_UNION_EMPTY,
TestUnionAdaptedTypesMutable.FbThriftUnionFieldEnum.EMPTY,
)
self.assertIsNone(u1.fbthrift_current_value)

Expand Down Expand Up @@ -677,7 +677,7 @@ def test_set_field(self) -> None:
u = TestUnionMutable()
self.assertIs(
u.fbthrift_current_field,
TestUnionMutable.FbThriftUnionFieldEnum.FBTHRIFT_UNION_EMPTY,
TestUnionMutable.FbThriftUnionFieldEnum.EMPTY,
)
self.assertIsNone(u.fbthrift_current_value)

Expand Down

0 comments on commit 6d525f2

Please sign in to comment.