From 4b99f31d2a3a6adf3b09bdb9786c57ec3971842c Mon Sep 17 00:00:00 2001 From: Yuxuan 'fishy' Wang Date: Tue, 17 Sep 2024 09:53:02 -0700 Subject: [PATCH] Fix python compiler generated all_structs with type_hints Currently with type_hints enabled, we do not explicitly declare the type of generated all_structs. It's meant to be a list of different types, but that will cause problems, as the generated code is usually like this: all_structs = [] ... all_structs.append(Type1) ... all_structs.append(Type2) ... The first append infers the type of all_struct to be list[Type1], so mypyc would start complaining from the 2nd append as the type is unexpected. Explicitly declare all_struct to be list[typing.Any] instead, which fixes the issue. --- compiler/cpp/src/thrift/generate/t_py_generator.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/compiler/cpp/src/thrift/generate/t_py_generator.cc b/compiler/cpp/src/thrift/generate/t_py_generator.cc index 893d6355cec..dfb8e6ed22d 100644 --- a/compiler/cpp/src/thrift/generate/t_py_generator.cc +++ b/compiler/cpp/src/thrift/generate/t_py_generator.cc @@ -437,7 +437,11 @@ void t_py_generator::init_generator() { << "from thrift.transport import TTransport" << '\n' << import_dynbase_; - f_types_ << "all_structs = []" << '\n'; + if (gen_type_hints_) { + f_types_ << "all_structs: list[typing.Any] = []" << '\n'; + } else { + f_types_ << "all_structs = []" << '\n'; + } f_consts_ << py_autogen_comment() << '\n' <<