You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
See protocolbuffers/protobuf#8182 which
will make it into protobuf 3.20.0
Have to use a `V = Union[ValueType]` hax, because
`V = ValueType` is interpreted as a value rather than alias
`V: TypeAlias = ValueType` appears buggy within nested scopes.
Fixes#169
Copy file name to clipboardExpand all lines: README.md
+21-18Lines changed: 21 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -87,32 +87,33 @@ enum MyEnum {
87
87
BAR = 1;
88
88
}
89
89
```
90
-
Will yield an [enum type wrapper](https://github.com/python/typeshed/blob/16ae4c61201cd8b96b8b22cdfb2ab9e89ba5bcf2/stubs/protobuf/google/protobuf/internal/enum_type_wrapper.pyi) whose methods type to `MyEnum.V` rather than `int`.
90
+
Will yield an [enum type wrapper](https://github.com/python/typeshed/blob/16ae4c61201cd8b96b8b22cdfb2ab9e89ba5bcf2/stubs/protobuf/google/protobuf/internal/enum_type_wrapper.pyi) whose methods type to `MyEnum.ValueType` (a `NewType(int)` rather than `int`.
91
91
This allows mypy to catch bugs where the wrong enum value is being used.
92
92
93
93
Calling code may be typed as follows.
94
94
95
95
In python >= 3.7
96
96
```python
97
-
#Need [PEP 563](https://www.python.org/dev/peps/pep-0563/) to postpone evaluation of annotations
98
-
from__future__import annotations # Not needed with python>=3.10
99
-
deff(x: MyEnum.V):
97
+
#May need [PEP 563](https://www.python.org/dev/peps/pep-0563/) to postpone evaluation of annotations
98
+
#from __future__ import annotations # Not needed with python>=3.10 or protobuf>=3.20.0
99
+
deff(x: MyEnum.ValueType):
100
100
print(x)
101
101
f(MyEnum.Value("FOO"))
102
102
```
103
103
104
-
For usages of cast, the type of `x` must be quoted
105
-
until [upstream protobuf](https://github.com/protocolbuffers/protobuf/pull/8182) includes `V`
104
+
With protobuf <= 3.20.0, for usages of cast, the type of `x` must be quoted
105
+
After protobuf >= 3.20.0 - `ValueType` exists in the python code and quotes aren't needed
106
+
until [upstream protobuf](https://github.com/protocolbuffers/protobuf/pull/8182) includes `ValueType`
106
107
```python
107
-
cast('MyEnum.V', x)
108
+
cast('MyEnum.ValueType', x)
108
109
```
109
110
110
-
Similarly, for type aliases, you must either quote the type or hide it behind `TYPE_CHECKING`
111
+
Similarly, for type aliases with protobuf < 3.20.0, you must either quote the type or hide it behind `TYPE_CHECKING`
0 commit comments