Skip to content

Commit

Permalink
Generate properties for mutable containers
Browse files Browse the repository at this point in the history
Summary: Generate `property` and `name.setter` for the mutable container hints.

Differential Revision: D64530530

fbshipit-source-id: 15e242be17262ae5893df199f0cf3f224d17c1dd
  • Loading branch information
yoney authored and facebook-github-bot committed Oct 18, 2024
1 parent c125854 commit 7c53333
Show file tree
Hide file tree
Showing 21 changed files with 818 additions and 137 deletions.
7 changes: 7 additions & 0 deletions thrift/compiler/generate/t_mstch_python_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ class python_mstch_field : public mstch_field {
{"field:user_default_value",
&python_mstch_field::user_default_value},
{"field:has_adapter?", &python_mstch_field::adapter},
{"field:is_container_type", &python_mstch_field::is_container_type},
});
}

Expand Down Expand Up @@ -851,6 +852,12 @@ class python_mstch_field : public mstch_field {
adapter_annotation_, transitive_adapter_annotation_, context_, pos_);
}

mstch::node is_container_type() {
const auto* type = field_->get_type();
return type->get_true_type()->is_list() ||
type->get_true_type()->is_map() || type->get_true_type()->is_set();
}

private:
const std::string py_name_;
const t_const* adapter_annotation_;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{{!
Copyright (c) Meta Platforms, Inc. and affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
}}{{!
Generate the type hints for fields
}}
{{#field:optional?}}_typing.Optional[{{/field:optional?}}{{!
}}{{> fields/adapted_type}}{{!
}}{{#field:optional?}}]{{/field:optional?}}
33 changes: 21 additions & 12 deletions thrift/compiler/generate/templates/python/types/types_pyi.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {{program:base_library_package}}.exceptions as _fbthrift_python_exception
{{#program:generate_mutable_types}}
import {{program:base_library_package}}.mutable_types as _fbthrift_python_mutable_types
import {{program:base_library_package}}.mutable_exceptions as _fbthrift_python_mutable_exceptions
import {{program:base_library_package}}.mutable_containers as _fbthrift_python_mutable_containers
{{/program:generate_mutable_types}}
{{#program:include_namespaces}}
{{#has_types?}}
Expand Down Expand Up @@ -77,18 +78,26 @@ class {{> structs/unadapted_name}}({{!
}}{{/struct:union?}}{{!
}}, _fbthrift_compatible_with_{{> structs/unadapted_name}}{{!
}}):
{{#struct:fields_ordered_by_id}}
{{field:py_name}}: {{!
}}{{#program:generate_immutable_types}}{{!
}}_typing.Final[{{!
}}{{/program:generate_immutable_types}}{{!
}}{{#field:optional?}}_typing.Optional[{{/field:optional?}}{{!
}}{{> fields/adapted_type}}{{!
}}{{#field:optional?}}]{{/field:optional?}}{{!
}}{{#program:generate_immutable_types}}{{!
}}]{{!
}}{{/program:generate_immutable_types}} = ...
{{/struct:fields_ordered_by_id}}
{{#program:generate_immutable_types}}{{!
}}{{#struct:fields_ordered_by_id}}
{{field:py_name}}: _typing.Final[{{> fields/field_type_pyi}}] = ...
{{/struct:fields_ordered_by_id}}{{!
}}{{/program:generate_immutable_types}}{{!
}}{{#program:generate_mutable_types}}{{!
}}{{#struct:fields_ordered_by_id}}{{!
}}{{^field:is_container_type}}
{{field:py_name}}: {{> fields/field_type_pyi}} = ...
{{/field:is_container_type}}{{!
}}{{#field:is_container_type}}

@property
def {{field:py_name}}(self) -> {{> fields/field_type_pyi}}: ...
@{{field:py_name}}.setter
def {{field:py_name}}(self, value: {{> fields/field_type_pyi}}): ...

{{/field:is_container_type}}{{!
}}{{/struct:fields_ordered_by_id}}{{!
}}{{/program:generate_mutable_types}}
def __init__(
self,{{#struct:fields?}} *{{/struct:fields?}}{{#struct:fields_ordered_by_id}},
{{field:py_name}}: _typing.Optional[{{> fields/compatible_adapted_type}}]=...{{/struct:fields_ordered_by_id}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import folly.iobuf as _fbthrift_iobuf
import thrift.python.types as _fbthrift_python_types
import thrift.python.mutable_types as _fbthrift_python_mutable_types
import thrift.python.mutable_exceptions as _fbthrift_python_mutable_exceptions
import thrift.python.mutable_containers as _fbthrift_python_mutable_containers

class _fbthrift_compatible_with_MyEnum:
pass
Expand Down Expand Up @@ -64,7 +65,12 @@ class MyStruct(_fbthrift_python_mutable_types.MutableStruct, _fbthrift_compatibl
oneway: bool = ...
readonly: bool = ...
idempotent: bool = ...
floatSet: _typing.MutableSet[float] = ...

@property
def floatSet(self) -> _typing.MutableSet[float]: ...
@floatSet.setter
def floatSet(self, value: _typing.MutableSet[float]): ...

no_hack_codegen_field: str = ...
def __init__(
self, *,
Expand Down Expand Up @@ -103,9 +109,24 @@ class _fbthrift_compatible_with_Containers:


class Containers(_fbthrift_python_mutable_types.MutableStruct, _fbthrift_compatible_with_Containers):
I32List: _typing.MutableSequence[int] = ...
StringSet: _typing.MutableSet[str] = ...
StringToI64Map: _typing.MutableMapping[str, int] = ...

@property
def I32List(self) -> _typing.MutableSequence[int]: ...
@I32List.setter
def I32List(self, value: _typing.MutableSequence[int]): ...


@property
def StringSet(self) -> _typing.MutableSet[str]: ...
@StringSet.setter
def StringSet(self, value: _typing.MutableSet[str]): ...


@property
def StringToI64Map(self) -> _typing.MutableMapping[str, int]: ...
@StringToI64Map.setter
def StringToI64Map(self, value: _typing.MutableMapping[str, int]): ...

def __init__(
self, *,
I32List: _typing.Optional[_typing.MutableSequence[int]]=...,
Expand Down Expand Up @@ -153,7 +174,12 @@ class MyUnion(_fbthrift_python_mutable_types.MutableUnion, _fbthrift_compatible_
myEnum: MyEnum = ...
myStruct: MyStruct = ...
myDataItem: MyDataItem = ...
floatSet: _typing.MutableSet[float] = ...

@property
def floatSet(self) -> _typing.MutableSet[float]: ...
@floatSet.setter
def floatSet(self, value: _typing.MutableSet[float]): ...

def __init__(
self, *,
myEnum: _typing.Optional[_fbthrift_compatible_with_MyEnum]=...,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import folly.iobuf as _fbthrift_iobuf
import thrift.python.types as _fbthrift_python_types
import thrift.python.mutable_types as _fbthrift_python_mutable_types
import thrift.python.mutable_exceptions as _fbthrift_python_mutable_exceptions
import thrift.python.mutable_containers as _fbthrift_python_mutable_containers


class _fbthrift_compatible_with_EchoRequest:
Expand Down
Loading

0 comments on commit 7c53333

Please sign in to comment.