8000 Use hasattr(x, "__iter__") rather than isinstance(x, Iterable) and fl… · Gobot1234/python-betterproto@0fa972c · GitHub
[go: up one dir, main page]

Skip to content

Commit 0fa972c

Browse files
committed
Use hasattr(x, "__iter__") rather than isinstance(x, Iterable) and float(-inf) works
1 parent 018ae74 commit 0fa972c

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

src/betterproto/__init__.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@
4343
)
4444
from .grpc.grpclib_client import ServiceStub
4545

46+
# Circular import workaround: google.protobuf depends on base classes defined above.
47+
from .lib.google.protobuf import DoubleValue # noqa
48+
from .lib.google.protobuf import (
49+
BoolValue,
50+
BytesValue,
51+
Duration,
52+
EnumValue,
53+
FloatValue,
54+
Int32Value,
55+
Int64Value,
56+
StringValue,
57+
Timestamp,
58+
UInt32Value,
59+
UInt64Value,
60+
)
61+
4662

4763
# Proto 3 data types
4864
TYPE_ENUM: Final = "enum"
@@ -454,7 +470,7 @@ def _parse_float(value: Any) -> float:
454470
if value == INFINITY:
455471
return float("inf")
456472
if value == NEG_INFINITY:
457-
return -float("inf")
473+
return float("-inf")
458474
if value == NAN:
459475
return float("nan")
460476
return float(value)
@@ -476,7 +492,7 @@ def _dump_float(value: float) -> Union[float, str]:
476492
"""
477493
if value == float("inf"):
478494
return INFINITY
479-
if value == -float("inf"):
495+
if value == float("-inf"):
480496
return NEG_INFINITY
481497
if value == float("nan"):
482498
return NAN
@@ -1162,9 +1178,7 @@ def to_dict(
11621178
elif meta.proto_type == TYPE_ENUM:
11631179
if field_is_repeated:
11641180
enum_class = field_types[field_name].__args__[0]
1165-
if isinstance(value, typing.Iterable) and not isinstance(
1166-
value, str
1167-
):
1181+
if hasattr(value, "__iter__") and not isinstance(value, str):
11681182
output[cased_name] = [enum_class(el).name for el in value]
11691183
else:
11701184
# transparently upgrade single value to repeated
@@ -1362,23 +1376,6 @@ def which_one_of(message: Message, group_name: str) -> Tuple[str, Optional[Any]]
13621376
return field_name, getattr(message, field_name)
13631377

13641378

1365-
# Circular import workaround: google.protobuf depends on base classes defined above.
1366-
from .lib.google.protobuf import ( # noqa
1367-
BoolValue,
1368-
BytesValue,
1369< 8FB9 /td>-
DoubleValue,
1370-
Duration,
1371-
EnumValue,
1372-
FloatValue,
1373-
Int32Value,
1374-
Int64Value,
1375-
StringValue,
1376-
Timestamp,
1377-
UInt32Value,
1378-
UInt64Value,
1379-
)
1380-
1381-
13821379
class _Duration(Duration):
13831380
def to_timedelta(self) -> timedelta:
13841381
return timedelta(seconds=self.seconds, microseconds=self.nanos / 1e3)

0 commit comments

Comments
 (0)
0