danielgtaylor / danielgtaylor/python-betterproto

RecursionError on import when using message with a field named "str"

Open
#471 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Python
Stars
1.8k
Forks
234
PR merge metrics
No merged PRs in 30d

Description

Hello,

We are using betterproto with the NI-XNET gRPC Server. (For Reference: https://github.com/ni/grpc-device)
They use some messages which have a field with name "str".
This causes a RecursionError on import of the generated python file.
(The recursion happens inside the dataclass library, presumabely since the str builtin gets shadowed.)

Example message spec:
```
message GetPropertyResponse {
int32 status = 1;
oneof property_value {
uint32 u32_scalar = 2;
bool bool_scalar = 3;
string str = 4;
uint64 u64_scalar = 5;
int32 i32_scalar = 6;
double f64_scalar = 7;
}
}
```

Generated python code:
```python
@dataclass
class GetPropertyResponse(betterproto.Message):
status: int = betterproto.int32_field(1)
u32_scalar: int = betterproto.uint32_field(2, group="property_value")
bool_scalar: bool = betterproto.bool_field(3, group="property_value")
str: str = betterproto.string_field(4, group="property_value")
u64_scalar: int = betterproto.uint64_field(5, group="property_value")
i32_scalar: int = betterproto.int32_field(6, group="property_value")
f64_scalar: float = betterproto.double_field(7, group="property_value")
```

Full Traceback:
```
Traceback (most recent call last):
File "C:\...\.py", line 8, in
from import
File "C:\...\.py", line 11, in
from import nixnet_grpc
File "C:\...\nixnet_grpc.py", line 1018, in
class GetPropertyResponse(betterproto.Message):
File "C:\...\Python\Python39\lib\dataclasses.py", line 1021, in dataclass
return wrap(cls)
File "C:\...\Python\Python39\lib\dataclasses.py", line 1013, in wrap
return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen)
File "C:\...\Python\Python39\lib\dataclasses.py", line 993, in _process_class
str(inspect.signature(cls)).replace(' -> None', ''))
File "C:\...\Python\Python39\lib\inspect.py", line 3087, in __str__
formatted = str(param)
File "C:\...\Python\Python39\lib\inspect.py", line 2606, in __str__
formatannotation(self._annotation))
File "C:\...\Python\Python39\lib\inspect.py", line 1240, in formatannotation
return repr(annotation)
File "C:\...\Python\Python39\lib\dataclasses.py", line 260, in __repr__
return ('Field('
File "C:\...\Python\Python39\lib\dataclasses.py", line 260, in __repr__
return ('Field('
File "C:\...\Python\Python39\lib\dataclasses.py", line 260, in __repr__
return ('Field('
[Previous line repeated 482 more times]
RecursionError: maximum recursion depth exceeded
```

We are not using this field actively so I was able to resolve the issue temporarily by renaming it to "str_" in the generated files.
I have not tested whether this causes issues when actually using the field.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.