Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Modelerfour version: 4.13.351
generated library will use that auth policy unless they pass in a separate one through kwargs #686
- Added support for a data plane multiapi client #693

**Bug Fixes**

- Fix typing for discriminator values in models, so Python 3.5 can import py3 file for models #691

### 2020-06-08 - 5.1.0-preview.2
Modelerfour version: 4.13.351

Expand Down
6 changes: 5 additions & 1 deletion autorest/codegen/serializers/model_generic_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ def init_args(model: ObjectSchema) -> List[str]:
init_args.append(f"self.{prop.name} = None")
elif prop.is_discriminator:
discriminator_value = f"'{model.discriminator_value}'" if model.discriminator_value else None
init_args.append(f"self.{prop.name} = {discriminator_value}")
if not discriminator_value:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discriminator is required, the base class will have None (all the time), and it will be a constant (all the time) in sub-classes. Optional[str] sounds incorrect

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. We originally had the Optional[str] for the base class, let me switch that to None. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tried to switch it to None for base class definitions of None, but that's throwing a mypy error, because the typing for the base class needs to encompass the typing of the discriminator in both the base class and the children classes. I think it needs to be left as Optional[str] for the base class. Here's the mypy error:

Incompatible types in assignment (expression has type "str", base class "Fish" defined the type as "None")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, that's ok then

typing = "Optional[str]"
else:
typing = "str"
init_args.append(f"self.{prop.name} = {discriminator_value} # type: {typing}")
elif not prop.constant:
if prop.required and not prop.schema.default_value:
init_args.append(f"self.{prop.name} = kwargs['{prop.name}']")
Expand Down
4 changes: 1 addition & 3 deletions autorest/codegen/serializers/model_python3_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,11 @@ def init_args(model: ObjectSchema) -> List[str]:
init_args.append(f"self.{prop.name} = None")
elif prop.is_discriminator:
discriminator_value = f"'{model.discriminator_value}'" if model.discriminator_value else None
# adding the type ignore because mypy throws an incompatible type error because
# the children have a value for the discriminator, while the parent sets it to None
if not discriminator_value:
typing = "Optional[str]"
else:
typing = "str"
init_args.append(f"self.{prop.name}: {typing} = {discriminator_value}")
init_args.append(f"self.{prop.name} = {discriminator_value} # type: {typing}")
elif not prop.constant:
init_args.append(f"self.{prop.name} = {prop.name}")

Expand Down
335 changes: 168 additions & 167 deletions test/azure/AcceptanceTests/asynctests/test_paging.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,170 +63,171 @@ async def custom_url_client(credential, authentication_policy):
async with AutoRestParameterizedHostTestPagingClient(credential, host="host:3000", authentication_policy=authentication_policy) as client:
await yield_(client)

@pytest.mark.asyncio
async def test_get_no_item_name_pages(client):
pages = client.paging.get_no_item_name_pages()
items = []
async for item in pages:
items.append(item)
assert len(items) == 1
assert items[0].properties.id == 1
assert items[0].properties.name == "Product"

@pytest.mark.asyncio
async def test_get_null_next_link_name_pages(client):
pages = client.paging.get_null_next_link_name_pages()
items = []
async for item in pages:
items.append(item)
assert len(items) == 1
assert items[0].properties.id == 1
assert items[0].properties.name == "Product"

@pytest.mark.asyncio
async def test_get_single_pages_with_cb(client):
def cb(list_of_obj):
for obj in list_of_obj:
obj.marked = True
return list_of_obj
async for obj in client.paging.get_single_pages(cls=cb):
assert obj.marked

@pytest.mark.asyncio
async def test_get_single_pages(client):
pages = client.paging.get_single_pages()
items = []
async for item in pages:
items.append(item)
assert len(items) == 1
assert items[0].properties.id == 1
assert items[0].properties.name == "Product"

@pytest.mark.asyncio
async def test_get_multiple_pages(client):
pages = client.paging.get_multiple_pages()
items = []
async for item in pages:
items.append(item)
assert len(items) == 10

@pytest.mark.asyncio
async def test_query_params(client):
pages = client.paging.get_with_query_params(required_query_parameter='100')
items = []
async for item in pages:
items.append(item)
assert len(items) == 2

@pytest.mark.asyncio
async def test_get_odata_multiple_pages(client):
pages = client.paging.get_odata_multiple_pages()
items = []
async for item in pages:
items.append(item)
assert len(items) == 10

@pytest.mark.asyncio
async def test_get_multiple_pages_retry_first(client):
pages = client.paging.get_multiple_pages_retry_first()
items = []
async for item in pages:
items.append(item)
assert len(items) == 10

@pytest.mark.asyncio
async def test_get_multiple_pages_retry_second(client):
pages = client.paging.get_multiple_pages_retry_second()
items = []
async for item in pages:
items.append(item)
assert len(items) == 10

@pytest.mark.asyncio
async def test_get_multiple_pages_with_offset(client):
from paging.models import PagingGetMultiplePagesWithOffsetOptions
options = PagingGetMultiplePagesWithOffsetOptions(offset=100)
pages = client.paging.get_multiple_pages_with_offset(paging_get_multiple_pages_with_offset_options=options)
items = []
async for item in pages:
items.append(item)
assert len(items) == 10
assert items[-1].properties.id == 110

@pytest.mark.asyncio
async def test_get_single_pages_failure(client):
pages = client.paging.get_single_pages_failure()
with pytest.raises(HttpResponseError):
async for i in pages:
...

@pytest.mark.asyncio
async def test_get_multiple_pages_failure(client):
pages = client.paging.get_multiple_pages_failure()
with pytest.raises(HttpResponseError):
async for i in pages:
...

@pytest.mark.asyncio
async def test_get_multiple_pages_failure_uri(client):
pages = client.paging.get_multiple_pages_failure_uri()
with pytest.raises(HttpResponseError):
async for i in pages:
...

@pytest.mark.asyncio
async def test_paging_fragment_path(client):

pages = client.paging.get_multiple_pages_fragment_next_link("1.6", "test_user")
items = []
async for item in pages:
items.append(item)
assert len(items) == 10

with pytest.raises(AttributeError):
# Be sure this method is not generated (Transform work)
await client.paging.get_multiple_pages_fragment_next_link_next() # pylint: disable=E1101

@pytest.mark.asyncio
async def test_custom_url_get_pages_partial_url(custom_url_client):
pages = custom_url_client.paging.get_pages_partial_url("local")
items = []
async for item in pages:
items.append(item)

assert len(items) == 2
assert items[0].properties.id == 1
assert items[1].properties.id == 2

@pytest.mark.asyncio
async def test_custom_url_get_pages_partial_url_operation(custom_url_client):
pages = custom_url_client.paging.get_pages_partial_url_operation("local")
items = []
async for item in pages:
items.append(item)

assert len(items) == 2
assert items[0].properties.id == 1
assert items[1].properties.id == 2

@pytest.mark.asyncio
async def test_get_multiple_pages_lro(client):
"""LRO + Paging at the same time.
"""
poller = await client.paging.begin_get_multiple_pages_lro()
pager = await poller.result()
items = []
async for item in pager:
items.append(item)

assert len(items) == 10
assert items[0].properties.id == 1
assert items[1].properties.id == 2

@pytest.mark.asyncio
async def test_item_name_with_xms_client_name(client):
pages = client.paging.get_paging_model_with_item_name_with_xms_client_name()
items = []
async for item in pages:
items.append(item)
assert len(items) == 1
class TestPaging(object):
@pytest.mark.asyncio
async def test_get_no_item_name_pages(self, client):
pages = client.paging.get_no_item_name_pages()
items = []
async for item in pages:
items.append(item)
assert len(items) == 1
assert items[0].properties.id == 1
assert items[0].properties.name == "Product"

@pytest.mark.asyncio
async def test_get_null_next_link_name_pages(self, client):
pages = client.paging.get_null_next_link_name_pages()
items = []
async for item in pages:
items.append(item)
assert len(items) == 1
assert items[0].properties.id == 1
assert items[0].properties.name == "Product"

@pytest.mark.asyncio
async def test_get_single_pages_with_cb(self, client):
def cb(list_of_obj):
for obj in list_of_obj:
obj.marked = True
return list_of_obj
async for obj in client.paging.get_single_pages(cls=cb):
assert obj.marked

@pytest.mark.asyncio
async def test_get_single_pages(self, client):
pages = client.paging.get_single_pages()
items = []
async for item in pages:
items.append(item)
assert len(items) == 1
assert items[0].properties.id == 1
assert items[0].properties.name == "Product"

@pytest.mark.asyncio
async def test_get_multiple_pages(self, client):
pages = client.paging.get_multiple_pages()
items = []
async for item in pages:
items.append(item)
assert len(items) == 10

@pytest.mark.asyncio
async def test_query_params(self, client):
pages = client.paging.get_with_query_params(required_query_parameter='100')
items = []
async for item in pages:
items.append(item)
assert len(items) == 2

@pytest.mark.asyncio
async def test_get_odata_multiple_pages(self, client):
pages = client.paging.get_odata_multiple_pages()
items = []
async for item in pages:
items.append(item)
assert len(items) == 10

@pytest.mark.asyncio
async def test_get_multiple_pages_retry_first(self, client):
pages = client.paging.get_multiple_pages_retry_first()
items = []
async for item in pages:
items.append(item)
assert len(items) == 10

@pytest.mark.asyncio
async def test_get_multiple_pages_retry_second(self, client):
pages = client.paging.get_multiple_pages_retry_second()
items = []
async for item in pages:
items.append(item)
assert len(items) == 10

@pytest.mark.asyncio
async def test_get_multiple_pages_with_offset(self, client):
from paging.models import PagingGetMultiplePagesWithOffsetOptions
options = PagingGetMultiplePagesWithOffsetOptions(offset=100)
pages = client.paging.get_multiple_pages_with_offset(paging_get_multiple_pages_with_offset_options=options)
items = []
async for item in pages:
items.append(item)
assert len(items) == 10
assert items[-1].properties.id == 110

@pytest.mark.asyncio
async def test_get_single_pages_failure(self, client):
pages = client.paging.get_single_pages_failure()
with pytest.raises(HttpResponseError):
async for i in pages:
...

@pytest.mark.asyncio
async def test_get_multiple_pages_failure(self, client):
pages = client.paging.get_multiple_pages_failure()
with pytest.raises(HttpResponseError):
async for i in pages:
...

@pytest.mark.asyncio
async def test_get_multiple_pages_failure_uri(self, client):
pages = client.paging.get_multiple_pages_failure_uri()
with pytest.raises(HttpResponseError):
async for i in pages:
...

@pytest.mark.asyncio
async def test_paging_fragment_path(self, client):

pages = client.paging.get_multiple_pages_fragment_next_link("1.6", "test_user")
items = []
async for item in pages:
items.append(item)
assert len(items) == 10

with pytest.raises(AttributeError):
# Be sure this method is not generated (Transform work)
await client.paging.get_multiple_pages_fragment_next_link_next() # pylint: disable=E1101

@pytest.mark.asyncio
async def test_custom_url_get_pages_partial_url(self, custom_url_client):
pages = custom_url_client.paging.get_pages_partial_url("local")
items = []
async for item in pages:
items.append(item)

assert len(items) == 2
assert items[0].properties.id == 1
assert items[1].properties.id == 2

@pytest.mark.asyncio
async def test_custom_url_get_pages_partial_url_operation(self, custom_url_client):
pages = custom_url_client.paging.get_pages_partial_url_operation("local")
items = []
async for item in pages:
items.append(item)

assert len(items) == 2
assert items[0].properties.id == 1
assert items[1].properties.id == 2

@pytest.mark.asyncio
async def test_get_multiple_pages_lro(self, client):
"""LRO + Paging at the same time.
"""
poller = await client.paging.begin_get_multiple_pages_lro()
pager = await poller.result()
items = []
async for item in pager:
items.append(item)

assert len(items) == 10
assert items[0].properties.id == 1
assert items[1].properties.id == 2

@pytest.mark.asyncio
async def test_item_name_with_xms_client_name(self, client):
pages = client.paging.get_paging_model_with_item_name_with_xms_client_name()
items = []
async for item in pages:
items.append(item)
assert len(items) == 1
10 changes: 10 additions & 0 deletions test/azure/AcceptanceTests/test_azure_custom_base_uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@ def test_custom_base_uri_bad_host(self, client):
client._config.host = "badhost:3000"
with pytest.raises(ServiceRequestError):
client.paths.get_empty("local")

def test_models(self):
from custombaseurl.models import Error

if sys.version_info >= (3,5):
from custombaseurl.models._models_py3 import Error as ErrorPy3
assert Error == ErrorPy3
else:
from custombaseurl.models._models import Error as ErrorPy2
assert Error == ErrorPy2
Loading