Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update paging implementation to handle unconventional pagination #750

Merged
merged 8 commits into from
Feb 4, 2021

Conversation

yon-mg
Copy link
Contributor

@yon-mg yon-mg commented Jan 25, 2021

Pagination for GCE is unconventional in that it may use max_results instead of page_results and may return a map paginated resource instead of a repeated resource. Pagination otherwise does not change and even GCE may use conventional pagination for some rpcs.

Before this PR, pagination has only been implemented for the conventional case. This fix resolves that and adds the relevant testing to ensure coverage.

On an unrelated note, this PR now also includes a minor update to mypy cli flags due to the recent mypy update, https://mypy-lang.blogspot.com/2021/01/mypy-0800-released.html.

@yon-mg yon-mg requested a review from a team as a code owner January 25, 2021 18:24
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Jan 25, 2021
@codecov
Copy link

codecov bot commented Jan 25, 2021

Codecov Report

Merging #750 (959e1a0) into master (4077b45) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #750   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           27        27           
  Lines         1619      1623    +4     
  Branches       328       330    +2     
=========================================
+ Hits          1619      1623    +4     
Impacted Files Coverage Δ
gapic/schema/wrappers.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4077b45...959e1a0. Read the comment docs.

Copy link
Contributor

@software-dov software-dov left a comment

Choose a reason for hiding this comment

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

Please add some comments in paged_result_field detailing the two options and how they work internally.

Comment on lines 870 to 873
(self.input, str, 'page_token'),
for page_field_token in ((self.input, str, 'page_token'),
(self.output, str, 'next_page_token')):
field = page_field[0].fields.get(page_field[2], None)
if not field or field.type != page_field[1]:
field = page_field_token[0].fields.get(page_field_token[2], None)
if not field or field.type != page_field_token[1]:
return None
Copy link
Contributor

Choose a reason for hiding this comment

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

Great extension. Since we're changing things here, can we tweak the loop so that we're destructuring into names instead of indexing the tuple?

for source, name in ((self.input, "page_token"), (self.output, "next_page_token")):

Something like that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. Done

page_fields = (self.input.fields.get('max_results', None),
self.input.fields.get('page_size', None))
page_field_size = next(
(field for field in page_fields if field is not None), None)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: we don't have to explicitly check for None.

next((field for field in page_fields if field), None)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just wanted to be extra explicit in case there was some false-ish value field might have. I suppose that's not the case for the Field class though.

Copy link
Contributor

@vchudnov-g vchudnov-g left a comment

Choose a reason for hiding this comment

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

Looks good! Minor comments and some questions for my own learning.

response = response + response

# Wrap the values into proper Response objs
response = tuple(map(lambda x: {{ method.output.ident }}.to_json(x), response))
Copy link
Contributor

Choose a reason for hiding this comment

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

Python question: response and side_effect only contain one element. Why do you need to make them tuples? (I see zip() requires iterables, but so does tuple())

Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: you don't need to call both map and tuple: tuple can take generator expressions as its iterable.

response = tuple({{ method.output.ident }}.to_json(x) for x in response)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for that. Not sure why I was dead set on using map.


# Wrap the values into proper Response objs
response = tuple(map(lambda x: {{ method.output.ident }}.to_json(x), response))
side_effect = tuple(map(lambda x: Response(), response))
Copy link
Contributor

Choose a reason for hiding this comment

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

Being inexperienced with Python mocks, I found the name side_effect confusing here. IIUC, this could be called return_values (and then assigned to req.side_effect)

(Note to self: https://docs.python.org/3/library/unittest.mock-examples.html#side-effect-functions-and-iterables)

Copy link
Contributor

Choose a reason for hiding this comment

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

We can be a little bit clever here:

side_effect = (Response(),) * len(response)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this does not work because new Response objects are not made. Instead it is aliases to the same object and editing a value within one changes it for all.

Copy link
Contributor

Choose a reason for hiding this comment

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

Aha, good point.

make_field(name='next_page_token', type=9), # str
))
)
assert method.paged_result_field is None
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we add tests in this file for max_results and for mapped responses?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mapped responses are treated the same here. Checking for repeated fields should be sufficient since mapped fields are also repeated. Test for max_results is now also added.

assert page_.raw_page.next_page_token == token


{% endif %} {# paged methods #}
Copy link
Contributor

Choose a reason for hiding this comment

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

Are the results of this template visible in a generated file within this repo? (just asking, the repo may not be structured that way)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. It may be useful to include a generated client but that might clog the repo. The showcase client is generated as part of CircleCI tests though.

response = response + response

# Wrap the values into proper Response objs
response = tuple(map(lambda x: {{ method.output.ident }}.to_json(x), response))
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: you don't need to call both map and tuple: tuple can take generator expressions as its iterable.

response = tuple({{ method.output.ident }}.to_json(x) for x in response)


# Wrap the values into proper Response objs
response = tuple(map(lambda x: {{ method.output.ident }}.to_json(x), response))
side_effect = tuple(map(lambda x: Response(), response))
Copy link
Contributor

Choose a reason for hiding this comment

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

We can be a little bit clever here:

side_effect = (Response(),) * len(response)

assert pager.get('h') is None
{% endif %}

results = [i for i in pager]
Copy link
Contributor

Choose a reason for hiding this comment

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

Judgement call: can also do results = list(pager)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is cleaner. Done.

Comment on lines 1265 to 1266
tuple(map(lambda x: type(x), results[0])) == (str, {{ method.paged_result_field.type.fields.get('value').ident }})
for i in results)
Copy link
Contributor

Choose a reason for hiding this comment

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

Am I misreading this expression, or is this a loop invariant? If so, let's lift it up. In either case, let's clarifiy it to

tuple(type(r) for r in results[0])

Actually, the whole line looks like a loop invariant.

@@ -123,6 +123,16 @@ def test_method_paged_result_field_no_page_field():
)
assert method.paged_result_field is None

method = make_method('Foo',
Copy link
Contributor

Choose a reason for hiding this comment

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

Formatting nit: please add a newline and then indent here for better readability

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.. I think.

@@ -68,14 +68,25 @@ class {{ method.name }}Pager:
self._response = self._method(self._request, metadata=self._metadata)
yield self._response

{% if method.paged_result_field.map %}
def __iter__(self) -> Iterable[Tuple[str, {{ method.paged_result_field.ident | replace('Sequence[', '') | replace(']', '') }}]]:
Copy link
Contributor

Choose a reason for hiding this comment

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

Please fix the typing issue as we discussed offline.

for page in self.pages:
yield from page.{{ method.paged_result_field.name}}.items()

def get(self, key: str) -> {{ method.paged_result_field.ident | replace('Sequence', 'Optional') }}:
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

@yon-mg
Copy link
Contributor Author

yon-mg commented Feb 1, 2021

@software-dov friendly ping for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants