Skip to content

Commit 238284a

Browse files
hchokshimeta-codesync[bot]
authored andcommitted
Consolidate py3 make_instance template for list/set
Summary: Reduce duplication and avoid out-of-sync issues between list/set. Reviewed By: iahs Differential Revision: D96017010 fbshipit-source-id: d910ce27f3c99ff41a6197f325f1bee1a5f15b74
1 parent 206f87b commit 238284a

14 files changed

Lines changed: 60 additions & 78 deletions

File tree

  • third-party/thrift/src/thrift/compiler
    • generate/templates/py3/common
    • test/fixtures
      • constants/out/py3/gen-py3/module
      • includes/out/py3_matching_struct_names/gen-py3/matching_struct_names
      • mcpp2-compare/out/py3/gen-py3/module
      • params/out/py3/gen-py3/module
      • py3/out
      • templated-deserialize/out/py3/gen-py3/module
      • transitive-deps/out/py3_a/gen-py3/a
      • types/out
        • py3_inplace/gen-py3/apache/thrift/fixtures/types/module
        • py3/gen-py3/apache/thrift/fixtures/types

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

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -344,29 +344,7 @@ between types.pyx (default mode) and converters.pyx (auto-migrate mode)
344344
{{/if type.containerOfString?}}
345345
for item in items:
346346
{{/if (not type.map?)}}
347-
{{#if type.list?}}
348-
{{#let elem_type=type.true_type.elem_type}}
349-
{{#if elem_type.container?}}
350-
if item is None:
351-
raise TypeError("None is not of the type {{#partial pep484.type_name type=elem_type}}")
352-
if not isinstance(item, {{#partial python_type type=elem_type}}):
353-
item = {{#partial python_type type=elem_type}}(item)
354-
{{#else}}
355-
if not isinstance(item, {{#partial python_is_instance_type type=elem_type}}):
356-
raise TypeError(f"{item!r} is not of type {{#partial pep484.type_name type=elem_type}}")
357-
{{#if elem_type.integer?}}
358-
{{! inject cython int overflow checks }}
359-
item = <{{#partial cython_python_type type=elem_type}}> item
360-
{{/if elem_type.integer?}}
361-
{{/if elem_type.container?}}
362-
c_inst.push_back({{#partial cython_python_to_cpp_item type=elem_type}})
363-
{{#else if type.set?}}
364-
{{!
365-
TODO(hchok): This is very similar to the list branch.W e can likely consolidate a bunch of this,
366-
but it's preserved as-is while converting the template to a partial, so we can rely on codegen
367-
output parity as a correctness signal. The consolidation will be split into its own change,
368-
alongside corresponding fixture changes.
369-
}}
347+
{{#if (or type.list? type.set?)}}
370348
{{#let elem_type=type.true_type.elem_type}}
371349
{{#if elem_type.container?}}
372350
if item is None:
@@ -378,13 +356,17 @@ between types.pyx (default mode) and converters.pyx (auto-migrate mode)
378356
raise TypeError(f"{item!r} is not of type {{#partial pep484.type_name type=elem_type}}")
379357
{{#if elem_type.integer?}}
380358
{{! inject cython int overflow checks }}
381-
c_item = <{{#partial cython_python_type type=elem_type}}> item
359+
{{#if type.set?}}c_{{/if}}item = <{{#partial cython_python_type type=elem_type}}> item
382360
{{/if elem_type.integer?}}
383361
{{/if elem_type.container?}}
384-
{{#if (not elem_type.integer?)}}
362+
{{#if type.list?}}
363+
c_inst.push_back({{#partial cython_python_to_cpp_item type=elem_type}})
364+
{{#else}}{{! Set }}
365+
{{#if (not elem_type.integer?)}}
385366
c_item = {{#partial cython_python_to_cpp_item type=elem_type}}
386-
{{/if (not elem_type.integer?)}}
367+
{{/if (not elem_type.integer?)}}
387368
c_inst.insert(c_item)
369+
{{/if type.list?}}
388370
{{#else if type.map?}}
389371
for key, item in items.items():
390372
{{#let key_type=type.true_type.key_type}}
@@ -420,7 +402,7 @@ between types.pyx (default mode) and converters.pyx (auto-migrate mode)
420402
{{/if val_type.container?}}
421403

422404
c_inst[c_key] = {{#partial cython_python_to_cpp_item type=val_type}}
423-
{{/if type.list?}}
405+
{{/if (or type.list? type.set?)}}
424406
return cmove(c_inst)
425407
{{/let partial}}
426408

third-party/thrift/src/thrift/compiler/test/fixtures/constants/out/py3/gen-py3/module/types.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1414,7 +1414,7 @@ cdef vector[cmap[string,cint32_t]] List__Map__string_i32__make_instance(object i
14141414
return cmove(c_inst)
14151415
for item in items:
14161416
if item is None:
1417-
raise TypeError("None is not of the type _typing.Mapping[str, int]")
1417+
raise TypeError("None is not of type _typing.Mapping[str, int]")
14181418
if not isinstance(item, Map__string_i32):
14191419
item = Map__string_i32(item)
14201420
c_inst.push_back(Map__string_i32__make_instance(item))

third-party/thrift/src/thrift/compiler/test/fixtures/includes/out/py3_matching_struct_names/gen-py3/matching_struct_names/types.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ cdef vector[vector[_matching_struct_names_cbindings.cMyStruct]] List__List__MySt
350350
return cmove(c_inst)
351351
for item in items:
352352
if item is None:
353-
raise TypeError("None is not of the type _typing.Sequence[MyStruct]")
353+
raise TypeError("None is not of type _typing.Sequence[MyStruct]")
354354
if not isinstance(item, List__MyStruct):
355355
item = List__MyStruct(item)
356356
c_inst.push_back(List__MyStruct__make_instance(item))
@@ -386,7 +386,7 @@ cdef vector[vector[_module_cbindings.cMyStruct]] List__List__module_MyStruct__ma
386386
return cmove(c_inst)
387387
for item in items:
388388
if item is None:
389-
raise TypeError("None is not of the type _typing.Sequence[_module_types.MyStruct]")
389+
raise TypeError("None is not of type _typing.Sequence[_module_types.MyStruct]")
390390
if not isinstance(item, List__module_MyStruct):
391391
item = List__module_MyStruct(item)
392392
c_inst.push_back(List__module_MyStruct__make_instance(item))

third-party/thrift/src/thrift/compiler/test/fixtures/mcpp2-compare/out/py3/gen-py3/module/types.pyx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3686,7 +3686,7 @@ cdef vector[cmap[_module_cbindings.cEmpty,_module_cbindings.cMyStruct]] List__Ma
36863686
return cmove(c_inst)
36873687
for item in items:
36883688
if item is None:
3689-
raise TypeError("None is not of the type _typing.Mapping[Empty, MyStruct]")
3689+
raise TypeError("None is not of type _typing.Mapping[Empty, MyStruct]")
36903690
if not isinstance(item, Map__Empty_MyStruct):
36913691
item = Map__Empty_MyStruct(item)
36923692
c_inst.push_back(Map__Empty_MyStruct__make_instance(item))
@@ -3705,7 +3705,7 @@ cdef vector[vector[cmap[_module_cbindings.cEmpty,_module_cbindings.cMyStruct]]]
37053705
return cmove(c_inst)
37063706
for item in items:
37073707
if item is None:
3708-
raise TypeError("None is not of the type _typing.Sequence[_typing.Mapping[Empty, MyStruct]]")
3708+
raise TypeError("None is not of type _typing.Sequence[_typing.Mapping[Empty, MyStruct]]")
37093709
if not isinstance(item, List__Map__Empty_MyStruct):
37103710
item = List__Map__Empty_MyStruct(item)
37113711
c_inst.push_back(List__Map__Empty_MyStruct__make_instance(item))
@@ -3724,7 +3724,7 @@ cdef vector[vector[vector[cmap[_module_cbindings.cEmpty,_module_cbindings.cMyStr
37243724
return cmove(c_inst)
37253725
for item in items:
37263726
if item is None:
3727-
raise TypeError("None is not of the type _typing.Sequence[_typing.Sequence[_typing.Mapping[Empty, MyStruct]]]")
3727+
raise TypeError("None is not of type _typing.Sequence[_typing.Sequence[_typing.Mapping[Empty, MyStruct]]]")
37283728
if not isinstance(item, List__List__Map__Empty_MyStruct):
37293729
item = List__List__Map__Empty_MyStruct(item)
37303730
c_inst.push_back(List__List__Map__Empty_MyStruct__make_instance(item))
@@ -3925,7 +3925,7 @@ cdef vector[vector[cint32_t]] List__List__i32__make_instance(object items) excep
39253925
return cmove(c_inst)
39263926
for item in items:
39273927
if item is None:
3928-
raise TypeError("None is not of the type _typing.Sequence[int]")
3928+
raise TypeError("None is not of type _typing.Sequence[int]")
39293929
if not isinstance(item, List__i32):
39303930
item = List__i32(item)
39313931
c_inst.push_back(List__i32__make_instance(item))
@@ -3944,7 +3944,7 @@ cdef vector[vector[vector[cint32_t]]] List__List__List__i32__make_instance(objec
39443944
return cmove(c_inst)
39453945
for item in items:
39463946
if item is None:
3947-
raise TypeError("None is not of the type _typing.Sequence[_typing.Sequence[int]]")
3947+
raise TypeError("None is not of type _typing.Sequence[_typing.Sequence[int]]")
39483948
if not isinstance(item, List__List__i32):
39493949
item = List__List__i32(item)
39503950
c_inst.push_back(List__List__i32__make_instance(item))
@@ -3963,7 +3963,7 @@ cdef vector[vector[vector[vector[cint32_t]]]] List__List__List__List__i32__make_
39633963
return cmove(c_inst)
39643964
for item in items:
39653965
if item is None:
3966-
raise TypeError("None is not of the type _typing.Sequence[_typing.Sequence[_typing.Sequence[int]]]")
3966+
raise TypeError("None is not of type _typing.Sequence[_typing.Sequence[_typing.Sequence[int]]]")
39673967
if not isinstance(item, List__List__List__i32):
39683968
item = List__List__List__i32(item)
39693969
c_inst.push_back(List__List__List__i32__make_instance(item))
@@ -4028,7 +4028,7 @@ cdef vector[cset[string]] List__Set__string__make_instance(object items) except
40284028
return cmove(c_inst)
40294029
for item in items:
40304030
if item is None:
4031-
raise TypeError("None is not of the type _typing.AbstractSet[str]")
4031+
raise TypeError("None is not of type _typing.AbstractSet[str]")
40324032
if not isinstance(item, Set__string):
40334033
item = Set__string(item)
40344034
c_inst.push_back(Set__string__make_instance(item))
@@ -4454,7 +4454,7 @@ cdef vector[cset[_module_cbindings.cSimpleUnion]] List__Set__SimpleUnion__make_i
44544454
return cmove(c_inst)
44554455
for item in items:
44564456
if item is None:
4457-
raise TypeError("None is not of the type _typing.AbstractSet[SimpleUnion]")
4457+
raise TypeError("None is not of type _typing.AbstractSet[SimpleUnion]")
44584458
if not isinstance(item, Set__SimpleUnion):
44594459
item = Set__SimpleUnion(item)
44604460
c_inst.push_back(Set__SimpleUnion__make_instance(item))
@@ -4552,7 +4552,7 @@ cdef vector[cset[cint32_t]] List__Set__i32__make_instance(object items) except *
45524552
return cmove(c_inst)
45534553
for item in items:
45544554
if item is None:
4555-
raise TypeError("None is not of the type _typing.AbstractSet[int]")
4555+
raise TypeError("None is not of type _typing.AbstractSet[int]")
45564556
if not isinstance(item, Set__i32):
45574557
item = Set__i32(item)
45584558
c_inst.push_back(Set__i32__make_instance(item))
@@ -4740,7 +4740,7 @@ cdef vector[cmap[cint16_t,string]] List__Map__i16_string__make_instance(object i
47404740
return cmove(c_inst)
47414741
for item in items:
47424742
if item is None:
4743-
raise TypeError("None is not of the type _typing.Mapping[int, str]")
4743+
raise TypeError("None is not of type _typing.Mapping[int, str]")
47444744
if not isinstance(item, Map__i16_string):
47454745
item = Map__i16_string(item)
47464746
c_inst.push_back(Map__i16_string__make_instance(item))
@@ -4801,7 +4801,7 @@ cdef vector[cmap[string,cint32_t]] List__Map__string_i32__make_instance(object i
48014801
return cmove(c_inst)
48024802
for item in items:
48034803
if item is None:
4804-
raise TypeError("None is not of the type _typing.Mapping[str, int]")
4804+
raise TypeError("None is not of type _typing.Mapping[str, int]")
48054805
if not isinstance(item, Map__string_i32):
48064806
item = Map__string_i32(item)
48074807
c_inst.push_back(Map__string_i32__make_instance(item))

third-party/thrift/src/thrift/compiler/test/fixtures/params/out/py3/gen-py3/module/types.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ cdef vector[cmap[cint32_t,cint32_t]] List__Map__i32_i32__make_instance(object it
198198
return cmove(c_inst)
199199
for item in items:
200200
if item is None:
201-
raise TypeError("None is not of the type _typing.Mapping[int, int]")
201+
raise TypeError("None is not of type _typing.Mapping[int, int]")
202202
if not isinstance(item, Map__i32_i32):
203203
item = Map__i32_i32(item)
204204
c_inst.push_back(Map__i32_i32__make_instance(item))
@@ -217,7 +217,7 @@ cdef vector[cset[cint32_t]] List__Set__i32__make_instance(object items) except *
217217
return cmove(c_inst)
218218
for item in items:
219219
if item is None:
220-
raise TypeError("None is not of the type _typing.AbstractSet[int]")
220+
raise TypeError("None is not of type _typing.AbstractSet[int]")
221221
if not isinstance(item, Set__i32):
222222
item = Set__i32(item)
223223
c_inst.push_back(Set__i32__make_instance(item))
@@ -263,7 +263,7 @@ cdef vector[cmap[cint32_t,cmap[cint32_t,cset[cint32_t]]]] List__Map__i32_Map__i3
263263
return cmove(c_inst)
264264
for item in items:
265265
if item is None:
266-
raise TypeError("None is not of the type _typing.Mapping[int, _typing.Mapping[int, _typing.AbstractSet[int]]]")
266+
raise TypeError("None is not of type _typing.Mapping[int, _typing.Mapping[int, _typing.AbstractSet[int]]]")
267267
if not isinstance(item, Map__i32_Map__i32_Set__i32):
268268
item = Map__i32_Map__i32_Set__i32(item)
269269
c_inst.push_back(Map__i32_Map__i32_Set__i32__make_instance(item))
@@ -282,7 +282,7 @@ cdef vector[vector[cmap[cint32_t,cmap[cint32_t,cset[cint32_t]]]]] List__List__Ma
282282
return cmove(c_inst)
283283
for item in items:
284284
if item is None:
285-
raise TypeError("None is not of the type _typing.Sequence[_typing.Mapping[int, _typing.Mapping[int, _typing.AbstractSet[int]]]]")
285+
raise TypeError("None is not of type _typing.Sequence[_typing.Mapping[int, _typing.Mapping[int, _typing.AbstractSet[int]]]]")
286286
if not isinstance(item, List__Map__i32_Map__i32_Set__i32):
287287
item = List__Map__i32_Map__i32_Set__i32(item)
288288
c_inst.push_back(List__Map__i32_Map__i32_Set__i32__make_instance(item))

third-party/thrift/src/thrift/compiler/test/fixtures/py3/out/py3/gen-py3/module/types.pyx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,7 +1992,7 @@ cdef vector[vector[cint32_t]] List__List__i32__make_instance(object items) excep
19921992
return cmove(c_inst)
19931993
for item in items:
19941994
if item is None:
1995-
raise TypeError("None is not of the type _typing.Sequence[int]")
1995+
raise TypeError("None is not of type _typing.Sequence[int]")
19961996
if not isinstance(item, List__i32):
19971997
item = List__i32(item)
19981998
c_inst.push_back(List__i32__make_instance(item))
@@ -2064,7 +2064,7 @@ cdef vector[cset[string]] List__Set__string__make_instance(object items) except
20642064
return cmove(c_inst)
20652065
for item in items:
20662066
if item is None:
2067-
raise TypeError("None is not of the type _typing.AbstractSet[str]")
2067+
raise TypeError("None is not of type _typing.AbstractSet[str]")
20682068
if not isinstance(item, Set__string):
20692069
item = Set__string(item)
20702070
c_inst.push_back(Set__string__make_instance(item))
@@ -2110,7 +2110,7 @@ cdef vector[vector[string]] List__List__string__make_instance(object items) exce
21102110
return cmove(c_inst)
21112111
for item in items:
21122112
if item is None:
2113-
raise TypeError("None is not of the type _typing.Sequence[str]")
2113+
raise TypeError("None is not of type _typing.Sequence[str]")
21142114
if not isinstance(item, List__string):
21152115
item = List__string(item)
21162116
c_inst.push_back(List__string__make_instance(item))
@@ -2129,7 +2129,7 @@ cdef vector[cset[cint32_t]] List__Set__i32__make_instance(object items) except *
21292129
return cmove(c_inst)
21302130
for item in items:
21312131
if item is None:
2132-
raise TypeError("None is not of the type _typing.AbstractSet[int]")
2132+
raise TypeError("None is not of type _typing.AbstractSet[int]")
21332133
if not isinstance(item, Set__i32):
21342134
item = Set__i32(item)
21352135
c_inst.push_back(Set__i32__make_instance(item))
@@ -2148,7 +2148,7 @@ cdef vector[cmap[string,string]] List__Map__string_string__make_instance(object
21482148
return cmove(c_inst)
21492149
for item in items:
21502150
if item is None:
2151-
raise TypeError("None is not of the type _typing.Mapping[str, str]")
2151+
raise TypeError("None is not of type _typing.Mapping[str, str]")
21522152
if not isinstance(item, Map__string_string):
21532153
item = Map__string_string(item)
21542154
c_inst.push_back(Map__string_string__make_instance(item))
@@ -2451,7 +2451,7 @@ cdef vector[cmap[cint32_t,double]] List__Map__i32_double__make_instance(object i
24512451
return cmove(c_inst)
24522452
for item in items:
24532453
if item is None:
2454-
raise TypeError("None is not of the type _typing.Mapping[int, float]")
2454+
raise TypeError("None is not of type _typing.Mapping[int, float]")
24552455
if not isinstance(item, Map__i32_double):
24562456
item = Map__i32_double(item)
24572457
c_inst.push_back(Map__i32_double__make_instance(item))

third-party/thrift/src/thrift/compiler/test/fixtures/py3/out/py3_auto_migrate_legacy_container_converters/gen-py3/module/converter.pyx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ cdef vector[vector[cint32_t]] List__List__i32__make_instance(object items) excep
241241
return cmove(c_inst)
242242
for item in items:
243243
if item is None:
244-
raise TypeError("None is not of the type _typing.Sequence[int]")
244+
raise TypeError("None is not of type _typing.Sequence[int]")
245245
if not isinstance(item, _module_types.List__i32):
246246
item = _module_types.List__i32(item)
247247
c_inst.push_back(List__i32__make_instance(item))
@@ -286,7 +286,7 @@ cdef vector[cset[string]] List__Set__string__make_instance(object items) except
286286
return cmove(c_inst)
287287
for item in items:
288288
if item is None:
289-
raise TypeError("None is not of the type _typing.AbstractSet[str]")
289+
raise TypeError("None is not of type _typing.AbstractSet[str]")
290290
if not isinstance(item, _module_types.Set__string):
291291
item = _module_types.Set__string(item)
292292
c_inst.push_back(Set__string__make_instance(item))
@@ -315,7 +315,7 @@ cdef vector[vector[string]] List__List__string__make_instance(object items) exce
315315
return cmove(c_inst)
316316
for item in items:
317317
if item is None:
318-
raise TypeError("None is not of the type _typing.Sequence[str]")
318+
raise TypeError("None is not of type _typing.Sequence[str]")
319319
if not isinstance(item, _module_types.List__string):
320320
item = _module_types.List__string(item)
321321
c_inst.push_back(List__string__make_instance(item))
@@ -327,7 +327,7 @@ cdef vector[cset[cint32_t]] List__Set__i32__make_instance(object items) except *
327327
return cmove(c_inst)
328328
for item in items:
329329
if item is None:
330-
raise TypeError("None is not of the type _typing.AbstractSet[int]")
330+
raise TypeError("None is not of type _typing.AbstractSet[int]")
331331
if not isinstance(item, _module_types.Set__i32):
332332
item = _module_types.Set__i32(item)
333333
c_inst.push_back(Set__i32__make_instance(item))
@@ -339,7 +339,7 @@ cdef vector[cmap[string,string]] List__Map__string_string__make_instance(object
339339
return cmove(c_inst)
340340
for item in items:
341341
if item is None:
342-
raise TypeError("None is not of the type _typing.Mapping[str, str]")
342+
raise TypeError("None is not of type _typing.Mapping[str, str]")
343343
if not isinstance(item, _module_types.Map__string_string):
344344
item = _module_types.Map__string_string(item)
345345
c_inst.push_back(Map__string_string__make_instance(item))
@@ -523,7 +523,7 @@ cdef vector[cmap[cint32_t,double]] List__Map__i32_double__make_instance(object i
523523
return cmove(c_inst)
524524
for item in items:
525525
if item is None:
526-
raise TypeError("None is not of the type _typing.Mapping[int, float]")
526+
raise TypeError("None is not of type _typing.Mapping[int, float]")
527527
if not isinstance(item, _module_types.Map__i32_double):
528528
item = _module_types.Map__i32_double(item)
529529
c_inst.push_back(Map__i32_double__make_instance(item))

0 commit comments

Comments
 (0)