question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The split enum value types break type checks when used with dataclasses

See original GitHub issue

Changes made in https://github.com/dropbox/mypy-protobuf/pull/120 are breaking typechecks when compiled types are used with dataclasses, or in general when enums are used as parameters or return values.

Quick example: proto:

syntax = "proto3";

package bikes;
    
enum EngineType {
    TWO_STROKE = 0;
    FOUR_STROKE = 1;
}

message Motorcycle {
    string name = 1;
    EngineType engine_type = 2;
}

python:

from dataclasses import dataclass
from motorcycles_pb2 import Engine, EngineType, Motorcycle

@dataclass
class DemoEngine:
    engine_type: EngineType
    name: str

    def __init__(self, engine_type: EngineType, name: str):
        self.engine_type = engine_type
        self.name = name

d = DemoEngine(EngineType.TWO_STROKE, "demo")

type check failure:

motorcycles/__main__.py:17: error: Argument 1 to "DemoEngine" has incompatible type "EngineTypeValue"; expected "EngineType"

Protos compiled with protoc 3.12.2 using python -m grpc_tools.protoc

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
pcorpetcommented, Jun 8, 2020

@nipunn1313 In your PR you’re only using comments for types. As such all you use are strings (they are never considered as actual Python object). What I use is full typing annotations but using strings:

e: 'test_pb2.OuterEnumValue' = test_pb2.BAR

Previously I was able to use without the quotes but not anymore

e: test_pb2.OuterEnum = test_pb2.BAR
0reactions
nipunn1313commented, Jun 11, 2020

cool - I will close this out and make a task to add a py3-syntax test.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The split enum value types break type checks when used with ...
Changes made in #120 are breaking typechecks when compiled types are used with dataclasses, or in general when enums are used as parameters ......
Read more >
python - Typing dataclass that can only take enum values
Dataclasses don't do any type-checking; the type hint only serves to mark the name as something dataclass should pay attention to. – chepner....
Read more >
Data Classes in Python 3.7+ (Guide)
Beneath the class Position: line, you simply list the fields you want in your data class. The : notation used for the fields...
Read more >
flytekit.core.type_engine - Flyte
Any: """ If any field inside the dataclass is flyte type, we should use flyte type transformer for that field. """ from flytekit.types.directory.types...
Read more >
Enum HOWTO — Python 3.11.1 documentation
An Enum is a set of symbolic names bound to unique values. They are similar to global variables, but they offer a more...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found