Handle mutable default arguments cleanly

When generating code, ensure that default list/dict arguments are
initialised in local scope if unspecified or `None`.
This commit is contained in:
Arun Babu Neelicattu
2020-07-07 18:13:43 +02:00
committed by Bouke Versteegh
parent 42e197f985
commit 0ba0692dec
6 changed files with 63 additions and 2 deletions

View File

@@ -1,11 +1,14 @@
import asyncio
import sys
from tests.output_betterproto.service.service import (
DoThingResponse,
DoThingRequest,
DoThingResponse,
GetThingRequest,
TestStub as ThingServiceClient,
)
import grpclib
import grpclib.metadata
from grpclib.testing import ChannelFor
import pytest
from betterproto.grpc.util.async_channel import AsyncChannel
@@ -35,6 +38,20 @@ async def test_simple_service_call():
await _test_client(ThingServiceClient(channel))
@pytest.mark.asyncio
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="async mock spy does works for python3.8+"
)
async def test_service_call_mutable_defaults(mocker):
async with ChannelFor([ThingService()]) as channel:
client = ThingServiceClient(channel)
spy = mocker.spy(client, "_unary_unary")
await _test_client(client)
comments = spy.call_args_list[-1].args[1].comments
await _test_client(client)
assert spy.call_args_list[-1].args[1].comments is not comments
@pytest.mark.asyncio
async def test_service_call_with_upfront_request_params():
# Setting deadline

View File

@@ -4,6 +4,7 @@ package service;
message DoThingRequest {
string name = 1;
repeated string comments = 2;
}
message DoThingResponse {