Skip to content

Commit

Permalink
chore: use black==22.3.0 (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
parthea authored Apr 26, 2022
1 parent 3a49154 commit cf3240f
Show file tree
Hide file tree
Showing 28 changed files with 243 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
with:
python-version: 3.8
- name: Install black
run: pip install black==19.10b0
run: pip install black==22.3.0
- name: Check diff
run: black --diff --check .
docs:
Expand Down
4 changes: 3 additions & 1 deletion proto/_file_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def maybe_add_descriptor(cls, filename, package):
if not descriptor:
descriptor = cls.registry[filename] = cls(
descriptor=descriptor_pb2.FileDescriptorProto(
name=filename, package=package, syntax="proto3",
name=filename,
package=package,
syntax="proto3",
),
enums=collections.OrderedDict(),
messages=collections.OrderedDict(),
Expand Down
2 changes: 1 addition & 1 deletion proto/datetime_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def from_rfc3339(cls, stamp):
nanos = 0
else:
scale = 9 - len(fraction)
nanos = int(fraction) * (10 ** scale)
nanos = int(fraction) * (10**scale)
return cls(
bare.year,
bare.month,
Expand Down
6 changes: 4 additions & 2 deletions proto/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def descriptor(self):
if isinstance(self.message, str):
if not self.message.startswith(self.package):
self.message = "{package}.{name}".format(
package=self.package, name=self.message,
package=self.package,
name=self.message,
)
type_name = self.message
elif self.message:
Expand All @@ -90,7 +91,8 @@ def descriptor(self):
elif isinstance(self.enum, str):
if not self.enum.startswith(self.package):
self.enum = "{package}.{name}".format(
package=self.package, name=self.enum,
package=self.package,
name=self.enum,
)
type_name = self.enum
elif self.enum:
Expand Down
3 changes: 2 additions & 1 deletion proto/marshal/marshal.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ def to_proto(self, proto_type, value, *, strict: bool = False):
raise TypeError(
"Parameter must be instance of the same class; "
"expected {expected}, got {got}".format(
expected=proto_type.__name__, got=pb_value.__class__.__name__,
expected=proto_type.__name__,
got=pb_value.__class__.__name__,
),
)

Expand Down
3 changes: 2 additions & 1 deletion proto/marshal/rules/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def to_proto(self, value) -> timestamp_pb2.Timestamp:
return value.timestamp_pb()
if isinstance(value, datetime):
return timestamp_pb2.Timestamp(
seconds=int(value.timestamp()), nanos=value.microsecond * 1000,
seconds=int(value.timestamp()),
nanos=value.microsecond * 1000,
)
return value

Expand Down
3 changes: 2 additions & 1 deletion proto/marshal/rules/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def to_python(self, value, *, absent: bool = None):
# the user realizes that an unexpected value came along.
warnings.warn(
"Unrecognized {name} enum value: {value}".format(
name=self._enum.__name__, value=value,
name=self._enum.__name__,
value=value,
)
)
return value
Expand Down
12 changes: 9 additions & 3 deletions proto/marshal/rules/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ def to_python(self, value, *, absent: bool = None):
return str(value.string_value)
if kind == "struct_value":
return self._marshal.to_python(
struct_pb2.Struct, value.struct_value, absent=False,
struct_pb2.Struct,
value.struct_value,
absent=False,
)
if kind == "list_value":
return self._marshal.to_python(
struct_pb2.ListValue, value.list_value, absent=False,
struct_pb2.ListValue,
value.list_value,
absent=False,
)
# If more variants are ever added, we want to fail loudly
# instead of tacitly returning None.
Expand Down Expand Up @@ -126,7 +130,9 @@ def to_proto(self, value) -> struct_pb2.Struct:
if isinstance(value, struct_pb2.Struct):
return value
if isinstance(value, maps.MapComposite):
return struct_pb2.Struct(fields={k: v for k, v in value.pb.items()},)
return struct_pb2.Struct(
fields={k: v for k, v in value.pb.items()},
)

# We got a dict (or something dict-like); convert it.
answer = struct_pb2.Struct(
Expand Down
37 changes: 29 additions & 8 deletions proto/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ def __new__(mcls, name, bases, attrs):
# Determine the name of the entry message.
msg_name = "{pascal_key}Entry".format(
pascal_key=re.sub(
r"_\w", lambda m: m.group()[1:].upper(), key,
r"_\w",
lambda m: m.group()[1:].upper(),
key,
).replace(key[0], key[0].upper(), 1),
)

Expand All @@ -83,20 +85,26 @@ def __new__(mcls, name, bases, attrs):
{
"__module__": attrs.get("__module__", None),
"__qualname__": "{prefix}.{name}".format(
prefix=attrs.get("__qualname__", name), name=msg_name,
prefix=attrs.get("__qualname__", name),
name=msg_name,
),
"_pb_options": {"map_entry": True},
}
)
entry_attrs["key"] = Field(field.map_key_type, number=1)
entry_attrs["value"] = Field(
field.proto_type, number=2, enum=field.enum, message=field.message,
field.proto_type,
number=2,
enum=field.enum,
message=field.message,
)
map_fields[msg_name] = MessageMeta(msg_name, (Message,), entry_attrs)

# Create the repeated field for the entry message.
map_fields[key] = RepeatedField(
ProtoType.MESSAGE, number=field.number, message=map_fields[msg_name],
ProtoType.MESSAGE,
number=field.number,
message=map_fields[msg_name],
)

# Add the new entries to the attrs
Expand Down Expand Up @@ -312,7 +320,13 @@ def pb(cls, obj=None, *, coerce: bool = False):
if coerce:
obj = cls(obj)
else:
raise TypeError("%r is not an instance of %s" % (obj, cls.__name__,))
raise TypeError(
"%r is not an instance of %s"
% (
obj,
cls.__name__,
)
)
return obj._pb

def wrap(cls, pb):
Expand Down Expand Up @@ -478,7 +492,11 @@ class Message(metaclass=MessageMeta):
"""

def __init__(
self, mapping=None, *, ignore_unknown_fields=False, **kwargs,
self,
mapping=None,
*,
ignore_unknown_fields=False,
**kwargs,
):
# We accept several things for `mapping`:
# * An instance of this class.
Expand Down Expand Up @@ -519,7 +537,10 @@ def __init__(
# Sanity check: Did we get something not a map? Error if so.
raise TypeError(
"Invalid constructor input for %s: %r"
% (self.__class__.__name__, mapping,)
% (
self.__class__.__name__,
mapping,
)
)

params = {}
Expand Down Expand Up @@ -564,7 +585,7 @@ def __init__(
super().__setattr__("_pb", self._meta.pb(**params))

def _get_pb_type_from_key(self, key):
"""Given a key, return the corresponding pb_type.
"""Given a key, return the corresponding pb_type.
Args:
key(str): The name of the field.
Expand Down
9 changes: 7 additions & 2 deletions proto/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@


_ProtoModule = collections.namedtuple(
"ProtoModule", ["package", "marshal", "manifest"],
"ProtoModule",
["package", "marshal", "manifest"],
)


Expand All @@ -39,7 +40,11 @@ def define_module(
"""
if not marshal:
marshal = package
return _ProtoModule(package=package, marshal=marshal, manifest=frozenset(manifest),)
return _ProtoModule(
package=package,
marshal=marshal,
manifest=frozenset(manifest),
)


__all__ = ("define_module",)
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@
platforms="Posix; MacOS X",
include_package_data=True,
install_requires=("protobuf >= 3.19.0",),
extras_require={"testing": ["google-api-core[grpc] >= 1.22.2",],},
extras_require={
"testing": [
"google-api-core[grpc] >= 1.22.2",
],
},
python_requires=">=3.6",
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand Down
8 changes: 7 additions & 1 deletion tests/clam.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@

import proto

__protobuf__ = proto.module(package="ocean.clam.v1", manifest={"Clam", "Species",},)
__protobuf__ = proto.module(
package="ocean.clam.v1",
manifest={
"Clam",
"Species",
},
)


class Species(proto.Enum):
Expand Down
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ def _register_messages(scope, iterable, sym_db):
"""Create and register messages from the file descriptor."""
for name, descriptor in iterable.items():
new_msg = reflection.GeneratedProtocolMessageType(
name, (message.Message,), {"DESCRIPTOR": descriptor, "__module__": None},
name,
(message.Message,),
{"DESCRIPTOR": descriptor, "__module__": None},
)
sym_db.RegisterMessage(new_msg)
setattr(scope, name, new_msg)
Expand Down
7 changes: 6 additions & 1 deletion tests/enums_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

import proto

__protobuf__ = proto.module(package="test.proto", manifest={"Enums",},)
__protobuf__ = proto.module(
package="test.proto",
manifest={
"Enums",
},
)


class OneEnum(proto.Enum):
Expand Down
7 changes: 6 additions & 1 deletion tests/mollusc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
import proto
import zone

__protobuf__ = proto.module(package="ocean.mollusc.v1", manifest={"Mollusc",},)
__protobuf__ = proto.module(
package="ocean.mollusc.v1",
manifest={
"Mollusc",
},
)


class Mollusc(proto.Message):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_fields_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ class Foo(proto.Message):
big = proto.Field(proto.INT64, number=2)

foo = Foo()
foo.big = 2 ** 40
assert foo.big == 2 ** 40
foo.big = 2**40
assert foo.big == 2**40
with pytest.raises(ValueError):
foo.small = 2 ** 40
foo.small = 2**40
with pytest.raises(ValueError):
Foo(small=2 ** 40)
Foo(small=2**40)


def test_int_unsigned():
Expand Down
35 changes: 30 additions & 5 deletions tests/test_fields_map_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ class Foo(proto.Message):
bar = proto.Field(proto.INT32, number=1)

class Baz(proto.Message):
foos = proto.MapField(proto.STRING, proto.MESSAGE, number=1, message=Foo,)
foos = proto.MapField(
proto.STRING,
proto.MESSAGE,
number=1,
message=Foo,
)

baz = Baz(foos={"i": Foo(bar=42), "j": Foo(bar=24)})
assert len(baz.foos) == 2
Expand All @@ -36,7 +41,12 @@ class Foo(proto.Message):
bar = proto.Field(proto.INT32, number=1)

class Baz(proto.Message):
foos = proto.MapField(proto.STRING, proto.MESSAGE, number=1, message=Foo,)
foos = proto.MapField(
proto.STRING,
proto.MESSAGE,
number=1,
message=Foo,
)

baz = Baz(foos={"i": {"bar": 42}, "j": {"bar": 24}})
assert len(baz.foos) == 2
Expand All @@ -52,7 +62,12 @@ class Foo(proto.Message):
bar = proto.Field(proto.INT32, number=1)

class Baz(proto.Message):
foos = proto.MapField(proto.STRING, proto.MESSAGE, number=1, message=Foo,)
foos = proto.MapField(
proto.STRING,
proto.MESSAGE,
number=1,
message=Foo,
)

baz = Baz()
baz.foos["i"] = Foo(bar=42)
Expand All @@ -68,7 +83,12 @@ class Foo(proto.Message):
bar = proto.Field(proto.INT32, number=1)

class Baz(proto.Message):
foos = proto.MapField(proto.STRING, proto.MESSAGE, number=1, message=Foo,)
foos = proto.MapField(
proto.STRING,
proto.MESSAGE,
number=1,
message=Foo,
)

baz = Baz()
baz.foos["i"] = Foo()
Expand All @@ -82,7 +102,12 @@ class Foo(proto.Message):
bar = proto.Field(proto.INT32, number=1)

class Baz(proto.Message):
foos = proto.MapField(proto.STRING, proto.MESSAGE, number=1, message=Foo,)
foos = proto.MapField(
proto.STRING,
proto.MESSAGE,
number=1,
message=Foo,
)

baz = Baz()
baz.foos["i"] = Foo(bar=42)
Expand Down
4 changes: 3 additions & 1 deletion tests/test_fields_repeated_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ class Baz(proto.Message):
def test_repeated_composite_marshaled():
class Foo(proto.Message):
timestamps = proto.RepeatedField(
proto.MESSAGE, message=timestamp_pb2.Timestamp, number=1,
proto.MESSAGE,
message=timestamp_pb2.Timestamp,
number=1,
)

foo = Foo(
Expand Down
4 changes: 3 additions & 1 deletion tests/test_file_info_salting.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ def sample_file_info(name):
filename,
_file_info._FileInfo(
descriptor=descriptor_pb2.FileDescriptorProto(
name=filename, package=package, syntax="proto3",
name=filename,
package=package,
syntax="proto3",
),
enums=collections.OrderedDict(),
messages=collections.OrderedDict(),
Expand Down
Loading

0 comments on commit cf3240f

Please sign in to comment.