diff --git a/third-party/thrift/src/thrift/compiler/generate/templates/patch/thrift_patch.py.mustache b/third-party/thrift/src/thrift/compiler/generate/templates/patch/thrift_patch.py.mustache index 50a9306005753..92d0606ea852b 100644 --- a/third-party/thrift/src/thrift/compiler/generate/templates/patch/thrift_patch.py.mustache +++ b/third-party/thrift/src/thrift/compiler/generate/templates/patch/thrift_patch.py.mustache @@ -43,6 +43,7 @@ from common.thrift.patch.detail.py_bindings.DynamicPatch import ( BinaryPatch, StructPatch as DynamicStructPatch, UnionPatch as DynamicUnionPatch, + DynamicPatch ) import thrift.python.types as _fbthrift_python_types @@ -73,8 +74,14 @@ class {{struct:py_name}}Patch( {{#field:type}}{{> ../python/types/unadapted_pep484_type}}{{/field:type}}, {{#field:type}}{{> ../python/types/unadapted_pep484_patch_type}}{{/field:type}}]: + {{#field:type}} + {{! e.g., if field is an i32, this function convert DynamicPatch --> I32Patch }} + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> {{#field:type}}{{> ../python/types/unadapted_pep484_patch_type}}{{/field:type}}: + {{> ../python/common/thrift_patch_callback}} + {{/field:type}} + return {{> types/field_patch_type}}( - {{#field:type}}{{> ../python/common/thrift_patch_callback}}{{/field:type}}, + cast_dynamic_patch_to_typed_field_patch, self._patch, {{field:key}}, {{#field:type}}{{> ../python/types/typeinfo }}{{/field:type}}) diff --git a/third-party/thrift/src/thrift/compiler/generate/templates/python/common/thrift_patch_callback.mustache b/third-party/thrift/src/thrift/compiler/generate/templates/python/common/thrift_patch_callback.mustache index 64f63bbdabfe6..3e9f33fe62b54 100644 --- a/third-party/thrift/src/thrift/compiler/generate/templates/python/common/thrift_patch_callback.mustache +++ b/third-party/thrift/src/thrift/compiler/generate/templates/python/common/thrift_patch_callback.mustache @@ -14,24 +14,51 @@ See the License for the specific language governing permissions and limitations under the License. -}}{{! - Lambda is not type checked by pyre, though here we only used lambda internally, it won't affect UX. -}}lambda patch, type_info: {{! -}}{{#type:bool?}}patch.as_bool_patch(){{/type:bool?}}{{! -}}{{#type:byte?}}patch.as_byte_patch(){{/type:byte?}}{{! -}}{{#type:i16?}}patch.as_i16_patch(){{/type:i16?}}{{! -}}{{#type:i32?}}patch.as_i32_patch(){{/type:i32?}}{{! -}}{{#type:i64?}}patch.as_i64_patch(){{/type:i64?}}{{! -}}{{#type:double?}}patch.as_double_patch(){{/type:double?}}{{! -}}{{#type:float?}}patch.as_float_patch(){{/type:float?}}{{! -}}{{#type:string?}}patch.as_string_patch(){{/type:string?}}{{! -}}{{#type:binary?}}patch.as_binary_patch(){{/type:binary?}}{{! -}}{{#type:enum}}patch.as_enum_patch(){{/type:enum}}{{! -}}{{#type:list?}}ListPatch(patch.as_list_patch(), type_info){{/type:list?}}{{! -}}{{#type:set?}}SetPatch(patch.as_set_patch(), type_info){{/type:set?}}{{! -}}{{#type:map?}}{{#type:value_type}}{{! -}}MapPatch({{> common/thrift_patch_callback}}, patch.as_map_patch(), type_info){{! -}}{{/type:value_type}}{{/type:map?}}{{! -}}{{#type:struct}}{{! - }}{{#type:need_patch_module_path?}}{{type:patch_module_path}}.{{/type:need_patch_module_path?}}{{struct:py_name}}Patch(patch){{! -}}{{/type:struct}} +}} +{{#type:bool?}} +return patch.as_bool_patch() +{{/type:bool?}} +{{#type:byte?}} +return patch.as_byte_patch() +{{/type:byte?}} +{{#type:i16?}} +return patch.as_i16_patch() +{{/type:i16?}} +{{#type:i32?}} +return patch.as_i32_patch() +{{/type:i32?}} +{{#type:i64?}} +return patch.as_i64_patch() +{{/type:i64?}} +{{#type:float?}} +return patch.as_float_patch() +{{/type:float?}} +{{#type:double?}} +return patch.as_double_patch() +{{/type:double?}} +{{#type:string?}} +return patch.as_string_patch() +{{/type:string?}} +{{#type:binary?}} +return patch.as_binary_patch() +{{/type:binary?}} +{{#type:enum?}} +return patch.as_enum_patch() +{{/type:enum?}} +{{#type:list?}} +return ListPatch(patch.as_list_patch(), type_info) +{{/type:list?}} +{{#type:set?}} +return SetPatch(patch.as_set_patch(), type_info) +{{/type:set?}} +{{#type:map?}} +{{! e.g., if map value is an i32, this function convert DynamicPatch --> I32Patch }} +{{#type:value_type}} +def cast_dynamic_patch_to_typed_map_value_patch(patch: DynamicPatch, type_info) -> {{> types/unadapted_pep484_patch_type}}: + {{> common/thrift_patch_callback}} +{{/type:value_type}} +return MapPatch(cast_dynamic_patch_to_typed_map_value_patch, patch.as_map_patch(), type_info) +{{/type:map?}} +{{#type:struct}} +return {{#type:need_patch_module_path?}}{{type:patch_module_path}}.{{/type:need_patch_module_path?}}{{struct:py_name}}Patch(patch) +{{/type:struct}} diff --git a/third-party/thrift/src/thrift/compiler/test/fixtures/basic/out/python_patch/gen-python-patch/test/fixtures/basic/module/thrift_patch.py b/third-party/thrift/src/thrift/compiler/test/fixtures/basic/out/python_patch/gen-python-patch/test/fixtures/basic/module/thrift_patch.py index 4b0b77e4ba9fe..e62b193de48c4 100644 --- a/third-party/thrift/src/thrift/compiler/test/fixtures/basic/out/python_patch/gen-python-patch/test/fixtures/basic/module/thrift_patch.py +++ b/third-party/thrift/src/thrift/compiler/test/fixtures/basic/out/python_patch/gen-python-patch/test/fixtures/basic/module/thrift_patch.py @@ -26,6 +26,7 @@ BinaryPatch, StructPatch as DynamicStructPatch, UnionPatch as DynamicUnionPatch, + DynamicPatch ) import thrift.python.types as _fbthrift_python_types @@ -45,8 +46,11 @@ def MyIntField(self) -> UnqualifiedFieldPatch[ int, I64Patch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> I64Patch: + return patch.as_i64_patch() + return UnqualifiedFieldPatch( - lambda patch, type_info: patch.as_i64_patch(), + cast_dynamic_patch_to_typed_field_patch, self._patch, 1, _fbthrift_python_types.typeinfo_i64) @@ -69,8 +73,11 @@ def MyStringField(self) -> UnqualifiedFieldPatch[ str, StringPatch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> StringPatch: + return patch.as_string_patch() + return UnqualifiedFieldPatch( - lambda patch, type_info: patch.as_string_patch(), + cast_dynamic_patch_to_typed_field_patch, self._patch, 2, _fbthrift_python_types.typeinfo_string) @@ -93,8 +100,11 @@ def MyDataField(self) -> UnqualifiedFieldPatch[ _fbthrift__test__fixtures__basic__module__thrift_types.MyDataItem, MyDataItemPatch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> MyDataItemPatch: + return MyDataItemPatch(patch) + return UnqualifiedFieldPatch( - lambda patch, type_info: MyDataItemPatch(patch), + cast_dynamic_patch_to_typed_field_patch, self._patch, 3, _fbthrift_python_types.StructTypeInfo(_fbthrift__test__fixtures__basic__module__thrift_types.MyDataItem)) @@ -117,8 +127,11 @@ def myEnum(self) -> UnqualifiedFieldPatch[ _fbthrift__test__fixtures__basic__module__thrift_types.MyEnum, _fbthrift__test__fixtures__basic__module__thrift_types.MyEnum]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> _fbthrift__test__fixtures__basic__module__thrift_types.MyEnum: + return patch.as_enum_patch() + return UnqualifiedFieldPatch( - lambda patch, type_info: patch.as_enum_patch(), + cast_dynamic_patch_to_typed_field_patch, self._patch, 4, _fbthrift_python_types.EnumTypeInfo(_fbthrift__test__fixtures__basic__module__thrift_types.MyEnum)) @@ -141,8 +154,11 @@ def oneway(self) -> UnqualifiedFieldPatch[ bool, BoolPatch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> BoolPatch: + return patch.as_bool_patch() + return UnqualifiedFieldPatch( - lambda patch, type_info: patch.as_bool_patch(), + cast_dynamic_patch_to_typed_field_patch, self._patch, 5, _fbthrift_python_types.typeinfo_bool) @@ -165,8 +181,11 @@ def readonly(self) -> UnqualifiedFieldPatch[ bool, BoolPatch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> BoolPatch: + return patch.as_bool_patch() + return UnqualifiedFieldPatch( - lambda patch, type_info: patch.as_bool_patch(), + cast_dynamic_patch_to_typed_field_patch, self._patch, 6, _fbthrift_python_types.typeinfo_bool) @@ -189,8 +208,11 @@ def idempotent(self) -> UnqualifiedFieldPatch[ bool, BoolPatch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> BoolPatch: + return patch.as_bool_patch() + return UnqualifiedFieldPatch( - lambda patch, type_info: patch.as_bool_patch(), + cast_dynamic_patch_to_typed_field_patch, self._patch, 7, _fbthrift_python_types.typeinfo_bool) @@ -213,8 +235,11 @@ def floatSet(self) -> UnqualifiedFieldPatch[ _typing.AbstractSet[float], SetPatch[float]]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> SetPatch[float]: + return SetPatch(patch.as_set_patch(), type_info) + return UnqualifiedFieldPatch( - lambda patch, type_info: SetPatch(patch.as_set_patch(), type_info), + cast_dynamic_patch_to_typed_field_patch, self._patch, 8, _fbthrift_python_types.SetTypeInfo(_fbthrift_python_types.typeinfo_float)) @@ -237,8 +262,11 @@ def no_hack_codegen_field(self) -> UnqualifiedFieldPatch[ str, StringPatch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> StringPatch: + return patch.as_string_patch() + return UnqualifiedFieldPatch( - lambda patch, type_info: patch.as_string_patch(), + cast_dynamic_patch_to_typed_field_patch, self._patch, 9, _fbthrift_python_types.typeinfo_string) @@ -266,8 +294,11 @@ def I32List(self) -> UnqualifiedFieldPatch[ _typing.Sequence[int], ListPatch[int]]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> ListPatch[int]: + return ListPatch(patch.as_list_patch(), type_info) + return UnqualifiedFieldPatch( - lambda patch, type_info: ListPatch(patch.as_list_patch(), type_info), + cast_dynamic_patch_to_typed_field_patch, self._patch, 1, _fbthrift_python_types.ListTypeInfo(_fbthrift_python_types.typeinfo_i32)) @@ -290,8 +321,11 @@ def StringSet(self) -> UnqualifiedFieldPatch[ _typing.AbstractSet[str], SetPatch[str]]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> SetPatch[str]: + return SetPatch(patch.as_set_patch(), type_info) + return UnqualifiedFieldPatch( - lambda patch, type_info: SetPatch(patch.as_set_patch(), type_info), + cast_dynamic_patch_to_typed_field_patch, self._patch, 2, _fbthrift_python_types.SetTypeInfo(_fbthrift_python_types.typeinfo_string)) @@ -314,8 +348,13 @@ def StringToI64Map(self) -> UnqualifiedFieldPatch[ _typing.Mapping[str, int], MapPatch[str, int, I64Patch]]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> MapPatch[str, int, I64Patch]: + def cast_dynamic_patch_to_typed_map_value_patch(patch: DynamicPatch, type_info) -> I64Patch: + return patch.as_i64_patch() + return MapPatch(cast_dynamic_patch_to_typed_map_value_patch, patch.as_map_patch(), type_info) + return UnqualifiedFieldPatch( - lambda patch, type_info: MapPatch(lambda patch, type_info: patch.as_i64_patch(), patch.as_map_patch(), type_info), + cast_dynamic_patch_to_typed_field_patch, self._patch, 3, _fbthrift_python_types.MapTypeInfo(_fbthrift_python_types.typeinfo_string, _fbthrift_python_types.typeinfo_i64)) @@ -348,8 +387,11 @@ def myEnum(self) -> OptionalFieldPatch[ _fbthrift__test__fixtures__basic__module__thrift_types.MyEnum, _fbthrift__test__fixtures__basic__module__thrift_types.MyEnum]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> _fbthrift__test__fixtures__basic__module__thrift_types.MyEnum: + return patch.as_enum_patch() + return OptionalFieldPatch( - lambda patch, type_info: patch.as_enum_patch(), + cast_dynamic_patch_to_typed_field_patch, self._patch, 1, _fbthrift_python_types.EnumTypeInfo(_fbthrift__test__fixtures__basic__module__thrift_types.MyEnum)) @@ -372,8 +414,11 @@ def myStruct(self) -> OptionalFieldPatch[ _fbthrift__test__fixtures__basic__module__thrift_types.MyStruct, MyStructPatch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> MyStructPatch: + return MyStructPatch(patch) + return OptionalFieldPatch( - lambda patch, type_info: MyStructPatch(patch), + cast_dynamic_patch_to_typed_field_patch, self._patch, 2, _fbthrift_python_types.StructTypeInfo(_fbthrift__test__fixtures__basic__module__thrift_types.MyStruct)) @@ -396,8 +441,11 @@ def myDataItem(self) -> OptionalFieldPatch[ _fbthrift__test__fixtures__basic__module__thrift_types.MyDataItem, MyDataItemPatch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> MyDataItemPatch: + return MyDataItemPatch(patch) + return OptionalFieldPatch( - lambda patch, type_info: MyDataItemPatch(patch), + cast_dynamic_patch_to_typed_field_patch, self._patch, 3, _fbthrift_python_types.StructTypeInfo(_fbthrift__test__fixtures__basic__module__thrift_types.MyDataItem)) @@ -420,8 +468,11 @@ def floatSet(self) -> OptionalFieldPatch[ _typing.AbstractSet[float], SetPatch[float]]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> SetPatch[float]: + return SetPatch(patch.as_set_patch(), type_info) + return OptionalFieldPatch( - lambda patch, type_info: SetPatch(patch.as_set_patch(), type_info), + cast_dynamic_patch_to_typed_field_patch, self._patch, 4, _fbthrift_python_types.SetTypeInfo(_fbthrift_python_types.typeinfo_float)) @@ -449,8 +500,11 @@ def MyIntField(self) -> UnqualifiedFieldPatch[ int, I64Patch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> I64Patch: + return patch.as_i64_patch() + return UnqualifiedFieldPatch( - lambda patch, type_info: patch.as_i64_patch(), + cast_dynamic_patch_to_typed_field_patch, self._patch, 1, _fbthrift_python_types.typeinfo_i64) @@ -473,8 +527,11 @@ def MyStringField(self) -> UnqualifiedFieldPatch[ str, StringPatch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> StringPatch: + return patch.as_string_patch() + return UnqualifiedFieldPatch( - lambda patch, type_info: patch.as_string_patch(), + cast_dynamic_patch_to_typed_field_patch, self._patch, 2, _fbthrift_python_types.typeinfo_string) @@ -497,8 +554,11 @@ def myStruct(self) -> UnqualifiedFieldPatch[ _fbthrift__test__fixtures__basic__module__thrift_types.MyStruct, MyStructPatch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> MyStructPatch: + return MyStructPatch(patch) + return UnqualifiedFieldPatch( - lambda patch, type_info: MyStructPatch(patch), + cast_dynamic_patch_to_typed_field_patch, self._patch, 3, _fbthrift_python_types.StructTypeInfo(_fbthrift__test__fixtures__basic__module__thrift_types.MyStruct)) @@ -521,8 +581,11 @@ def myUnion(self) -> UnqualifiedFieldPatch[ _fbthrift__test__fixtures__basic__module__thrift_types.MyUnion, MyUnionPatch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> MyUnionPatch: + return MyUnionPatch(patch) + return UnqualifiedFieldPatch( - lambda patch, type_info: MyUnionPatch(patch), + cast_dynamic_patch_to_typed_field_patch, self._patch, 4, _fbthrift_python_types.StructTypeInfo(_fbthrift__test__fixtures__basic__module__thrift_types.MyUnion)) @@ -550,8 +613,11 @@ def MyIntField(self) -> UnqualifiedFieldPatch[ int, I64Patch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> I64Patch: + return patch.as_i64_patch() + return UnqualifiedFieldPatch( - lambda patch, type_info: patch.as_i64_patch(), + cast_dynamic_patch_to_typed_field_patch, self._patch, 1, _fbthrift_python_types.typeinfo_i64) @@ -574,8 +640,11 @@ def MyStringField(self) -> UnqualifiedFieldPatch[ str, StringPatch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> StringPatch: + return patch.as_string_patch() + return UnqualifiedFieldPatch( - lambda patch, type_info: patch.as_string_patch(), + cast_dynamic_patch_to_typed_field_patch, self._patch, 2, _fbthrift_python_types.typeinfo_string) @@ -598,8 +667,11 @@ def myStruct(self) -> UnqualifiedFieldPatch[ _fbthrift__test__fixtures__basic__module__thrift_types.MyStruct, MyStructPatch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> MyStructPatch: + return MyStructPatch(patch) + return UnqualifiedFieldPatch( - lambda patch, type_info: MyStructPatch(patch), + cast_dynamic_patch_to_typed_field_patch, self._patch, 3, _fbthrift_python_types.StructTypeInfo(_fbthrift__test__fixtures__basic__module__thrift_types.MyStruct)) @@ -622,8 +694,11 @@ def myUnion(self) -> UnqualifiedFieldPatch[ _fbthrift__test__fixtures__basic__module__thrift_types.MyUnion, MyUnionPatch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> MyUnionPatch: + return MyUnionPatch(patch) + return UnqualifiedFieldPatch( - lambda patch, type_info: MyUnionPatch(patch), + cast_dynamic_patch_to_typed_field_patch, self._patch, 4, _fbthrift_python_types.StructTypeInfo(_fbthrift__test__fixtures__basic__module__thrift_types.MyUnion)) @@ -651,8 +726,11 @@ def reserved_field(self) -> UnqualifiedFieldPatch[ int, I32Patch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> I32Patch: + return patch.as_i32_patch() + return UnqualifiedFieldPatch( - lambda patch, type_info: patch.as_i32_patch(), + cast_dynamic_patch_to_typed_field_patch, self._patch, 1, _fbthrift_python_types.typeinfo_i32) @@ -680,8 +758,11 @@ def reserved_field(self) -> OptionalFieldPatch[ int, I32Patch]: + def cast_dynamic_patch_to_typed_field_patch(patch: DynamicPatch, type_info) -> I32Patch: + return patch.as_i32_patch() + return OptionalFieldPatch( - lambda patch, type_info: patch.as_i32_patch(), + cast_dynamic_patch_to_typed_field_patch, self._patch, 1, _fbthrift_python_types.typeinfo_i32)