gapic-generator-python icon indicating copy to clipboard operation
gapic-generator-python copied to clipboard

mypy type error for pager

Open tmshn opened this issue 3 years ago • 2 comments

Thanks for stopping by to let us know something could be better!

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

Please run down the following list and make sure you've tried the usual "quick fixes":

  • Search the issues already opened: https://github.com/googleapis/python-datacatalog/issues
  • Search StackOverflow: https://stackoverflow.com/questions/tagged/google-cloud-platform+python

If you are still having issues, please be sure to include as much information as possible:

Environment details

  • OS type and version: macOS/debian
  • Python version: python --version 3.9.1
  • pip version: pip --version 21.0.1
  • google-cloud-datacatalog version: pip show google-cloud-datacatalog 3.2.0
  • mypy version: 0.812

Steps to reproduce

  1. use DataCatlogClient#list_tags in for-loop
  2. check that code using mypy

Code example

from google.cloud import datacatalog

dcclient = datacatalog.DataCatalogClient()
for tag in dcclient.list_tags(parent="dummy"):
    print(tag)

Error

$ mypy snippet.py
snippet.py:4:12: error: "Iterable[Tag]" has no attribute "__next__"
Found 1 error in 1 file (checked 1 source file)

I suspect this is due to the return type of ListTagsPager#__iter__ is annotated as Iterable rather than Iterator, but not sure (according to the Python protocol, Iterable only offers __iter__ method, while Iterator offers __next__).

https://github.com/googleapis/python-datacatalog/blob/936800c92a348b8ce438c4b608f17fe6d9f926a1/google/cloud/datacatalog_v1beta1/services/data_catalog/pagers.py#L470

Making sure to follow these steps will guarantee the quickest resolution possible.

Thanks!

tmshn avatar May 28 '21 10:05 tmshn

This is generated code. Moving to generator component.

tswast avatar Sep 13 '21 21:09 tswast

@dpcollins-google ran into the same issue for Pub/Sub (see https://github.com/googleapis/gapic-generator-python/pull/970) and opened a bug fix to switch the annotation to Iterator.

The main branch for datacatalog pages now has the correct annotation and was released last week in google-cloud-datalog==3.4.3. https://github.com/googleapis/python-datacatalog/blob/main/CHANGELOG.md#343-2021-10-05

https://github.com/googleapis/python-datacatalog/blob/16435a3753d760cec3d88a025630d109cbe52310/google/cloud/datacatalog_v1beta1/services/data_catalog/pagers.py#L463-L472

    @property
    def pages(self) -> Iterator[datacatalog.ListTagsResponse]:
        yield self._response
        while self._response.next_page_token:
            self._request.page_token = self._response.next_page_token
            self._response = self._method(self._request, metadata=self._metadata)
            yield self._response

    def __iter__(self) -> Iterator[tags.Tag]:
        for page in self.pages:
            yield from page.tags

Closing this issue as I believe it has been resolved, but please let me know if you are still seeing issues @tmshn.

busunkim96 avatar Oct 12 '21 20:10 busunkim96

Closing as obsolete

parthea avatar Feb 16 '23 18:02 parthea