This is a constraint of current "grpc.StatusCode" design. Each enum is
constructed by an int and a str, e.g., (0, 'ok'). So, to convert literals
into enums, we need to write:

        grpc.StatusCode((5, 'not found'))

For a short-term fix, you may encapsulate an iterating function to map int
to enum, like:

        [x for x in grpc.StatusCode if x.value[0] == 5]

Do you think it would be helpful to support direct enum conversion? If we
add a new enum conversion feature, then the client library needs to bump up
the grpcio minimum version again. The conversion can be done without
regression via overriding the initialization method of "grpc.StatusCode".


On Mon, Oct 5, 2020 at 4:15 PM busunkim via grpc.io <
[email protected]> wrote:

> Hi,
>
> How do I convert the integer status code (5) into the enum (
> grpc.StatusCode.NOT_FOUND)?
>
>
> https://grpc.github.io/grpc/python/grpc.html#grpc-status-code
>
> I tried the following (see Programmatic access to enumeration members and
> their attributes
> <https://docs.python.org/3/library/enum.html#programmatic-access-to-enumeration-members-and-their-attributes>)
> but get an error.
> >>> import grpc
> >>> grpc.StatusCode(5)
> ValueError: 5 is not a valid StatusCode
>
> During handling of the above exception, another exception occurred:
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File
> "/usr/local/google/home/busunkim/.pyenv/versions/3.7.6/lib/python3.7/enum.py",
> line 310, in __call__
>     return cls.__new__(cls, value)
>   File
> "/usr/local/google/home/busunkim/.pyenv/versions/3.7.6/lib/python3.7/enum.py",
> line 564, in __new__
>     raise exc
>   File
> "/usr/local/google/home/busunkim/.pyenv/versions/3.7.6/lib/python3.7/enum.py",
> line 548, in __new__
>     result = cls._missing_(value)
>   File
> "/usr/local/google/home/busunkim/.pyenv/versions/3.7.6/lib/python3.7/enum.py",
> line 577, in _missing_
>     raise ValueError("%r is not a valid %s" % (value, cls.__name__))
> ValueError: 5 is not a valid StatusCode
>
>
> *Context*: I'd like turn a status code from a long running operation (
> google/longrunning/operations.proto
> <https://github.com/googleapis/googleapis/blob/cc0c4e7790fc57b3e720dfdd97b71ff85c85c911/google/longrunning/operations.proto#L128-L162>
> and google/rpc/status.proto
> <https://github.com/googleapis/googleapis/blob/cc0c4e7790fc57b3e720dfdd97b71ff85c85c911/google/rpc/status.proto#L35-L47>)
> into a grpc.StatusCode for nicer exceptions in google-api-core
> <https://github.com/googleapis/python-api-core>.
>
> An operation message looks something like this:
>
> name:
> "projects/my-project/instances/test-instance/databases/db2/operations/_auto_op_43eacb16d4b5d9da"
> metadata {
>   type_url: "
> type.googleapis.com/google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata
> "
>   value:
> "\nHprojects/my-project/instances/test-instance/databases/db2\0223CREATE
> TABLE Albums (\n  id INT64,\n) PRIMARY KEY(id)"
> }
> done: true
> error {
>   code: 9
>   message: "Duplicate name in schema: Albums."
> }
>
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups "
> grpc.io" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/grpc-io/375d3166-2c49-4fb4-becd-44bfbf7b9f3do%40googlegroups.com
> <https://groups.google.com/d/msgid/grpc-io/375d3166-2c49-4fb4-becd-44bfbf7b9f3do%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/grpc-io/CAMC1%3Djd5taKpfUDNbRNu53OaE6gdqr6E_B6HodAO%3DNMd_iqYgQ%40mail.gmail.com.

Reply via email to