8000 correct type hinting to alleviate mypy failures during CI/CD checks. · Issue #649 · googleapis/python-datastore · GitHub
[go: up one dir, main page]

Skip to content

correct type hinting to alleviate mypy failures during CI/CD checks. #649

@chalmerlowe

Description

@chalmerlowe

Correct the type hinting to reflect all the possible types of the variable request.

Background

When performing the mypy CI/CD checks, mypy announces a series of notes (~12) and errors (~8).
While the notes don't cause mypy to fail, the error conditions DO cause the nox session to fail. See traceback below.

I only looked at one error (i.e. near line 1722) and found this situation.

the variable request is type hinted as Optional which means it can be a ListOperationsRequest object or None.

However in the code, we first check to see if request is a dict and if yes we convert it to a ListOperationsRequest. If it is None nothing happens and it remains None.

A few lines later, we then try to use the .name attribute on requests incorrectly presuming it is now a ListOperationsRequest when mypy is asserting that it could still be a NoneType object. And thus the Error. (Presuming the remainder of the errors are similar.)

Code

    def list_operations(
        self,
        request: Optional[operations_pb2.ListOperationsRequest] = None,
        *,
        retry: OptionalRetry = gapic_v1.method.DEFAULT,
        timeout: Union[float, object] = gapic_v1.method.DEFAULT,
        metadata: Sequence[Tuple[str, Union[str, bytes]]] = (),
    ) -> operations_pb2.ListOperationsResponse:
        r"""Lists operations that match the specified filter in the request.

        Args:
            request (:class:`~.operations_pb2.ListOperationsRequest`):
                The request object. Request message for
                `ListOperations` method.
            retry (google.api_core.retry.Retry): Designation of what errors,
                    if any, should be retried.
            timeout (float): The timeout for this request.
            metadata (Sequence[Tuple[str, Union[str, bytes]]]): Key/value pairs which should be
                sent along with the request as metadata. Normally, each value must be of type `str`,
                but for metadata keys ending with the suffix `-bin`, the corresponding values must
                be of type `bytes`.
        Returns:
            ~.operations_pb2.ListOperationsResponse:
                Response message for ``ListOperations`` method.
        """
        # Create or coerce a protobuf request object.
        # The request isn't a proto-plus wrapped type,
        # so it must be constructed via keyword expansion.
        if isinstance(request, dict):
            request = operations_pb2.ListOperationsRequest(**request)


        # Wrap the RPC method; this adds retry and timeout information,
        # and friendly error handling.
        rpc = self._transport._wrapped_methods[self._transport.list_operations]


        # Certain fields should be provided within the metadata header;
        # add these here.
        metadata = tuple(metadata) + (
            gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)),
        )

mypy Traceback

nox > Running session mypy
nox > Creating virtual environment (virtualenv) using python3.10 in .nox/mypy
nox > python -m pip install -e .
nox > python -m pip install mypy types-setuptools types-mock 'types-protobuf!=4.24.0.20240106' types-requests
nox > mypy -p google.cloud.datastore
google/cloud/datastore_v1/services/datastore/transports/rest_base.py:109: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
google/cloud/datastore_v1/services/datastore/transports/rest_base.py:166: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
google/cloud/datastore_v1/services/datastore/transports/rest_base.py:223: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
google/cloud/datastore_v1/services/datastore/transports/rest_base.py:280: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
google/cloud/datastore_v1/services/datastore/transports/rest_base.py:337: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
google/cloud/datastore_v1/services/datastore/transports/rest_base.py:394: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
google/cloud/datastore_v1/services/datastore/transports/rest_base.py:451: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
google/cloud/datastore_v1/services/datastore/transports/rest_base.py:508: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
google/cloud/datastore_v1/services/datastore/transports/rest_base.py:555: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
google/cloud/datastore_v1/services/datastore/transports/rest_base.py:580: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
google/cloud/datastore_v1/services/datastore/transports/rest_base.py:605: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
google/cloud/datastore_v1/services/datastore/transports/rest_base.py:630: note: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs  [annotation-unchecked]
google/cloud/datastore_v1/services/datastore/client.py:1722: error: Item "None" of "Any | None" has no attribute "name"  [union-attr]
google/cloud/datastore_v1/services/datastore/client.py:1781: error: Item "None" of "Any | None" has no attribute "name"  [union-attr]
google/cloud/datastore_v1/services/datastore/client.py:1844: error: Item "None" of "Any | None" has no attribute "name"  [union-attr]
google/cloud/datastore_v1/services/datastore/client.py:1899: error: Item "None" of "Any | None" has no attribute "name"  [union-attr]
google/cloud/datastore_v1/services/datastore/async_client.py:1322: error: Item "None" of "Any | None" has no attribute "name"  [union-attr]
google/cloud/datastore_v1/services/datastore/async_client.py:1377: error: Item "None" of "Any | None" has no attribute "name"  [union-attr]
google/cloud/datastore_v1/services/datastore/async_client.py:1436: error: Item "None" of "Any | None" has no attribute "name"  [union-attr]
google/cloud/datastore_v1/services/datastore/async_client.py:1491: error: Item "None" of "Any | None" has no attribute "name"  [union-attr]
Found 8 errors in 2 files (checked 15 source files)
nox > Command mypy -p google.cloud.datastore failed with exit code 1
nox > Session mypy failed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api: datastoreIssues related to the googleapis/python-datastore API.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0