Skip to content

Commit 411280d

Browse files
Filip Franceticfacebook-github-bot
authored andcommitted
Make reflection work on thrift-py3 container flatnames when auto-migrate is enabled
Summary: Reflection has to work on container flatnames in py3 because they're used recursively by the reflection library, so anything that expects to be able to recurse through a thrift python struct (eg. the py3 converter) will error out upon hitting a list. This makes it so that the special py3 flatnames correctly generate reflection information, and also changes the converter to check for thrift python list types correctly. Reviewed By: ahilger Differential Revision: D68822077 fbshipit-source-id: 3844d11f476a902f230e1809d4d82de70ad6a1dc
1 parent 5b30e10 commit 411280d

9 files changed

Lines changed: 688 additions & 7 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ want to include.
3636
{{#program:auto_migrate?}}
3737
from {{#program:py3Namespaces}}{{value}}.{{/program:py3Namespaces}}{{program:name}}.thrift_types import *
3838
{{#program:hasContainerTypes}}
39+
import importlib
3940
import thrift.python.types as _fbthrift_python_types
4041
import {{#program:py3Namespaces}}{{value}}.{{/program:py3Namespaces}}{{program:name}}.thrift_types as {{!
4142
}}_{{#program:py3Namespaces}}{{value}}_{{/program:py3Namespaces}}{{program:name}}_thrift_types
@@ -49,6 +50,12 @@ import {{#includeNamespace}}{{value}}.{{/includeNamespace}}thrift_types as _{{#i
4950
py3-container "flat name" name aliases for backwards-compatibility
5051
5152
}}
53+
{{#program:hasContainerTypes}}
54+
def get_types_reflection():
55+
return importlib.import_module(
56+
"{{#program:py3Namespaces}}{{value}}.{{/program:py3Namespaces}}{{program:name}}.types_reflection"
57+
)
58+
{{/program:hasContainerTypes}}
5259
{{#program:containerTypes}}
5360
{{#type:container?}}
5461
class {{type:flat_name}}__Meta(type):
@@ -80,6 +87,10 @@ class {{type:flat_name}}(_fbthrift_python_types.{{> types/container_base}}, meta
8087
**kwargs,
8188
)
8289
90+
@staticmethod
91+
def __get_reflection__():
92+
return get_types_reflection().get_reflection__{{type:flat_name}}()
93+
8394
{{/type:container?}}
8495
{{/program:containerTypes}}
8596
{{/program:auto_migrate?}}

third-party/thrift/src/thrift/compiler/generate/templates/py3/types_reflection.py.mustache

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ def get_reflection__{{struct:name}}() -> __StructSpec:
107107
{{/struct:py3_fields}}
108108
return spec
109109
{{/program:filtered_structs}}
110-
{{^program:auto_migrate?}}
111110
{{#program:containerTypes}}
112111
{{#type:list?}}
113112
def get_reflection__{{type:flat_name}}() -> __ListSpec :
@@ -138,4 +137,3 @@ def get_reflection__{{type:flat_name}}() -> __MapSpec:
138137
{{/type:map?}}
139138
140139
{{/program:containerTypes}}
141-
{{/program:auto_migrate?}}

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

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
# @generated
66
#
77
from module.thrift_types import *
8+
import importlib
89
import thrift.python.types as _fbthrift_python_types
910
import module.thrift_types as _module_thrift_types
11+
def get_types_reflection():
12+
return importlib.import_module(
13+
"module.types_reflection"
14+
)
1015
class List__i16__Meta(type):
1116
def _fbthrift_type_info(cls):
1217
return (
@@ -27,6 +32,10 @@ def __init__(self, *args, **kwargs):
2732
**kwargs,
2833
)
2934

35+
@staticmethod
36+
def __get_reflection__():
37+
return get_types_reflection().get_reflection__List__i16()
38+
3039
class List__i32__Meta(type):
3140
def _fbthrift_type_info(cls):
3241
return (
@@ -47,6 +56,10 @@ def __init__(self, *args, **kwargs):
4756
**kwargs,
4857
)
4958

59+
@staticmethod
60+
def __get_reflection__():
61+
return get_types_reflection().get_reflection__List__i32()
62+
5063
class List__i64__Meta(type):
5164
def _fbthrift_type_info(cls):
5265
return (
@@ -67,6 +80,10 @@ def __init__(self, *args, **kwargs):
6780
**kwargs,
6881
)
6982

83+
@staticmethod
84+
def __get_reflection__():
85+
return get_types_reflection().get_reflection__List__i64()
86+
7087
class List__string__Meta(type):
7188
def _fbthrift_type_info(cls):
7289
return (
@@ -87,6 +104,10 @@ def __init__(self, *args, **kwargs):
87104
**kwargs,
88105
)
89106

107+
@staticmethod
108+
def __get_reflection__():
109+
return get_types_reflection().get_reflection__List__string()
110+
90111
class List__SimpleStruct__Meta(type):
91112
def _fbthrift_type_info(cls):
92113
return (
@@ -107,6 +128,10 @@ def __init__(self, *args, **kwargs):
107128
**kwargs,
108129
)
109130

131+
@staticmethod
132+
def __get_reflection__():
133+
return get_types_reflection().get_reflection__List__SimpleStruct()
134+
110135
class Set__i32__Meta(type):
111136
def _fbthrift_type_info(cls):
112137
return (
@@ -127,6 +152,10 @@ def __init__(self, *args, **kwargs):
127152
**kwargs,
128153
)
129154

155+
@staticmethod
156+
def __get_reflection__():
157+
return get_types_reflection().get_reflection__Set__i32()
158+
130159
class Set__string__Meta(type):
131160
def _fbthrift_type_info(cls):
132161
return (
@@ -147,6 +176,10 @@ def __init__(self, *args, **kwargs):
147176
**kwargs,
148177
)
149178

179+
@staticmethod
180+
def __get_reflection__():
181+
return get_types_reflection().get_reflection__Set__string()
182+
150183
class Map__string_string__Meta(type):
151184
def _fbthrift_type_info(cls):
152185
return (
@@ -168,6 +201,10 @@ def __init__(self, *args, **kwargs):
168201
**kwargs,
169202
)
170203

204+
@staticmethod
205+
def __get_reflection__():
206+
return get_types_reflection().get_reflection__Map__string_string()
207+
171208
class Map__string_SimpleStruct__Meta(type):
172209
def _fbthrift_type_info(cls):
173210
return (
@@ -189,6 +226,10 @@ def __init__(self, *args, **kwargs):
189226
**kwargs,
190227
)
191228

229+
@staticmethod
230+
def __get_reflection__():
231+
return get_types_reflection().get_reflection__Map__string_SimpleStruct()
232+
192233
class Map__string_i16__Meta(type):
193234
def _fbthrift_type_info(cls):
194235
return (
@@ -210,6 +251,10 @@ def __init__(self, *args, **kwargs):
210251
**kwargs,
211252
)
212253

254+
@staticmethod
255+
def __get_reflection__():
256+
return get_types_reflection().get_reflection__Map__string_i16()
257+
213258
class List__List__i32__Meta(type):
214259
def _fbthrift_type_info(cls):
215260
return (
@@ -230,6 +275,10 @@ def __init__(self, *args, **kwargs):
230275
**kwargs,
231276
)
232277

278+
@staticmethod
279+
def __get_reflection__():
280+
return get_types_reflection().get_reflection__List__List__i32()
281+
233282
class Map__string_i32__Meta(type):
234283
def _fbthrift_type_info(cls):
235284
return (
@@ -251,6 +300,10 @@ def __init__(self, *args, **kwargs):
251300
**kwargs,
252301
)
253302

303+
@staticmethod
304+
def __get_reflection__():
305+
return get_types_reflection().get_reflection__Map__string_i32()
306+
254307
class Map__string_Map__string_i32__Meta(type):
255308
def _fbthrift_type_info(cls):
256309
return (
@@ -272,6 +325,10 @@ def __init__(self, *args, **kwargs):
272325
**kwargs,
273326
)
274327

328+
@staticmethod
329+
def __get_reflection__():
330+
return get_types_reflection().get_reflection__Map__string_Map__string_i32()
331+
275332
class List__Set__string__Meta(type):
276333
def _fbthrift_type_info(cls):
277334
return (
@@ -292,6 +349,10 @@ def __init__(self, *args, **kwargs):
292349
**kwargs,
293350
)
294351

352+
@staticmethod
353+
def __get_reflection__():
354+
return get_types_reflection().get_reflection__List__Set__string()
355+
295356
class Map__string_List__SimpleStruct__Meta(type):
296357
def _fbthrift_type_info(cls):
297358
return (
@@ -313,6 +374,10 @@ def __init__(self, *args, **kwargs):
313374
**kwargs,
314375
)
315376

377+
@staticmethod
378+
def __get_reflection__():
379+
return get_types_reflection().get_reflection__Map__string_List__SimpleStruct()
380+
316381
class List__List__string__Meta(type):
317382
def _fbthrift_type_info(cls):
318383
return (
@@ -333,6 +398,10 @@ def __init__(self, *args, **kwargs):
333398
**kwargs,
334399
)
335400

401+
@staticmethod
402+
def __get_reflection__():
403+
return get_types_reflection().get_reflection__List__List__string()
404+
336405
class List__Set__i32__Meta(type):
337406
def _fbthrift_type_info(cls):
338407
return (
@@ -353,6 +422,10 @@ def __init__(self, *args, **kwargs):
353422
**kwargs,
354423
)
355424

425+
@staticmethod
426+
def __get_reflection__():
427+
return get_types_reflection().get_reflection__List__Set__i32()
428+
356429
class List__Map__string_string__Meta(type):
357430
def _fbthrift_type_info(cls):
358431
return (
@@ -373,6 +446,10 @@ def __init__(self, *args, **kwargs):
373446
**kwargs,
374447
)
375448

449+
@staticmethod
450+
def __get_reflection__():
451+
return get_types_reflection().get_reflection__List__Map__string_string()
452+
376453
class List__binary__Meta(type):
377454
def _fbthrift_type_info(cls):
378455
return (
@@ -393,6 +470,10 @@ def __init__(self, *args, **kwargs):
393470
**kwargs,
394471
)
395472

473+
@staticmethod
474+
def __get_reflection__():
475+
return get_types_reflection().get_reflection__List__binary()
476+
396477
class Set__binary__Meta(type):
397478
def _fbthrift_type_info(cls):
398479
return (
@@ -413,6 +494,10 @@ def __init__(self, *args, **kwargs):
413494
**kwargs,
414495
)
415496

497+
@staticmethod
498+
def __get_reflection__():
499+
return get_types_reflection().get_reflection__Set__binary()
500+
416501
class List__AnEnum__Meta(type):
417502
def _fbthrift_type_info(cls):
418503
return (
@@ -433,6 +518,10 @@ def __init__(self, *args, **kwargs):
433518
**kwargs,
434519
)
435520

521+
@staticmethod
522+
def __get_reflection__():
523+
return get_types_reflection().get_reflection__List__AnEnum()
524+
436525
class _std_unordered_map__Map__i32_i32__Meta(type):
437526
def _fbthrift_type_info(cls):
438527
return (
@@ -454,6 +543,10 @@ def __init__(self, *args, **kwargs):
454543
**kwargs,
455544
)
456545

546+
@staticmethod
547+
def __get_reflection__():
548+
return get_types_reflection().get_reflection___std_unordered_map__Map__i32_i32()
549+
457550
class _MyType__List__i32__Meta(type):
458551
def _fbthrift_type_info(cls):
459552
return (
@@ -474,6 +567,10 @@ def __init__(self, *args, **kwargs):
474567
**kwargs,
475568
)
476569

570+
@staticmethod
571+
def __get_reflection__():
572+
return get_types_reflection().get_reflection___MyType__List__i32()
573+
477574
class _MyType__Set__i32__Meta(type):
478575
def _fbthrift_type_info(cls):
479576
return (
@@ -494,6 +591,10 @@ def __init__(self, *args, **kwargs):
494591
**kwargs,
495592
)
496593

594+
@staticmethod
595+
def __get_reflection__():
596+
return get_types_reflection().get_reflection___MyType__Set__i32()
597+
497598
class _MyType__Map__i32_i32__Meta(type):
498599
def _fbthrift_type_info(cls):
499600
return (
@@ -515,6 +616,10 @@ def __init__(self, *args, **kwargs):
515616
**kwargs,
516617
)
517618

619+
@staticmethod
620+
def __get_reflection__():
621+
return get_types_reflection().get_reflection___MyType__Map__i32_i32()
622+
518623
class _py3_simple_AdaptedList__List__i32__Meta(type):
519624
def _fbthrift_type_info(cls):
520625
return (
@@ -535,6 +640,10 @@ def __init__(self, *args, **kwargs):
535640
**kwargs,
536641
)
537642

643+
@staticmethod
644+
def __get_reflection__():
645+
return get_types_reflection().get_reflection___py3_simple_AdaptedList__List__i32()
646+
538647
class _py3_simple_AdaptedSet__Set__i32__Meta(type):
539648
def _fbthrift_type_info(cls):
540649
return (
@@ -555,6 +664,10 @@ def __init__(self, *args, **kwargs):
555664
**kwargs,
556665
)
557666

667+
@staticmethod
668+
def __get_reflection__():
669+
return get_types_reflection().get_reflection___py3_simple_AdaptedSet__Set__i32()
670+
558671
class _py3_simple_AdaptedMap__Map__i32_i32__Meta(type):
559672
def _fbthrift_type_info(cls):
560673
return (
@@ -576,6 +689,10 @@ def __init__(self, *args, **kwargs):
576689
**kwargs,
577690
)
578691

692+
@staticmethod
693+
def __get_reflection__():
694+
return get_types_reflection().get_reflection___py3_simple_AdaptedMap__Map__i32_i32()
695+
579696
class Map__i32_double__Meta(type):
580697
def _fbthrift_type_info(cls):
581698
return (
@@ -597,6 +714,10 @@ def __init__(self, *args, **kwargs):
597714
**kwargs,
598715
)
599716

717+
@staticmethod
718+
def __get_reflection__():
719+
return get_types_reflection().get_reflection__Map__i32_double()
720+
600721
class List__Map__i32_double__Meta(type):
601722
def _fbthrift_type_info(cls):
602723
return (
@@ -617,6 +738,10 @@ def __init__(self, *args, **kwargs):
617738
**kwargs,
618739
)
619740

741+
@staticmethod
742+
def __get_reflection__():
743+
return get_types_reflection().get_reflection__List__Map__i32_double()
744+
620745
class Map__AnEnumRenamed_i32__Meta(type):
621746
def _fbthrift_type_info(cls):
622747
return (
@@ -638,3 +763,7 @@ def __init__(self, *args, **kwargs):
638763
**kwargs,
639764
)
640765

766+
@staticmethod
767+
def __get_reflection__():
768+
return get_types_reflection().get_reflection__Map__AnEnumRenamed_i32()
769+

0 commit comments

Comments
 (0)