googleapis / gapic-generator-python

Generate Python API client libraries from Protocol Buffers.
Apache License 2.0
122 stars 69 forks source link

Wrong type annotation for `metadata`, instead of `Sequence[Tuple[str, str]]`, should be `Sequence[Tuple[str, Union[str, bytes]]]` #2250

Closed gabrysiaolsz closed 2 days ago

gabrysiaolsz commented 1 week ago

Environment details

Steps to reproduce

  1. Install pip install google-cloud-datacatalog-lineage==0.3.9 (as a quick way of getting a generated client)
  2. Create a generated LineageClient
  3. Annotated type for metadata in API methods is Sequence[Tuple[str, str]], but in reality it should be Sequence[Tuple[str, Union[str, bytes]]].

Failing Code example

Sending a pair of str, when the first one has a suffix of "-bin" causes errors:

client.process_open_lineage_run_event(
    request,
    metadata=[
        ('x-goog-ext-512598505-bin', '\n\x08BIGQUERY\x12\x05LOAD1'),
    ]
)

Stack trace

E   TypeError: Binary metadata key="x-goog-ext-512598505-bin" expected bytes, got <class 'str'>

src/python/grpcio/grpc/_cython/_cygrpc/metadata.pyx.pxi:45: TypeError

Working Code example

Sending a pair of str, bytes succeeds:

client.process_open_lineage_run_event(
    request,
    metadata=[
        ('x-goog-ext-512598505-bin', b'\n\x08BIGQUERY\x12\x05LOAD1'),
    ]
)

So the type annotation should be Sequence[Tuple[str, Union[str, bytes]]], to avoid confusion and time wasted on debbuging. The examples here also show to send str, bytes.