Skip to content

Commit

Permalink
Generate mutable types alongside immutable types
Browse files Browse the repository at this point in the history
Summary: Remove "experimental_generate_mutable_types" flag and generate mutable types alongside immutable types.

Reviewed By: createdbysk

Differential Revision: D66937883

fbshipit-source-id: 91413488ccf526f31c83eec25847956128c96272
  • Loading branch information
yoney authored and facebook-github-bot committed Dec 12, 2024
1 parent c4e8c04 commit 39502dd
Show file tree
Hide file tree
Showing 107 changed files with 39,994 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,8 @@ void t_mstch_python_generator::generate_file(
}

void t_mstch_python_generator::generate_types() {
// Keep the "experimental_generate_mutable_types" flag for now to enable
// generation of "to_mutable_python" conversion methods.
const bool experimental_generate_mutable_types =
has_option("experimental_generate_mutable_types");
// DO_BEFORE(satishvk, 20250130): Remove flags related to abstract types after
Expand Down Expand Up @@ -1423,18 +1425,17 @@ void t_mstch_python_generator::generate_types() {

mstch_context_.options.erase("generate_to_mutable_python_conversion_methods");

if (experimental_generate_mutable_types) {
generate_file(
"thrift_mutable_types.py",
IsTypesFile::Yes,
TypeKind::Mutable,
generate_root_path_);
generate_file(
"thrift_mutable_types.pyi",
IsTypesFile::Yes,
TypeKind::Mutable,
generate_root_path_);
}
generate_file(
"thrift_mutable_types.py",
IsTypesFile::Yes,
TypeKind::Mutable,
generate_root_path_);
generate_file(
"thrift_mutable_types.pyi",
IsTypesFile::Yes,
TypeKind::Mutable,
generate_root_path_);

mstch_context_.options["enable_abstract_types"] = "true";
}

Expand All @@ -1458,13 +1459,11 @@ void t_mstch_python_generator::generate_clients() {
IsTypesFile::No,
TypeKind::Immutable,
generate_root_path_);
if (has_option("experimental_generate_mutable_types")) {
generate_file(
"thrift_mutable_clients.py",
IsTypesFile::No,
TypeKind::Mutable,
generate_root_path_);
}
generate_file(
"thrift_mutable_clients.py",
IsTypesFile::No,
TypeKind::Mutable,
generate_root_path_);
}

void t_mstch_python_generator::generate_services() {
Expand All @@ -1478,13 +1477,11 @@ void t_mstch_python_generator::generate_services() {
IsTypesFile::No,
TypeKind::Immutable,
generate_root_path_);
if (has_option("experimental_generate_mutable_types")) {
generate_file(
"thrift_mutable_services.py",
IsTypesFile::No,
TypeKind::Mutable,
generate_root_path_);
}
generate_file(
"thrift_mutable_services.py",
IsTypesFile::No,
TypeKind::Mutable,
generate_root_path_);
}

class t_python_patch_generator : public t_mstch_generator {
Expand Down Expand Up @@ -1533,16 +1530,6 @@ THRIFT_REGISTER_GENERATOR(
mstch_python,
"Python",
" include_prefix: Use full include paths in generated files.\n"
" experimental_generate_mutable_types:\n"
" DO NOT USE. Enables the experimental generation of mutable\n"
" thrift-python types (i.e., structs and unions). This is for local\n"
" experimentation, development and testing ONLY. When this option is\n"
" enabled, NO GUARANTEE is provided on any output (including any\n"
" seemingly unrelated logic, such as previously existing generated\n"
" code). Any \"new\" behavior (including the existence of this\n"
" option) should be considered UNSTABLE and is subject to arbitrary\n"
" (backwards incompatible) changes and undefined behavior.\n"
" NEVER ENABLE THIS OPTION in production environments.\n"
" disable_abstract_types:\n"
" Disable the use of abstract types with thrift-python"
" immutable and mutable types.\n");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#
# Autogenerated by Thrift
#
# DO NOT EDIT
# @generated
#

from __future__ import annotations

import typing as _typing

import apache.thrift.metadata.thrift_types as _fbthrift_metadata
import folly.iobuf as _fbthrift_iobuf
from thrift.python.client import (
AsyncClient as _fbthrift_python_AsyncClient,
SyncClient as _fbthrift_python_SyncClient,
Client as _fbthrift_python_Client,
)
from thrift.python.client.omni_client import InteractionMethodPosition as _fbthrift_InteractionMethodPosition, FunctionQualifier as _fbthrift_FunctionQualifier
from thrift.python.common import RpcOptions
import thrift.python.mutable_containers as _fbthrift_python_mutable_containers
import thrift.python.mutable_exceptions as _fbthrift_python_mutable_exceptions
import thrift.python.mutable_types as _fbthrift_python_mutable_types
import thrift.python.exceptions as _fbthrift_python_exceptions
import thrift.python.types as _fbthrift_python_types
import a.thrift_mutable_types as _fbthrift__a__thrift_mutable_types
import a.thrift_metadata
import b.thrift_mutable_types as _fbthrift__b__thrift_mutable_types
import c.thrift_mutable_types as _fbthrift__c__thrift_mutable_types
import three
import two
import one
import typeshed_three
import typeshed_two
import typeshed_one

class MyService(_fbthrift_python_Client["MyService.Async", "MyService.Sync"]):
@staticmethod
def __get_thrift_name__() -> str:
return "a.MyService"

@staticmethod
def __get_thrift_uri__() -> _typing.Optional[str]:
return None

@staticmethod
def __get_thrift_unstructured_annotations_DEPRECATED__() -> _typing.Mapping[str, str]:
return {
}

@staticmethod
def __get_metadata__() -> _fbthrift_metadata.ThriftMetadata:
return a.thrift_metadata.gen_metadata_service_MyService()

class Async(_fbthrift_python_AsyncClient):
@staticmethod
def __get_thrift_name__() -> str:
return "a.MyService"

@staticmethod
def __get_thrift_uri__() -> _typing.Optional[str]:
return None

@staticmethod
def __get_metadata__() -> _fbthrift_metadata.ThriftMetadata:
return a.thrift_metadata.gen_metadata_service_MyService()

async def adapted_return(
self,
*,
rpc_options: _typing.Optional[RpcOptions] = None,
) -> typeshed_one.AdapterOneType[_fbthrift__b__thrift_mutable_types.B]:
_fbthrift_resp = await self._send_request(
"MyService",
"adapted_return",
_fbthrift__a__thrift_mutable_types._fbthrift_MyService_adapted_return_args(),
_fbthrift__a__thrift_mutable_types._fbthrift_MyService_adapted_return_result,
qualifier = _fbthrift_FunctionQualifier.Unspecified,
uri_or_name="MyService",
rpc_options=rpc_options,
is_mutable_types=True,
)
# shortcut to success path for non-void returns
if _fbthrift_resp.success is not None:
return _fbthrift_resp.success
raise _fbthrift_python_exceptions.ApplicationError(
_fbthrift_python_exceptions.ApplicationErrorType.MISSING_RESULT,
"Empty Response",
)

async def adapted_param(
self,
param: typeshed_two.AdapterTwoType[_fbthrift__c__thrift_mutable_types.C2],
*,
rpc_options: _typing.Optional[RpcOptions] = None,
) -> None:
_fbthrift_resp = await self._send_request(
"MyService",
"adapted_param",
_fbthrift__a__thrift_mutable_types._fbthrift_MyService_adapted_param_args(
param=param,),
_fbthrift__a__thrift_mutable_types._fbthrift_MyService_adapted_param_result,
qualifier = _fbthrift_FunctionQualifier.Unspecified,
uri_or_name="MyService",
rpc_options=rpc_options,
is_mutable_types=True,
)


# pyre-ignore[4]: Missing annotation.
adapted_return = Async.adapted_return
# pyre-ignore[4]: Missing annotation.
adapted_param = Async.adapted_param

class Sync(_fbthrift_python_SyncClient):
@staticmethod
def __get_thrift_name__() -> str:
return "a.MyService"

@staticmethod
def __get_thrift_uri__() -> _typing.Optional[str]:
return None

@staticmethod
def __get_metadata__() -> _fbthrift_metadata.ThriftMetadata:
return a.thrift_metadata.gen_metadata_service_MyService()

def adapted_return(
self,
*,
rpc_options: _typing.Optional[RpcOptions] = None,
) -> typeshed_one.AdapterOneType[_fbthrift__b__thrift_mutable_types.B]:
_fbthrift_resp = self._send_request(
"MyService",
"adapted_return",
_fbthrift__a__thrift_mutable_types._fbthrift_MyService_adapted_return_args(),
_fbthrift__a__thrift_mutable_types._fbthrift_MyService_adapted_return_result,
uri_or_name="MyService",
rpc_options=rpc_options,
is_mutable_types=True,
)
# shortcut to success path for non-void returns
if _fbthrift_resp.success is not None:
return _fbthrift_resp.success
raise _fbthrift_python_exceptions.ApplicationError(
_fbthrift_python_exceptions.ApplicationErrorType.MISSING_RESULT,
"Empty Response",
)

def adapted_param(
self,
param: typeshed_two.AdapterTwoType[_fbthrift__c__thrift_mutable_types.C2],
*,
rpc_options: _typing.Optional[RpcOptions] = None,
) -> None:
_fbthrift_resp = self._send_request(
"MyService",
"adapted_param",
_fbthrift__a__thrift_mutable_types._fbthrift_MyService_adapted_param_args(
param=param,),
_fbthrift__a__thrift_mutable_types._fbthrift_MyService_adapted_param_result,
uri_or_name="MyService",
rpc_options=rpc_options,
is_mutable_types=True,
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#
# Autogenerated by Thrift
#
# DO NOT EDIT
# @generated
#

from __future__ import annotations

from abc import ABCMeta
import typing as _typing

import folly.iobuf as _fbthrift_iobuf

import apache.thrift.metadata.thrift_types as _fbthrift_metadata
import thrift.python.mutable_containers as _fbthrift_python_mutable_containers
from thrift.python.mutable_serializer import serialize_iobuf, deserialize, Protocol
from thrift.python.server import ServiceInterface, RpcKind, PythonUserException

import a.thrift_mutable_types as _fbthrift__a__thrift_mutable_types
import a.thrift_metadata
import b.thrift_mutable_types as _fbthrift__b__thrift_mutable_types
import c.thrift_mutable_types as _fbthrift__c__thrift_mutable_types
import three
import two
import one
import typeshed_three
import typeshed_two
import typeshed_one

class MyServiceInterface(
ServiceInterface,
metaclass=ABCMeta
):

@staticmethod
def service_name() -> bytes:
return b"MyService"

def getFunctionTable(self) -> _typing.Mapping[bytes, _typing.Callable[..., object]]:
functionTable = {
b"adapted_return": (RpcKind.SINGLE_REQUEST_SINGLE_RESPONSE, self._fbthrift__handler_adapted_return),
b"adapted_param": (RpcKind.SINGLE_REQUEST_SINGLE_RESPONSE, self._fbthrift__handler_adapted_param),
}
return {**super().getFunctionTable(), **functionTable}

@staticmethod
def __get_thrift_name__() -> str:
return "a.MyService"

@staticmethod
def __get_metadata__() -> _fbthrift_metadata.ThriftMetadata:
return a.thrift_metadata.gen_metadata_service_MyService()

@staticmethod
def __get_metadata_service_response__() -> _fbthrift_metadata.ThriftServiceMetadataResponse:
return a.thrift_metadata._fbthrift_metadata_service_response_MyService()



async def adapted_return(
self
) -> typeshed_one.AdapterOneType[_fbthrift__b__thrift_mutable_types.B]:
raise NotImplementedError("async def adapted_return is not implemented")

async def _fbthrift__handler_adapted_return(self, args: _fbthrift_iobuf.IOBuf, protocol: Protocol) -> _fbthrift_iobuf.IOBuf:
args_struct = deserialize(_fbthrift__a__thrift_mutable_types._fbthrift_MyService_adapted_return_args, args, protocol)
value = await self.adapted_return()
return_struct = _fbthrift__a__thrift_mutable_types._fbthrift_MyService_adapted_return_result(success=value)
return serialize_iobuf(return_struct, protocol)


async def adapted_param(
self,
param: typeshed_two.AdapterTwoType[_fbthrift__c__thrift_mutable_types.C2]
) -> None:
raise NotImplementedError("async def adapted_param is not implemented")

async def _fbthrift__handler_adapted_param(self, args: _fbthrift_iobuf.IOBuf, protocol: Protocol) -> _fbthrift_iobuf.IOBuf:
args_struct = deserialize(_fbthrift__a__thrift_mutable_types._fbthrift_MyService_adapted_param_args, args, protocol)
value = await self.adapted_param(args_struct.param,)
return_struct = _fbthrift__a__thrift_mutable_types._fbthrift_MyService_adapted_param_result()
return serialize_iobuf(return_struct, protocol)

Loading

0 comments on commit 39502dd

Please sign in to comment.