googleapis / gapic-generator

Tools for generating API client libraries from API Service Configuration descriptions.
Apache License 2.0
304 stars 129 forks source link

Python: 'CallOptions' not imported in examples (nor is it needed for page iteration) #2388

Closed tseaver closed 5 years ago

tseaver commented 5 years ago

From https://github.com/googleapis/google-cloud-python/issues/5560.

Examples in generated docstrings for methods returning iterators use CallOptions without importing it, e.g.:


    # Service calls
    def list_monitored_resource_descriptors(
            self,
            name,
            filter_=None,
            page_size=None,
            retry=google.api_core.gapic_v1.method.DEFAULT,
            timeout=google.api_core.gapic_v1.method.DEFAULT,
            metadata=None):
        """
        Lists monitored resource descriptors that match a filter. This method does not require a Stackdriver account.

        Example:
            >>> from google.cloud import monitoring_v3
            >>>
            >>> client = monitoring_v3.MetricServiceClient()
            >>>
            >>> name = client.project_path('[PROJECT]')
            >>>
            >>> # Iterate over all results
            >>> for element in client.list_monitored_resource_descriptors(name):
            ...     # process element
            ...     pass
            >>>
            >>>
            >>> # Alternatively:
            >>>
            >>> # Iterate over results one page at a time
            >>> for page in client.list_monitored_resource_descriptors(name, options=CallOptions(page_token=INITIAL_PAGE)):
            ...     for element in page:
            ...         # process element
            ...         pass
   ...
   """

Worse, that usage is unidiomatic: the iterators returned from GAPIC methods have a pages attribute, which allows users to iterate pages without managing the token at all. E.g., for the example above, the more correct / idiomatic usage would be:

            >>> # Iterate over results one page at a time
            >>> for page in client.list_monitored_resource_descriptors(name).pages:
            ...     for element in page:
            ...         # process element
            ...         pass
tseaver commented 5 years ago

Note that those same docstrings lie about the return type of their methods:

        Returns:
            A :class:`~google.gax.PageIterator` instance.

They are returning instances of google.api_core.page_iterator.GRPCIterator.