The split enum value types break type checks when used with dataclasses
See original GitHub issueChanges 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:
- Created 3 years ago
- Reactions:2
- Comments:11 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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:
Previously I was able to use without the quotes but not anymore
cool - I will close this out and make a task to add a py3-syntax test.