Skip to content

Commit

Permalink
Fix python compiler generated all_structs with type_hints
Browse files Browse the repository at this point in the history
Currently with type_hints enabled, we does 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
mypy 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.
  • Loading branch information
fishy committed Sep 17, 2024
1 parent 1a31d90 commit be1ddb6
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/cpp/src/thrift/generate/t_py_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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' <<
Expand Down

0 comments on commit be1ddb6

Please sign in to comment.