Skip to content

Commit 28b1474

Browse files
hchokshimeta-codesync[bot]
authored andcommitted
Migrate py3 types.py/types.pxd/types.h/services.pyi/clients.pyi to Whisker
Summary: Convert templates to Whisker: - types.py - types.pxd - types.h - services.pyi - clients.pyi Reviewed By: iahs Differential Revision: D95461819 fbshipit-source-id: a3961a76c55652c2c96cf972cfffdc9533c30f0c
1 parent cf12b97 commit 28b1474

File tree

7 files changed

+207
-211
lines changed

7 files changed

+207
-211
lines changed

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

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,20 @@ class t_mstch_py3_generator : public t_mstch_generator {
11231123
def.property("container_types", [&](const t_program& self) {
11241124
return to_type_array(context_->container_types(self), proto);
11251125
});
1126+
def.property("stream_types", [&](const t_program& self) {
1127+
std::vector<const t_type*> types;
1128+
if (!has_compiler_option("no_stream")) {
1129+
for (const auto& [_, type] : context_->stream_types(self)) {
1130+
types.push_back(type);
1131+
}
1132+
}
1133+
return to_type_array(types, proto);
1134+
});
1135+
def.property("response_and_stream_functions", [&](const t_program& self) {
1136+
return to_array(
1137+
context_->response_and_stream_functions(self),
1138+
proto.of<t_function>());
1139+
});
11261140
def.property("custom_templates", [&](const t_program& self) {
11271141
return to_type_array(context_->custom_templates(self), proto);
11281142
});
@@ -1517,7 +1531,7 @@ void t_mstch_py3_generator::generate_whisker_file(
15171531
void t_mstch_py3_generator::generate_types() {
15181532
// {template_name, use_whisker}
15191533
std::vector<std::pair<std::string, bool>> autoMigrateFilesWithTypeContext{
1520-
{"types.py", false},
1534+
{"types.py", true},
15211535
{"types_auto_FBTHRIFT_ONLY_DO_NOT_USE.py", false},
15221536
{"types_auto_migrated.py", true},
15231537
};
@@ -1534,7 +1548,7 @@ void t_mstch_py3_generator::generate_types() {
15341548

15351549
std::vector<std::pair<std::string, bool>> cythonFilesWithTypeContext{
15361550
{"types.pyx", false},
1537-
{"types.pxd", false},
1551+
{"types.pxd", true},
15381552
{"types.pyi", false},
15391553
};
15401554

@@ -1553,8 +1567,8 @@ void t_mstch_py3_generator::generate_types() {
15531567
{"types_reflection.py", true},
15541568
};
15551569

1556-
std::vector<std::pair<std::string, bool>> cppFilesWithTypeContext{
1557-
{"types.h", false},
1570+
std::vector<std::string> cppFilesWithTypeContext{
1571+
"types.h",
15581572
};
15591573

15601574
std::vector<std::string> cppFilesWithNoTypeContext{
@@ -1586,7 +1600,9 @@ void t_mstch_py3_generator::generate_types() {
15861600
}
15871601
generate_mixed_template_list(
15881602
cythonFilesWithTypeContext, FileType::TypesFile, generateRootPath_);
1589-
generate_mixed_template_list(cppFilesWithTypeContext, FileType::TypesFile);
1603+
for (const auto& file : cppFilesWithTypeContext) {
1604+
generate_whisker_file(file, FileType::TypesFile);
1605+
}
15901606
generate_mixed_template_list(
15911607
cythonFilesNoTypeContext, FileType::NotTypesFile, generateRootPath_);
15921608
for (const auto& file : cppFilesWithNoTypeContext) {
@@ -1612,16 +1628,16 @@ void t_mstch_py3_generator::generate_services() {
16121628
std::vector<std::pair<std::string, bool>> normalCythonFiles{
16131629
{"clients.pxd", true},
16141630
{"clients.pyx", false},
1615-
{"clients.pyi", false},
1631+
{"clients.pyi", true},
16161632
{"services.pxd", true},
16171633
{"services.pyx", false},
1618-
{"services.pyi", false},
1634+
{"services.pyi", true},
16191635
};
16201636

1621-
std::vector<std::pair<std::string, bool>> cythonFiles{
1622-
{"clients_wrapper.pxd", true},
1623-
{"services_wrapper.pxd", true},
1624-
{"services_interface.pxd", true},
1637+
std::vector<std::string> cythonFiles{
1638+
"clients_wrapper.pxd",
1639+
"services_wrapper.pxd",
1640+
"services_interface.pxd",
16251641
};
16261642

16271643
std::vector<std::string> cppFiles{
@@ -1644,8 +1660,9 @@ void t_mstch_py3_generator::generate_services() {
16441660
for (const auto& file : cppFiles) {
16451661
generate_whisker_file(file, FileType::NotTypesFile);
16461662
}
1647-
generate_mixed_template_list(
1648-
cythonFiles, FileType::NotTypesFile, generateRootPath_);
1663+
for (const auto& file : cythonFiles) {
1664+
generate_whisker_file(file, FileType::NotTypesFile, generateRootPath_);
1665+
}
16491666
}
16501667

16511668
} // namespace

third-party/thrift/src/thrift/compiler/generate/templates/py3/clients.pyi.mustache

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -30,68 +30,62 @@ import thrift.py3.common
3030
import typing as _typing
3131
from types import TracebackType
3232

33-
import {{#program:py3Namespaces}}{{.}}.{{/program:py3Namespaces}}{{program:name}}.types as _{{#program:py3Namespaces}}{{.}}_{{/program:py3Namespaces}}{{program:name}}_types
34-
{{#each program:includeNamespaces as |include|}}
33+
import {{#each program.py3Namespaces as |ns|}}{{ns}}.{{/each}}{{program.name}}.types as _{{#each program.py3Namespaces as |ns|}}{{ns}}_{{/each}}{{program.name}}_types
34+
{{#each program.includeNamespaces as |include|}}
3535
{{#if include.has_types?}}
3636
import {{include.modulePathPeriodSeparated}}.types as _{{include.modulePathUnderscoreSeparated}}_types
3737
{{/if include.has_types?}}
3838
{{#if include.import_services?}}
3939
import {{include.modulePathPeriodSeparated}}.clients as _{{include.modulePathUnderscoreSeparated}}_clients
4040
{{/if include.import_services?}}
4141
{{/each}}
42-
{{#program:services}}
42+
{{#each program.services as |service|}}
4343

4444

45-
_{{service:name}}T = _typing.TypeVar('_{{service:name}}T', bound='{{service:name}}')
45+
_{{service.name}}T = _typing.TypeVar('_{{service.name}}T', bound='{{service.name}}')
4646

4747

48-
class {{service:name}}({{#service:extends}}{{#service:externalProgram?}}{{!
49-
}}_{{#each service:program.py3Namespaces as |ns|}}{{ns}}_{{/each}}{{!
50-
}}{{service:program.name}}_clients.{{/service:externalProgram?}}{{service:name}}{{!
51-
}}{{/service:extends}}{{^service:extends?}}thrift.py3.client.Client{{/service:extends?}}):
48+
class {{service.name}}({{#let parent = service.extends}}{{#if (object.notnull? parent)}}{{#if parent.externalProgram?}}{{!
49+
}}_{{#each parent.program.py3Namespaces as |ns|}}{{ns}}_{{/each}}{{!
50+
}}{{parent.program.name}}_clients.{{/if parent.externalProgram?}}{{parent.name}}{{!
51+
}}{{#else}}thrift.py3.client.Client{{/if (object.notnull? parent)}}):
5252

53-
{{#service:supportedFunctions}}
54-
async def {{function:name}}(
55-
self{{#function:args}},
56-
{{field:py_name}}: {{#partial pep484.type_name type=field:self.type}}{{/function:args}},
53+
{{#each service.supportedFunctions as |function|}}
54+
async def {{function.name}}(
55+
self{{#each function.params.fields as |field|}},
56+
{{field.py_name}}: {{#partial pep484.type_name type=field.type}}{{/each}},
5757
*,
5858
rpc_options: _typing.Optional[thrift.py3.common.RpcOptions]=None
59-
) -> {{#if function:stream?}}{{#partial clients.stream_response_class_name function=function:self}}{{!
60-
}}{{#else}}{{#partial pep484.type_name type=function:self.return_type}}{{/if function:stream?}}: ...
61-
62-
{{/service:supportedFunctions}}
63-
{{#let parent_service_name=service:name}}
64-
{{#service:supportedInteractions}}
65-
def create{{service:name}}(self) -> {{parent_service_name}}_{{service:name}}: ...
66-
def async_create{{service:name}}(self) -> {{parent_service_name}}_{{service:name}}: ...
67-
{{/service:supportedInteractions}}
68-
{{^service:supportedInteractions}}
69-
{{^service:supportedFunctions}}
59+
) -> {{#if function.stream?}}{{#partial clients.stream_response_class_name function=function}}{{!
60+
}}{{#else}}{{#partial pep484.type_name type=function.return_type}}{{/if function.stream?}}: ...
61+
62+
{{/each}}
63+
{{#each service.supportedInteractions as |interaction|}}
64+
def create{{interaction.name}}(self) -> {{service.name}}_{{interaction.name}}: ...
65+
def async_create{{interaction.name}}(self) -> {{service.name}}_{{interaction.name}}: ...
66+
{{/each}}
67+
{{#if (and (array.empty? service.supportedInteractions) (array.empty? service.supportedFunctions))}}
7068
pass
71-
{{/service:supportedFunctions}}
72-
{{/service:supportedInteractions}}
73-
{{#service:supportedInteractions}}
69+
{{/if (and (array.empty? service.supportedInteractions) (array.empty? service.supportedFunctions))}}
70+
{{#each service.supportedInteractions as |interaction|}}
7471

75-
_{{parent_service_name}}_{{service:name}} = _typing.TypeVar('_{{parent_service_name}}_{{service:name}}', bound='{{parent_service_name}}_{{service:name}}')
72+
_{{service.name}}_{{interaction.name}} = _typing.TypeVar('_{{service.name}}_{{interaction.name}}', bound='{{service.name}}_{{interaction.name}}')
7673

7774

78-
class {{parent_service_name}}_{{service:name}}({{#service:extends}}{{#service:externalProgram?}}{{!
79-
}}_{{#each service:program.py3Namespaces as |ns|}}{{ns}}_{{/each}}{{!
80-
}}{{service:program.name}}_clients.{{/service:externalProgram?}}{{service:name}}{{!
81-
}}{{/service:extends}}{{^service:extends?}}thrift.py3.client.Client{{/service:extends?}}):
75+
class {{service.name}}_{{interaction.name}}(thrift.py3.client.Client):
8276

83-
{{#service:supportedFunctions}}
84-
async def {{function:name}}(
85-
self{{#function:args}},
86-
{{field:py_name}}: {{#partial pep484.type_name type=field:self.type}}{{/function:args}},
77+
{{#each interaction.supportedFunctions as |function|}}
78+
async def {{function.name}}(
79+
self{{#each function.params.fields as |field|}},
80+
{{field.py_name}}: {{#partial pep484.type_name type=field.type}}{{/each}},
8781
*,
8882
rpc_options: _typing.Optional[thrift.py3.common.RpcOptions]=None
89-
) -> {{#if function:stream?}}{{#partial clients.stream_response_class_name function=function:self}}{{!
90-
}}{{#else}}{{#partial pep484.type_name type=function:self.return_type}}{{/if function:stream?}}: ...
83+
) -> {{#if function.stream?}}{{#partial clients.stream_response_class_name function=function}}{{!
84+
}}{{#else}}{{#partial pep484.type_name type=function.return_type}}{{/if function.stream?}}: ...
9185

92-
{{/service:supportedFunctions}}
93-
{{^service:supportedFunctions}}
86+
{{/each}}
87+
{{#if (array.empty? interaction.supportedFunctions)}}
9488
pass
95-
{{/service:supportedFunctions}}
96-
{{/service:supportedInteractions}}
97-
{{/program:services}}
89+
{{/if (array.empty? interaction.supportedFunctions)}}
90+
{{/each}}
91+
{{/each}}

0 commit comments

Comments
 (0)