Skip to content

Commit 8431731

Browse files
hchokshimeta-codesync[bot]
authored andcommitted
Migrate python_mstch_function to Whisker
Reviewed By: iahs Differential Revision: D84536289 fbshipit-source-id: da98756710bb7667f70f73b2a8801ab2315ba005
1 parent 0b70061 commit 8431731

7 files changed

Lines changed: 76 additions & 82 deletions

File tree

third-party/thrift/src/thrift/compiler/generate/t_mstch_python_generator.cc

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -646,28 +646,6 @@ void validate_no_reserved_key_in_namespace(
646646
}
647647
}
648648

649-
class python_mstch_function : public mstch_function {
650-
public:
651-
python_mstch_function(
652-
const t_function* f, mstch_context& ctx, mstch_element_position pos)
653-
: mstch_function(f, ctx, pos) {
654-
register_methods(
655-
this,
656-
{
657-
{"function:regular_response_type",
658-
&python_mstch_function::regular_response_type},
659-
});
660-
}
661-
662-
mstch::node regular_response_type() {
663-
if (function_->qualifier() == t_function_qualifier::oneway) {
664-
return {};
665-
}
666-
const t_type* rettype = function_->return_type()->get_true_type();
667-
return context_.type_factory->make_mstch_object(rettype, context_, pos_);
668-
}
669-
};
670-
671649
class python_mstch_struct : public mstch_struct {
672650
public:
673651
python_mstch_struct(
@@ -1267,7 +1245,6 @@ void t_mstch_python_generator::set_mstch_factories() {
12671245
mstch_context_.add<python_mstch_program>();
12681246
mstch_context_.add<python_mstch_service>();
12691247
mstch_context_.add<python_mstch_interaction>();
1270-
mstch_context_.add<python_mstch_function>();
12711248
mstch_context_.add<python_mstch_struct>();
12721249
}
12731250

third-party/thrift/src/thrift/compiler/generate/templates/python/common/adapter_info.mustache

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@ A tuple of:
1919
1. adapter class,
2020
2. a lambda that returns transitive annotation (if the adapter is applied via transitive annotation, else None.)
2121
if the node has an adapter, else None.
22-
}}{{#pragma ignore-newlines}}
23-
{{#if self.has_adapter?}}
24-
{{#let adapter = self.adapter}}
22+
}}
23+
{{#let export partial adapter_info |node|}}
24+
{{#pragma ignore-newlines}}
25+
{{#if node.has_adapter?}}
26+
{{#let adapter = node.adapter}}
2527
(
2628
{{adapter.name}}, lambda: {{!preserve trailing space}}
27-
{{#if (object.notnull? adapter.transitive_annotation)}}
29+
{{#if (object.notnull? adapter.transitive_annotation)}}
2830
{{! }}{{#adapter.transitive_annotation}}{{> types/constant_value }}{{/adapter.transitive_annotation}}
29-
{{#else}}
31+
{{#else}}
3032
{{! }}None
31-
{{/if (object.notnull? adapter.transitive_annotation)}}
33+
{{/if (object.notnull? adapter.transitive_annotation)}}
3234
)
33-
{{#else}}
35+
{{#else}}
3436
None
35-
{{/if self.has_adapter?}}
37+
{{/if node.has_adapter?}}
38+
{{/let partial}}

third-party/thrift/src/thrift/compiler/generate/templates/python/types/service_arg_types.mustache

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
1717
}}{{!
1818
The argument and result types for functions on thrift services (and interactions)
19-
}}{{#service:supported_functions}}
19+
}}
20+
{{#import "common/adapter_info" as m_adapter_info }}
21+
{{#service:supported_functions}}
2022

2123

2224
class {{> types/function_args_type}}(metaclass={{> types/struct_metaclass}}):
@@ -29,7 +31,7 @@ class {{> types/function_args_type}}(metaclass={{> types/struct_metaclass}}):
2931
"{{field:py_name}}", # python name (from @python.Name annotation)
3032
{{#field:type}}{{^type:primitive?}}lambda: {{/type:primitive?}}{{> types/typeinfo }}{{/field:type}}, # typeinfo
3133
None, # default value
32-
{{> common/adapter_info }}, # adapter info
34+
{{#partial m_adapter_info.adapter_info node=field:self}}, # adapter info
3335
{{#field:type}}{{> types/is_primitive }}{{/field:type}}, # field type is primitive
3436
{{field:idl_type}}, # IDL type (see BaseTypeEnum)
3537
),
@@ -40,20 +42,21 @@ class {{> types/function_args_type}}(metaclass={{> types/struct_metaclass}}):
4042

4143
class _fbthrift_{{service:name}}_{{function:name}}_result(metaclass={{> types/struct_metaclass}}):
4244
_fbthrift_SPEC = (
43-
{{#function:regular_response_type}}
44-
{{^type:void?}}
45+
{{#let regular_response_type = (if function:oneway? null function:self.return_type.true_type)}}
46+
{{#if (and (object.notnull? regular_response_type) (not regular_response_type.void?))}}
47+
{{#regular_response_type}}
4548
_fbthrift_python_types.FieldInfo(
4649
0, # id
4750
_fbthrift_python_types.FieldQualifier.Optional, # qualifier
4851
"success", # name
4952
"success", # name
50-
{{^type:primitive?}}lambda: {{/type:primitive?}}{{> types/typeinfo }}, # typeinfo
53+
{{#if (not regular_response_type.primitive?)}}lambda: {{/if}}{{> types/typeinfo }}, # typeinfo
5154
None, # default value
52-
{{> common/adapter_info}}, # adapter info
55+
{{#partial m_adapter_info.adapter_info node=regular_response_type}}, # adapter info
5356
{{> types/is_primitive }}, # field type is primitive
5457
),
55-
{{/type:void?}}
56-
{{/function:regular_response_type}}
58+
{{/regular_response_type}}
59+
{{/if (and (object.notnull? regular_response_type) (not regular_response_type.void?))}}
5760
{{#function:exceptions}}
5861
_fbthrift_python_types.FieldInfo(
5962
{{field:id}}, # id
@@ -62,7 +65,7 @@ class _fbthrift_{{service:name}}_{{function:name}}_result(metaclass={{> types/st
6265
"{{> fields/exception_field_name}}", # python name (from @python.Name annotation)
6366
{{#field:type}}{{^type:primitive?}}lambda: {{/type:primitive?}}{{> types/typeinfo }}{{/field:type}}, # typeinfo
6467
None, # default value
65-
{{> common/adapter_info }}, # adapter info
68+
{{#partial m_adapter_info.adapter_info node=field:self}}, # adapter info
6669
{{#field:type}}{{> types/is_primitive }}{{/field:type}}, # field type is primitive
6770
{{field:idl_type}}, # IDL type (see BaseTypeEnum)
6871
),
@@ -83,7 +86,7 @@ class _fbthrift_{{service:name}}_{{function:name}}_result_stream_elem(metaclass=
8386
"success", # name
8487
{{^type:primitive?}}lambda: {{/type:primitive?}}{{> types/typeinfo }}, # typeinfo
8588
None, # default value
86-
{{> common/adapter_info}}, # adapter info
89+
{{#partial m_adapter_info.adapter_info node=type:self}}, # adapter info
8790
{{> types/is_primitive }}, # field type is primitive
8891
),
8992
{{/function:stream_elem_type}}
@@ -95,7 +98,7 @@ class _fbthrift_{{service:name}}_{{function:name}}_result_stream_elem(metaclass=
9598
"{{> fields/exception_field_name}}", # python name (from @python.Name annotation)
9699
{{#field:type}}{{^type:primitive?}}lambda: {{/type:primitive?}}{{> types/typeinfo }}{{/field:type}}, # typeinfo
97100
None, # default value
98-
{{> common/adapter_info }}, # adapter info
101+
{{#partial m_adapter_info.adapter_info node=field:self}}, # adapter info
99102
{{#field:type}}{{> types/is_primitive }}{{/field:type}}, # field type is primitive
100103
{{field:idl_type}}, # IDL type (see BaseTypeEnum)
101104
),
@@ -116,7 +119,7 @@ class _fbthrift_{{service:name}}_{{function:name}}_result_sink_elem(metaclass={{
116119
"success", # name
117120
{{^type:primitive?}}lambda: {{/type:primitive?}}{{> types/typeinfo }}, # typeinfo
118121
None, # default value
119-
{{> common/adapter_info}}, # adapter info
122+
{{#partial m_adapter_info.adapter_info node=type:self}}, # adapter info
120123
{{> types/is_primitive }}, # field type is primitive
121124
),
122125
{{/function:sink_elem_type}}
@@ -128,7 +131,7 @@ class _fbthrift_{{service:name}}_{{function:name}}_result_sink_elem(metaclass={{
128131
"{{> fields/exception_field_name}}", # python name (from @python.Name annotation)
129132
{{#field:type}}{{^type:primitive?}}lambda: {{/type:primitive?}}{{> types/typeinfo }}{{/field:type}}, # typeinfo
130133
None, # default value
131-
{{> common/adapter_info }}, # adapter info
134+
{{#partial m_adapter_info.adapter_info node=field:self}}, # adapter info
132135
{{#field:type}}{{> types/is_primitive }}{{/field:type}}, # field type is primitive
133136
{{field:idl_type}}, # IDL type (see BaseTypeEnum)
134137
),
@@ -172,7 +175,7 @@ class _fbthrift_{{service:name}}_{{function:name}}_result_sink_final(metaclass={
172175
"success", # name
173176
{{^type:primitive?}}lambda: {{/type:primitive?}}{{> types/typeinfo }}, # typeinfo
174177
None, # default value
175-
{{> common/adapter_info}}, # adapter info
178+
{{#partial m_adapter_info.adapter_info node=type:self}}, # adapter info
176179
{{> types/is_primitive }}, # field type is primitive
177180
),
178181
{{/function:sink_final_response_type}}
@@ -184,7 +187,7 @@ class _fbthrift_{{service:name}}_{{function:name}}_result_sink_final(metaclass={
184187
"{{> fields/exception_field_name}}", # python name (from @python.Name annotation)
185188
{{#field:type}}{{^type:primitive?}}lambda: {{/type:primitive?}}{{> types/typeinfo }}{{/field:type}}, # typeinfo
186189
None, # default value
187-
{{> common/adapter_info }}, # adapter info
190+
{{#partial m_adapter_info.adapter_info node=field:self}}, # adapter info
188191
{{#field:type}}{{> types/is_primitive }}{{/field:type}}, # field type is primitive
189192
{{field:idl_type}}, # IDL type (see BaseTypeEnum)
190193
),

third-party/thrift/src/thrift/compiler/generate/templates/python/types/service_arg_types_pyi.mustache

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ class {{> types/function_args_type}}({{> types/struct_base }}):
3737

3838

3939
class _fbthrift_{{service:name}}_{{function:name}}_result({{> types/struct_base }}):
40-
{{#function:regular_response_type}}
40+
{{#let regular_response_type = (if function:oneway? null function:self.return_type.true_type)}}
41+
{{#regular_response_type}}
4142
success: _typing.Final[{{> types/pep484_type }}]
42-
{{/function:regular_response_type}}
43+
{{/regular_response_type}}
4344
{{#function:exceptions}}
4445
{{field:py_name}}: _typing.Final[{{#field:type}}{{> types/pep484_type}}{{/field:type}}]
4546
{{/function:exceptions}}
4647

4748
def __init__(
48-
self, *, success: _typing.Optional[{{#function:regular_response_type}}{{> types/pep484_type }}{{/function:regular_response_type}}] = ...{{!
49+
self, *, success: _typing.Optional[{{#regular_response_type}}{{> types/pep484_type }}{{/regular_response_type}}] = ...{{!
4950
}}{{#function:exceptions}}{{!
5051
}}, {{> fields/exception_field_name }}: _typing.Optional[{{#field:type}}{{> types/pep484_type}}{{/field:type}}]=...{{!
5152
}}{{/function:exceptions}}
@@ -54,7 +55,7 @@ class _fbthrift_{{service:name}}_{{function:name}}_result({{> types/struct_base
5455
def __iter__(self) -> _typing.Iterator[_typing.Tuple[
5556
str,
5657
_typing.Union[
57-
{{#function:regular_response_type}}{{> types/pep484_type }}{{/function:regular_response_type}},
58+
{{#regular_response_type}}{{> types/pep484_type }}{{/regular_response_type}},
5859
{{#function:exceptions}}
5960
{{#field:type}}{{> types/pep484_type}}{{/field:type}},
6061
{{/function:exceptions}}

third-party/thrift/src/thrift/compiler/generate/templates/python/types/typeinfo.mustache

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,6 @@ in a different thrift file, or defined in the same thrift file but this is
2424
in the client or server source rather than the type definition source.
2525
2626
}}{{#pragma ignore-newlines}}
27-
{{#if (not (object.is? this type="native_handle"))}}
28-
{{! If we're dealing with a mstch object, recurse this template with its
29-
Whisker prototype as the context. }}
30-
{{#type:self}}
31-
{{> types/typeinfo }}
32-
{{/type:self}}
33-
34-
{{#else}}
35-
{{! We're dealing with a native handle (Whisker prototype). !}}
3627
{{#if type:has_adapter?}}
3728
{{! If the type has an adapter, open AdaptedTypeInfo wrapper }}
3829
_fbthrift_python_types.AdaptedTypeInfo(
@@ -110,5 +101,3 @@ None
110101
{{/if (object.notnull? adapter.transitive_annotation)}}
111102
)
112103
{{/if type:has_adapter?}}
113-
114-
{{/if (not (object.is? this type="native_handle"))}}

third-party/thrift/src/thrift/compiler/generate/templates/python/types/types.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
}}{{!
1818
Generated Python classes for Thrift types
1919
}}
20+
{{#import "common/adapter_info" as m_adapter_info }}
2021
{{> common/auto_generated_py}}
2122

2223
{{#if (not (or python.generate_immutable_types? python.generate_mutable_types?))}}
@@ -90,7 +91,7 @@ class {{> structs/unadapted_name}}{{!
9091
{{#else}}
9192
None, # default value
9293
{{/if (object.notnull? default_val)}}
93-
{{> common/adapter_info }}, # adapter info
94+
{{#partial m_adapter_info.adapter_info node=field:self}}, # adapter info
9495
{{#field:type}}{{> types/is_primitive }}{{/field:type}}, # field type is primitive
9596
{{field:idl_type}}, # IDL type (see BaseTypeEnum)
9697
),

third-party/thrift/src/thrift/compiler/generate/templates/python/types/unadapted_pep484_type.mustache

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,43 @@ This is for pep 484 typing
2121
This is useful for typing call patterns since it allows python container types OR
2222
thrift-python specific container types.
2323
24-
}}{{> types/unadapted_builtin_pep484_type}}{{!
25-
}}{{#type:structured}}{{!
26-
}}{{#type:need_module_path?}}{{type:module_mangle}}.{{/type:need_module_path?}}{{!
27-
}}{{#if (not (or type:need_module_path? struct:has_adapter?))}}{{> private/alias_prefix}}{{/if}}{{> structs/unadapted_name}}{{!
28-
}}{{/type:structured}}{{!
29-
}}{{#type:list?}}{{> types/sequence }}[{{!
30-
}}{{#type:list_elem_type}}{{> types/pep484_type}}{{/type:list_elem_type}}{{!
31-
}}]{{/type:list?}}{{!
32-
}}{{#type:set?}}{{> types/set }}[{{!
33-
}}{{#type:set_elem_type}}{{> types/pep484_type}}{{/type:set_elem_type}}{{!
34-
}}]{{/type:set?}}{{!
35-
}}{{#type:map?}}{{> types/mapping }}[{{!
36-
}}{{#type:key_type}}{{> types/pep484_type}}{{/type:key_type}}, {{!
37-
}}{{#type:value_type}}{{> types/pep484_type}}{{/type:value_type}}{{!
38-
}}]{{/type:map?}}{{!
39-
}}{{#type:void?}}None{{/type:void?}}{{!
40-
}}{{#type:enum}}{{!
41-
}}{{#type:need_module_path?}}{{type:module_mangle}}.{{/type:need_module_path?}}{{!
42-
}}{{^type:need_module_path?}}{{> private/alias_prefix}}{{/type:need_module_path?}}{{enum:name}}{{!
43-
}}{{/type:enum}}
24+
}}{{#pragma ignore-newlines}}
25+
{{#if type:void?}}
26+
None
27+
{{#else if type:primitive?}}
28+
{{> types/unadapted_builtin_pep484_type }}
29+
30+
{{#else if type:structured?}}
31+
{{#type:true_type}}
32+
{{#if type:need_module_path?}}{{type:module_mangle}}.{{/if}}
33+
{{#if (not (or type:need_module_path? struct:has_adapter?))}}{{> private/alias_prefix}}{{/if}}
34+
{{> structs/unadapted_name}}
35+
{{/type:true_type}}
36+
37+
{{#else if type:list?}}
38+
{{#type:true_type}}
39+
{{> types/sequence }}[{{#list:elem_type}}{{> types/pep484_type }}{{/list:elem_type}}]
40+
{{/type:true_type}}
41+
42+
{{#else if type:set?}}
43+
{{#type:true_type}}
44+
{{> types/set }}[{{#set:elem_type}}{{> types/pep484_type }}{{/set:elem_type}}]
45+
{{/type:true_type}}
46+
47+
{{#else if type:map?}}
48+
{{#type:true_type}}
49+
{{> types/mapping }}[
50+
{{#map:key_type}}{{> types/pep484_type }}{{/map:key_type}}, {{!}}
51+
{{#map:val_type}}{{> types/pep484_type }}{{/map:val_type}}
52+
]
53+
{{/type:true_type}}
54+
55+
{{#else if type:enum?}}
56+
{{#if type:need_module_path?}}
57+
{{type:module_mangle}}.
58+
{{#else}}
59+
{{> private/alias_prefix}}
60+
{{/if type:need_module_path?}}
61+
{{type:true_type.py_name}}
62+
63+
{{/if type:void?}}

0 commit comments

Comments
 (0)