Skip to content

Commit cce8822

Browse files
ahilgerfacebook-github-bot
authored andcommitted
use the thrift-py3 __Type enum in unions
Summary: The `Union.type` and `Union.Type` api was left partially unimplemented in the hopes we could easily unify thrift-py3 and thrift-python enums on trunk. Since that appears to be a non-trivial lift, add a test documenting current behavior and update inplace-migrate to have self-consistent behavior that matches py3 trunk. Reviewed By: createdbysk Differential Revision: D73063538 fbshipit-source-id: a8774af8909938d685712075505d366bcebd54dc
1 parent 0e610f4 commit cce8822

File tree

6 files changed

+59
-14
lines changed

6 files changed

+59
-14
lines changed

Diff for: thrift/compiler/generate/templates/py3/types_inplace_FBTHRIFT_ONLY_DO_NOT_USE.py.mustache

+8-2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ __all__.append("{{enum:name}}")
8080

8181
{{> inplace/union_enum_py }}
8282

83+
84+
{{! this is a dumb hack due to dunder class mangling rules}}
85+
_{{struct:name}}__{{struct:name}}Type = __{{struct:name}}Type
86+
8387
{{#program:inplace_migrate?}}
8488
__all__.append("__{{struct:name}}Type")
8589

@@ -120,7 +124,7 @@ class {{struct:name}}({{> types/python_struct_class }}):
120124
_fbthrift_allow_inheritance_DO_NOT_USE = True
121125
{{/struct:allow_inheritance?}}
122126
{{#struct:union?}}
123-
Type = _fbthrift_python_types.{{struct:name}}.Type
127+
Type = __{{struct:name}}Type
124128
{{/struct:union?}}
125129
_fbthrift__inner : _fbthrift_python_types.{{struct:name}}
126130
{{#struct:fields_and_mixin_fields}}
@@ -231,7 +235,9 @@ class {{struct:name}}({{> types/python_struct_class }}):
231235
{{#struct:union?}}
232236
@_python__property
233237
def type(self) -> _fbthrift_python_types.{{struct:name}}.Type:
234-
return self._fbthrift__inner.type
238+
if self._fbthrift_inner__type is None:
239+
self._fbthrift_inner__type = self.Type(self._fbthrift__inner.type.value)
240+
return self._fbthrift_inner__type
235241
236242
@_python__property
237243
def value(self) -> {{> inplace/union_value_type }}:

Diff for: thrift/compiler/test/fixtures/basic/out/py3_inplace/gen-py3/test/fixtures/basic/module/types_inplace_FBTHRIFT_ONLY_DO_NOT_USE.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class __MyUnionType(enum.Enum):
6060

6161
__module__ = _fbthrift__module_name__
6262
__slots__ = ()
63+
64+
_MyUnion__MyUnionType = __MyUnionType
65+
6366
__all__.append("__MyUnionType")
6467

6568

@@ -69,6 +72,9 @@ class __UnionToBeRenamedType(enum.Enum):
6972

7073
__module__ = _fbthrift__module_name__
7174
__slots__ = ()
75+
76+
_UnionToBeRenamed__UnionToBeRenamedType = __UnionToBeRenamedType
77+
7278
__all__.append("__UnionToBeRenamedType")
7379

7480

@@ -740,7 +746,7 @@ class MyUnion(thrift.py3.types.Union):
740746
"myDataItem",
741747
"floatSet",
742748
)
743-
Type = _fbthrift_python_types.MyUnion.Type
749+
Type = __MyUnionType
744750
_fbthrift__inner : _fbthrift_python_types.MyUnion
745751
_fbthrift_inner__myStruct : MyStruct | None
746752
_fbthrift_inner__myDataItem : MyDataItem | None
@@ -828,7 +834,9 @@ def floatSet(self) -> _typing.AbstractSet[float]:
828834

829835
@_python__property
830836
def type(self) -> _fbthrift_python_types.MyUnion.Type:
831-
return self._fbthrift__inner.type
837+
if self._fbthrift_inner__type is None:
838+
self._fbthrift_inner__type = self.Type(self._fbthrift__inner.type.value)
839+
return self._fbthrift_inner__type
832840

833841
@_python__property
834842
def value(self) -> MyEnum | MyStruct | MyDataItem | _typing.AbstractSet[float] | None:
@@ -1256,7 +1264,7 @@ class UnionToBeRenamed(thrift.py3.types.Union):
12561264
_FBTHRIFT__FIELD_NAMES = (
12571265
"reserved_field",
12581266
)
1259-
Type = _fbthrift_python_types.UnionToBeRenamed.Type
1267+
Type = __UnionToBeRenamedType
12601268
_fbthrift__inner : _fbthrift_python_types.UnionToBeRenamed
12611269
_fbthrift_inner__type: Type
12621270
_fbthrift_inner__value: int | None
@@ -1308,7 +1316,9 @@ def reserved_field(self) -> int:
13081316

13091317
@_python__property
13101318
def type(self) -> _fbthrift_python_types.UnionToBeRenamed.Type:
1311-
return self._fbthrift__inner.type
1319+
if self._fbthrift_inner__type is None:
1320+
self._fbthrift_inner__type = self.Type(self._fbthrift__inner.type.value)
1321+
return self._fbthrift_inner__type
13121322

13131323
@_python__property
13141324
def value(self) -> int | None:

Diff for: thrift/compiler/test/fixtures/constants/out/py3_inplace/gen-py3/module/types_inplace_FBTHRIFT_ONLY_DO_NOT_USE.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class __union1Type(enum.Enum):
6363

6464
__module__ = _fbthrift__module_name__
6565
__slots__ = ()
66+
67+
_union1__union1Type = __union1Type
68+
6669
__all__.append("__union1Type")
6770

6871

@@ -75,6 +78,9 @@ class __union2Type(enum.Enum):
7578

7679
__module__ = _fbthrift__module_name__
7780
__slots__ = ()
81+
82+
_union2__union2Type = __union2Type
83+
7884
__all__.append("__union2Type")
7985

8086

@@ -1478,7 +1484,7 @@ class union1(thrift.py3.types.Union):
14781484
"i",
14791485
"d",
14801486
)
1481-
Type = _fbthrift_python_types.union1.Type
1487+
Type = __union1Type
14821488
_fbthrift__inner : _fbthrift_python_types.union1
14831489
_fbthrift_inner__type: Type
14841490
_fbthrift_inner__value: int | float | None
@@ -1534,7 +1540,9 @@ def d(self) -> float:
15341540

15351541
@_python__property
15361542
def type(self) -> _fbthrift_python_types.union1.Type:
1537-
return self._fbthrift__inner.type
1543+
if self._fbthrift_inner__type is None:
1544+
self._fbthrift_inner__type = self.Type(self._fbthrift__inner.type.value)
1545+
return self._fbthrift_inner__type
15381546

15391547
@_python__property
15401548
def value(self) -> int | float | None:
@@ -1617,7 +1625,7 @@ class union2(thrift.py3.types.Union):
16171625
"s",
16181626
"u",
16191627
)
1620-
Type = _fbthrift_python_types.union2.Type
1628+
Type = __union2Type
16211629
_fbthrift__inner : _fbthrift_python_types.union2
16221630
_fbthrift_inner__s : struct1 | None
16231631
_fbthrift_inner__u : union1 | None
@@ -1697,7 +1705,9 @@ def u(self) -> union1:
16971705

16981706
@_python__property
16991707
def type(self) -> _fbthrift_python_types.union2.Type:
1700-
return self._fbthrift__inner.type
1708+
if self._fbthrift_inner__type is None:
1709+
self._fbthrift_inner__type = self.Type(self._fbthrift__inner.type.value)
1710+
return self._fbthrift_inner__type
17011711

17021712
@_python__property
17031713
def value(self) -> int | float | struct1 | union1 | None:

Diff for: thrift/compiler/test/fixtures/py3/out/py3_inplace/gen-py3/module/types_inplace_FBTHRIFT_ONLY_DO_NOT_USE.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class __BinaryUnionType(enum.Enum):
6262

6363
__module__ = _fbthrift__module_name__
6464
__slots__ = ()
65+
66+
_BinaryUnion__BinaryUnionType = __BinaryUnionType
67+
6568
__all__.append("__BinaryUnionType")
6669

6770

@@ -2388,7 +2391,7 @@ class BinaryUnion(thrift.py3.types.Union):
23882391
_FBTHRIFT__FIELD_NAMES = (
23892392
"iobuf_val",
23902393
)
2391-
Type = _fbthrift_python_types.BinaryUnion.Type
2394+
Type = __BinaryUnionType
23922395
_fbthrift__inner : _fbthrift_python_types.BinaryUnion
23932396
_fbthrift_inner__type: Type
23942397
_fbthrift_inner__value: _fbthrift_iobuf.IOBuf | None
@@ -2440,7 +2443,9 @@ def iobuf_val(self) -> _fbthrift_iobuf.IOBuf:
24402443

24412444
@_python__property
24422445
def type(self) -> _fbthrift_python_types.BinaryUnion.Type:
2443-
return self._fbthrift__inner.type
2446+
if self._fbthrift_inner__type is None:
2447+
self._fbthrift_inner__type = self.Type(self._fbthrift__inner.type.value)
2448+
return self._fbthrift_inner__type
24442449

24452450
@_python__property
24462451
def value(self) -> _fbthrift_iobuf.IOBuf | None:

Diff for: thrift/compiler/test/fixtures/py3/out/py3_inplace_auto_migrate/gen-py3/module/types_inplace_FBTHRIFT_ONLY_DO_NOT_USE.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class __BinaryUnionType(enum.Enum):
6262

6363
__module__ = _fbthrift__module_name__
6464
__slots__ = ()
65+
66+
_BinaryUnion__BinaryUnionType = __BinaryUnionType
67+
6568
__all__.append("__BinaryUnionType")
6669

6770

@@ -2388,7 +2391,7 @@ class BinaryUnion(thrift.py3.types.Union):
23882391
_FBTHRIFT__FIELD_NAMES = (
23892392
"iobuf_val",
23902393
)
2391-
Type = _fbthrift_python_types.BinaryUnion.Type
2394+
Type = __BinaryUnionType
23922395
_fbthrift__inner : _fbthrift_python_types.BinaryUnion
23932396
_fbthrift_inner__type: Type
23942397
_fbthrift_inner__value: _fbthrift_iobuf.IOBuf | None
@@ -2440,7 +2443,9 @@ def iobuf_val(self) -> _fbthrift_iobuf.IOBuf:
24402443

24412444
@_python__property
24422445
def type(self) -> _fbthrift_python_types.BinaryUnion.Type:
2443-
return self._fbthrift__inner.type
2446+
if self._fbthrift_inner__type is None:
2447+
self._fbthrift_inner__type = self.Type(self._fbthrift__inner.type.value)
2448+
return self._fbthrift_inner__type
24442449

24452450
@_python__property
24462451
def value(self) -> _fbthrift_iobuf.IOBuf | None:

Diff for: thrift/lib/py3/test/auto_migrate/unions.py

+9
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ def test_union_enum_dir(self) -> None:
8282
for itype in iter(Integers.Type):
8383
self.assertTrue(itype.name in contents)
8484

85+
def test_union_type_enum_name(self) -> None:
86+
type_enum = Integers.Type
87+
if is_auto_migrated():
88+
self.assertEqual(type_enum.__name__, "Integers")
89+
else:
90+
self.assertEqual(type_enum.__name__, "__IntegersType")
91+
92+
self.assertIsInstance(Integers().type, Integers.Type)
93+
8594
def test_union_enum_members(self) -> None:
8695
members = Integers.Type.__members__
8796
# Alias can't happen in this enum so these should always equal

0 commit comments

Comments
 (0)