Skip to content

Commit

Permalink
Do not generate the property for invariant container
Browse files Browse the repository at this point in the history
Summary:
Suppress generating properties for invariant container types.
This diff extends the logic introduced in D67674205

Reviewed By: prakashgayasen, createdbysk

Differential Revision: D67718293

fbshipit-source-id: 1dc589cfa3826a73cac843a2448a6b614d33810a
  • Loading branch information
yoney authored and facebook-github-bot committed Dec 31, 2024
1 parent 7553857 commit ea577d5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
36 changes: 26 additions & 10 deletions thrift/compiler/generate/t_mstch_python_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -936,22 +936,38 @@ class python_mstch_field : public mstch_field {
}

mstch::node is_invariant_container_type() {
const auto* type = field_->get_type()->get_true_type();
if (type->is_map()) {
// Mapping is invariant in its key type
const auto* key_type =
dynamic_cast<const t_map*>(type)->get_key_type()->get_true_type();
return key_type->is_struct() || key_type->is_union() ||
key_type->is_exception() || key_type->is_container();
}

return false;
return is_invariant_container_type(field_->get_type());
}

private:
const std::string py_name_;
const t_const* adapter_annotation_;
const t_const* transitive_adapter_annotation_;

static bool is_invariant_container_type(const t_type* type) {
// Mapping is invariant in its key type
// For example, if `Derived` extends `Base`,
// then Mapping[Derived, Any] is not compatible with Mapping[Base, Any].
// We first check if the map has an `Base` key type (for abstract types).
// Then, we recursively verify whether the map's value type, or the element
// type of a list or set, contains any such mapping incompatibility.
const t_type* true_type = type->get_true_type();
if (true_type->is_map()) {
const t_map* map_type = dynamic_cast<const t_map*>(true_type);
const t_type* key_type = map_type->get_key_type()->get_true_type();
return key_type->is_struct() || key_type->is_union() ||
key_type->is_exception() || key_type->is_container() ||
is_invariant_container_type(map_type->get_val_type());
} else if (true_type->is_list()) {
return is_invariant_container_type(
dynamic_cast<const t_list*>(true_type)->get_elem_type());
} else if (true_type->is_set()) {
return is_invariant_container_type(
dynamic_cast<const t_set*>(true_type)->get_elem_type());
}

return false;
}
};

class python_mstch_enum : public mstch_enum {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,6 @@ def list_map_i32_i32(self) -> _typing.Sequence[_typing.Mapping[int, int]]: ...
def list_map_i32_struct(self) -> _typing.Sequence[_typing.Mapping[int, _fbthrift_MyStruct]]: ...
@_fbthrift_property
@_abc.abstractmethod
def list_map_struct_i32(self) -> _typing.Sequence[_typing.Mapping[_fbthrift_MyStruct, int]]: ...
@_fbthrift_property
@_abc.abstractmethod
def set_list_i32(self) -> _typing.AbstractSet[_typing.Sequence[int]]: ...
@_fbthrift_property
@_abc.abstractmethod
Expand All @@ -565,22 +562,13 @@ def set_map_i32_i32(self) -> _typing.AbstractSet[_typing.Mapping[int, int]]: ...
def set_map_i32_struct(self) -> _typing.AbstractSet[_typing.Mapping[int, _fbthrift_MyStruct]]: ...
@_fbthrift_property
@_abc.abstractmethod
def set_map_struct_i32(self) -> _typing.AbstractSet[_typing.Mapping[_fbthrift_MyStruct, int]]: ...
@_fbthrift_property
@_abc.abstractmethod
def map_i32_map_i32_i32(self) -> _typing.Mapping[int, _typing.Mapping[int, int]]: ...
@_fbthrift_property
@_abc.abstractmethod
def map_i32_map_struct_i32(self) -> _typing.Mapping[int, _typing.Mapping[_fbthrift_MyStruct, int]]: ...
@_fbthrift_property
@_abc.abstractmethod
def map_i32_map_i32_struct(self) -> _typing.Mapping[int, _typing.Mapping[int, _fbthrift_MyStruct]]: ...
@_fbthrift_property
@_abc.abstractmethod
def map_i32_map_list_i32_i32(self) -> _typing.Mapping[int, _typing.Sequence[_typing.Mapping[int, int]]]: ...
@_fbthrift_property
@_abc.abstractmethod
def map_i32_map_list_struct_i32(self) -> _typing.Mapping[int, _typing.Sequence[_typing.Mapping[_fbthrift_MyStruct, int]]]: ...
@_abc.abstractmethod
def _to_mutable_python(self) -> "module.thrift_mutable_types.Containers": ... # type: ignore
@_abc.abstractmethod
Expand Down

0 comments on commit ea577d5

Please sign in to comment.