Skip to content

Commit

Permalink
Opt: apply black formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Dec 21, 2022
1 parent 233d871 commit 5fd02b0
Show file tree
Hide file tree
Showing 17 changed files with 393 additions and 292 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ indent_size = 4
indent_size = 2

[*.py]
line_length = 119
line_length = 100
default_section = THIRDPARTY
known_first_party = pure_protobuf
not_skip = __init__.py
Expand Down
23 changes: 19 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ venv:
$(BIN)/pip install --upgrade pip wheel
$(BIN)/pip install -e.[dev]

.PHONY: lint
lint: lint/flake8 lint/isort lint/mypy

.PHONY: test check
test check: unittests lint

Expand All @@ -36,14 +33,32 @@ unittests:
benchmark:
$(BIN)/pytest --benchmark-only --benchmark-columns=mean,stddev,median,ops --benchmark-warmup=on tests

.PHONY: format
format: format/isort format/black

.PHONY: format/isort
format/isort:
$(BIN)/isort pure_protobuf tests

.PHONY: format/black
format/black:
$(BIN)/black pure_protobuf tests

.PHONY: lint
lint: lint/flake8 lint/isort lint/mypy lint/format

.PHONY: lint/flake8
lint/flake8:
$(BIN)/flake8 pure_protobuf tests

.PHONY: lint/isort
lint/isort:
$(BIN)/isort -c pure_protobuf tests
$(BIN)/isort -c --diff pure_protobuf tests

.PHONY: lint/mypy
lint/mypy:
$(BIN)/mypy pure_protobuf tests

.PHONY: lint/format
lint/format:
$(BIN)/black --check --diff pure_protobuf tests
88 changes: 57 additions & 31 deletions pure_protobuf/dataclasses_.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,48 @@
from collections import abc
from enum import IntEnum
from io import BytesIO
from typing import Any, ByteString, ClassVar, Dict, Iterable, List, Tuple, Type, TypeVar, Union, cast, get_type_hints
from typing import (
Any,
ByteString,
ClassVar,
Dict,
Iterable,
List,
Tuple,
Type,
TypeVar,
Union,
cast,
get_type_hints,
)

from pure_protobuf import serializers, types
from pure_protobuf.enums import WireType
from pure_protobuf.fields import Field, NonRepeatedField, OneOfPartField, PackedRepeatedField, UnpackedRepeatedField
from pure_protobuf.fields import (
Field,
NonRepeatedField,
OneOfPartField,
PackedRepeatedField,
UnpackedRepeatedField,
)
from pure_protobuf.io_ import IO
from pure_protobuf.oneof import OneOf_, OneOfPartInfo, scheme
from pure_protobuf.serializers import IntEnumSerializer, MessageSerializer, PackingSerializer, Serializer
from pure_protobuf.serializers import (
IntEnumSerializer,
MessageSerializer,
PackingSerializer,
Serializer,
)
from pure_protobuf.types import NoneType

try:
import dataclasses
except ImportError:
raise ImportError('dataclasses interface requires dataclasses support')
raise ImportError("dataclasses interface requires dataclasses support")


T = TypeVar('T')
TMessage = TypeVar('TMessage', bound='Message')
T = TypeVar("T")
TMessage = TypeVar("TMessage", bound="Message")


@dataclasses.dataclass
Expand Down Expand Up @@ -62,10 +86,14 @@ def merge_from(self: TMessage, other: TMessage):
Merge another message into the current one, as if with the ``Message::MergeFrom`` method.
"""
for field_ in self.__protobuf_fields__.values():
setattr(self, field_.name, field_.merge(
getattr(self, field_.name),
getattr(other, field_.name),
))
setattr(
self,
field_.name,
field_.merge(
getattr(self, field_.name),
getattr(other, field_.name),
),
)


def load(cls: Type[TMessage], io: IO) -> TMessage:
Expand All @@ -88,11 +116,7 @@ def _field(number: int, *args, packed=True, isoneof=False, **kwargs) -> Any:
Convenience function to assign field numbers.
Calls the standard ``dataclasses.field`` function with the metadata assigned.
"""
metadata = {
'number': number,
'packed': packed,
'isoneof': isoneof
}
metadata = {"number": number, "packed": packed, "isoneof": isoneof}

return dataclasses.field(*args, metadata=metadata, **kwargs)

Expand Down Expand Up @@ -131,7 +155,7 @@ def message(cls: Type[T]) -> Type[TMessage]:

Message.register(cls) # type: ignore
cls.serializer = MessageSerializer(cls) # type: ignore
cls.type_url = f'type.googleapis.com/{cls.__module__}.{cls.__name__}' # type: ignore
cls.type_url = f"type.googleapis.com/{cls.__module__}.{cls.__name__}" # type: ignore
cls.validate = Message.validate # type: ignore
cls.dump = Message.dump # type: ignore
cls.dumps = Message.dumps # type: ignore
Expand All @@ -144,14 +168,16 @@ def message(cls: Type[T]) -> Type[TMessage]:
casted_cls = cast(Type[TMessage], cls)
casted_cls.__protobuf_fields__ = {} # used to list all fields and locate fields by field number
for field_ in dataclasses.fields(cls):
if field_.metadata['isoneof']:
if field_.metadata["isoneof"]:
children = make_one_of_field(field_.default_factory(), field_.name) # type: ignore
casted_cls.__protobuf_fields__.update(children)
else:
num, proto_field = make_field(field_.metadata['number'],
field_.name,
type_hints[field_.name],
field_.metadata['packed'])
num, proto_field = make_field(
field_.metadata["number"],
field_.name,
type_hints[field_.name],
field_.metadata["packed"],
)
casted_cls.__protobuf_fields__[num] = proto_field

return casted_cls
Expand Down Expand Up @@ -199,7 +225,7 @@ def make_field(number: int, name: str, type_: Any, packed: bool = True) -> Tuple
try:
serializer = SERIALIZERS[type_]
except KeyError as e:
raise TypeError(f'type is not serializable: {type_}') from e
raise TypeError(f"type is not serializable: {type_}") from e

if not is_repeated:
# Non-repeated field.
Expand All @@ -218,13 +244,13 @@ def get_optional(type_: Any) -> Tuple[bool, Any]:
Extracts ``Optional`` type annotation if present.
This may be useful if a user wants to annotate a field with ``Optional[...]`` and set default to ``None``.
"""
if getattr(type_, '__origin__', None) is Union:
if getattr(type_, "__origin__", None) is Union:
args = set(type_.__args__)

# Check if it's a union of `NoneType` and something else.
if len(args) == 2 and NoneType in args:
# Extract inner type.
type_, = args - {NoneType}
(type_,) = args - {NoneType}
return True, type_

return False, type_
Expand All @@ -234,8 +260,8 @@ def get_repeated(type_: Any) -> Tuple[bool, Any]:
"""
Extracts ``repeated`` modifier if present.
"""
if getattr(type_, '__origin__', None) in (list, List, Iterable, abc.Iterable):
type_, = type_.__args__
if getattr(type_, "__origin__", None) in (list, List, Iterable, abc.Iterable):
(type_,) = type_.__args__
return True, type_
else:
return False, type_
Expand All @@ -262,9 +288,9 @@ def get_repeated(type_: Any) -> Tuple[bool, Any]:
}

__all__ = [
'field',
'load',
'loads',
'message',
'Message',
"field",
"load",
"loads",
"message",
"Message",
]
10 changes: 5 additions & 5 deletions pure_protobuf/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def dump(self, value: Any, io: IO):

def load(self, wire_type: WireType, io: IO) -> Any:
if wire_type != self.serializer.wire_type:
raise ValueError(f'expected {self.serializer.wire_type}, got {wire_type}')
raise ValueError(f"expected {self.serializer.wire_type}, got {wire_type}")
return self.serializer.load(io)

def merge(self, old_value: Any, new_value: Any) -> Any:
Expand All @@ -105,8 +105,9 @@ def load(self, wire_type: WireType, io: IO) -> Any:
return list(self.load_packed(bytes_serializer.load(io)))
if wire_type == self.serializer.wire_type:
return [self.serializer.load(io)]
raise ValueError((f'expected {self.serializer.wire_type} \n'
'or {WireType.BYTES}, got {wire_type}'))
raise ValueError(
(f"expected {self.serializer.wire_type} \n" "or {WireType.BYTES}, got {wire_type}")
)

def load_packed(self, bytes_: bytes) -> Iterable[Any]:
"""
Expand Down Expand Up @@ -152,8 +153,7 @@ def dump(self, value: Any, io: IO):


class OneOfPartField(Field):
def __init__(self, number: int, name: str,
scheme_: Tuple[OneOfPartInfo, ...], origin: Field):
def __init__(self, number: int, name: str, scheme_: Tuple[OneOfPartInfo, ...], origin: Field):
super().__init__(number, name, origin.serializer)

self.scheme = scheme_
Expand Down
11 changes: 5 additions & 6 deletions pure_protobuf/oneof.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class _OneOfAttrs(Enum):
FIELDS = "__fields__"


def scheme(obj: 'OneOf_') -> Tuple[OneOfPartInfo, ...]:
def scheme(obj: "OneOf_") -> Tuple[OneOfPartInfo, ...]:
return obj.__parts__


Expand All @@ -32,6 +32,7 @@ def inner(self, name, *args):
if name not in self.__fields__:
raise AttributeError(f"Field {name} is not found")
return func(self, name, *args)

return inner


Expand All @@ -40,6 +41,7 @@ class OneOf_:
Defines an oneof field.
See also: https://developers.google.com/protocol-buffers/docs/proto3#oneof
"""

def __init__(self, scheme_: Tuple[OneOfPartInfo, ...]):
# ugly sets to get round custom setattr
super().__setattr__(_OneOfAttrs.FIELDS.value, frozenset(part.name for part in scheme_))
Expand All @@ -59,8 +61,7 @@ def __delattr__(self, name):
if self.which_one_of == name:
return super().__setattr__(_OneOfAttrs.SET_VALUE.value, None)

raise AttributeError(f"Field {name} is not set, "
f"{self.which_one_of} is set")
raise AttributeError(f"Field {name} is not set, " f"{self.which_one_of} is set")

@_name_in_attrs_check
def __setattr__(self, name, value):
Expand All @@ -87,6 +88,4 @@ def __hash__(self) -> int:
# for debug purposes I guess
def __repr__(self) -> str:
fields, parts, set_value = _internals(self)
return (f"{fields} \n"
f"parts: {parts} \n"
f"set: {set_value}")
return f"{fields} \n" f"parts: {parts} \n" f"set: {set_value}"
Loading

0 comments on commit 5fd02b0

Please sign in to comment.