Skip to content

Commit 1105c58

Browse files
reformatting applied
1 parent 59f72fa commit 1105c58

File tree

6 files changed

+77
-33
lines changed

6 files changed

+77
-33
lines changed

src/surrealdb/cbor2/_decoder.py

+29-10
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
T = TypeVar("T")
3131

3232
timestamp_re = re.compile(
33-
r"^(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)" r"(?:\.(\d{1,6})\d*)?(?:Z|([+-])(\d\d):(\d\d))$"
33+
r"^(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)"
34+
r"(?:\.(\d{1,6})\d*)?(?:Z|([+-])(\d\d):(\d\d))$"
3435
)
3536
incremental_utf8_decoder = getincrementaldecoder("utf-8")
3637

@@ -141,7 +142,9 @@ def object_hook(self) -> Callable[[CBORDecoder, dict[Any, Any]], Any] | None:
141142
return self._object_hook
142143

143144
@object_hook.setter
144-
def object_hook(self, value: Callable[[CBORDecoder, Mapping[Any, Any]], Any] | None) -> None:
145+
def object_hook(
146+
self, value: Callable[[CBORDecoder, Mapping[Any, Any]], Any] | None
147+
) -> None:
145148
if value is None or callable(value):
146149
self._object_hook = value
147150
else:
@@ -253,9 +256,13 @@ def decode_from_bytes(self, buf: bytes) -> object:
253256
def _decode_length(self, subtype: int) -> int: ...
254257

255258
@overload
256-
def _decode_length(self, subtype: int, allow_indefinite: Literal[True]) -> int | None: ...
259+
def _decode_length(
260+
self, subtype: int, allow_indefinite: Literal[True]
261+
) -> int | None: ...
257262

258-
def _decode_length(self, subtype: int, allow_indefinite: bool = False) -> int | None:
263+
def _decode_length(
264+
self, subtype: int, allow_indefinite: bool = False
265+
) -> int | None:
259266
if subtype < 24:
260267
return subtype
261268
elif subtype == 24:
@@ -269,7 +276,9 @@ def _decode_length(self, subtype: int, allow_indefinite: bool = False) -> int |
269276
elif subtype == 31 and allow_indefinite:
270277
return None
271278
else:
272-
raise CBORDecodeValueError(f"unknown unsigned integer subtype 0x{subtype:x}")
279+
raise CBORDecodeValueError(
280+
f"unknown unsigned integer subtype 0x{subtype:x}"
281+
)
273282

274283
def decode_uint(self, subtype: int) -> int:
275284
# Major tag 0
@@ -304,7 +313,9 @@ def decode_bytestring(self, subtype: int) -> bytes:
304313
)
305314
else:
306315
if length > sys.maxsize:
307-
raise CBORDecodeValueError(f"invalid length for bytestring 0x{length:x}")
316+
raise CBORDecodeValueError(
317+
f"invalid length for bytestring 0x{length:x}"
318+
)
308319
elif length <= 65536:
309320
result = self.read(length)
310321
else:
@@ -356,11 +367,15 @@ def decode_string(self, subtype: int) -> str:
356367
try:
357368
value = self.read(length).decode("utf-8", self._str_errors)
358369
except UnicodeDecodeError as exc:
359-
raise CBORDecodeValueError("error decoding unicode string") from exc
370+
raise CBORDecodeValueError(
371+
"error decoding unicode string"
372+
) from exc
360373

361374
buf.append(value)
362375
else:
363-
raise CBORDecodeValueError("non-string found in indefinite length string")
376+
raise CBORDecodeValueError(
377+
"non-string found in indefinite length string"
378+
)
364379
else:
365380
if length > sys.maxsize:
366381
raise CBORDecodeValueError(f"invalid length for string 0x{length:x}")
@@ -381,7 +396,9 @@ def decode_string(self, subtype: int) -> str:
381396
try:
382397
result += codec.decode(self.read(chunk_size), final)
383398
except UnicodeDecodeError as exc:
384-
raise CBORDecodeValueError("error decoding unicode string") from exc
399+
raise CBORDecodeValueError(
400+
"error decoding unicode string"
401+
) from exc
385402

386403
left -= chunk_size
387404

@@ -619,7 +636,9 @@ def decode_sharedref(self) -> Any:
619636
raise CBORDecodeValueError("shared reference %d not found" % value)
620637

621638
if shared is None:
622-
raise CBORDecodeValueError("shared value %d has not been initialized" % value)
639+
raise CBORDecodeValueError(
640+
"shared value %d has not been initialized" % value
641+
)
623642
else:
624643
return shared
625644

src/surrealdb/cbor2/_encoder.py

+28-11
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,19 @@ def __init__(
179179
self.string_namespacing = string_referencing
180180
self.default = default
181181
self._canonical = canonical
182-
self._shared_containers: dict[
183-
int, tuple[object, int | None]
184-
] = {} # indexes used for value sharing
185-
self._string_references: dict[str | bytes, int] = {} # indexes used for string references
182+
self._shared_containers: dict[int, tuple[object, int | None]] = (
183+
{}
184+
) # indexes used for value sharing
185+
self._string_references: dict[str | bytes, int] = (
186+
{}
187+
) # indexes used for string references
186188
self._encoders = default_encoders.copy()
187189
if canonical:
188190
self._encoders.update(canonical_encoders)
189191

190-
def _find_encoder(self, obj_type: type) -> Callable[[CBOREncoder, Any], None] | None:
192+
def _find_encoder(
193+
self, obj_type: type
194+
) -> Callable[[CBOREncoder, Any], None] | None:
191195
for type_or_tuple, enc in list(self._encoders.items()):
192196
if type(type_or_tuple) is tuple:
193197
try:
@@ -306,7 +310,11 @@ def encode(self, obj: Any) -> None:
306310
the object to encode
307311
"""
308312
obj_type = obj.__class__
309-
encoder = self._encoders.get(obj_type) or self._find_encoder(obj_type) or self._default
313+
encoder = (
314+
self._encoders.get(obj_type)
315+
or self._find_encoder(obj_type)
316+
or self._default
317+
)
310318
if not encoder:
311319
raise CBOREncodeTypeError(f"cannot serialize type {obj_type.__name__}")
312320

@@ -327,15 +335,19 @@ def encode_to_bytes(self, obj: Any) -> bytes:
327335
self.fp = old_fp
328336
return fp.getvalue()
329337

330-
def encode_container(self, encoder: Callable[[CBOREncoder, Any], Any], value: Any) -> None:
338+
def encode_container(
339+
self, encoder: Callable[[CBOREncoder, Any], Any], value: Any
340+
) -> None:
331341
if self.string_namespacing:
332342
# Create a new string reference domain
333343
self.encode_length(6, 256)
334344

335345
with self.disable_string_namespacing():
336346
self.encode_shared(encoder, value)
337347

338-
def encode_shared(self, encoder: Callable[[CBOREncoder, Any], Any], value: Any) -> None:
348+
def encode_shared(
349+
self, encoder: Callable[[CBOREncoder, Any], Any], value: Any
350+
) -> None:
339351
value_id = id(value)
340352
try:
341353
index = self._shared_containers[id(value)][1]
@@ -470,7 +482,9 @@ def encode_sortable_key(self, value: Any) -> tuple[int, bytes]:
470482
@container_encoder
471483
def encode_canonical_map(self, value: Mapping[Any, Any]) -> None:
472484
"""Reorder keys according to Canonical CBOR specification"""
473-
keyed_keys = ((self.encode_sortable_key(key), key, value) for key, value in value.items())
485+
keyed_keys = (
486+
(self.encode_sortable_key(key), key, value) for key, value in value.items()
487+
)
474488
self.encode_length(5, len(value))
475489
for sortkey, realkey, value in sorted(keyed_keys):
476490
if self.string_referencing:
@@ -507,7 +521,8 @@ def encode_datetime(self, value: datetime) -> None:
507521
value = value.replace(tzinfo=self._timezone)
508522
else:
509523
raise CBOREncodeValueError(
510-
f"naive datetime {value!r} encountered and no default timezone " "has been set"
524+
f"naive datetime {value!r} encountered and no default timezone "
525+
"has been set"
511526
)
512527

513528
if self.datetime_as_timestamp:
@@ -593,7 +608,9 @@ def encode_ipaddress(self, value: IPv4Address | IPv6Address) -> None:
593608

594609
def encode_ipnetwork(self, value: IPv4Network | IPv6Network) -> None:
595610
# Semantic tag 261
596-
self.encode_semantic(CBORTag(261, {value.network_address.packed: value.prefixlen}))
611+
self.encode_semantic(
612+
CBORTag(261, {value.network_address.packed: value.prefixlen})
613+
)
597614

598615
#
599616
# Special encoders (major tag 7)

src/surrealdb/cbor2/decoder.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44
from surrealdb.cbor2._decoder import load as load
55
from surrealdb.cbor2._decoder import loads as loads
66

7-
warn("The cbor.decoder module has been deprecated. Instead import everything directly from cbor2.")
7+
warn(
8+
"The cbor.decoder module has been deprecated. Instead import everything directly from cbor2."
9+
)

src/surrealdb/cbor2/tool.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
from typing import Literal, TypeAlias
2424

2525
T = TypeVar("T")
26-
JSONValue: TypeAlias = "str | float | bool | None | list[JSONValue] | dict[str, JSONValue]"
26+
JSONValue: TypeAlias = (
27+
"str | float | bool | None | list[JSONValue] | dict[str, JSONValue]"
28+
)
2729

2830
default_encoders: dict[type, Callable[[Any], Any]] = {
2931
bytes: lambda x: x.decode(encoding="utf-8", errors="backslashreplace"),
@@ -44,7 +46,9 @@
4446
}
4547

4648

47-
def tag_hook(decoder: CBORDecoder, tag: CBORTag, ignore_tags: Collection[int] = ()) -> object:
49+
def tag_hook(
50+
decoder: CBORDecoder, tag: CBORTag, ignore_tags: Collection[int] = ()
51+
) -> object:
4852
if tag.tag in ignore_tags:
4953
return tag.value
5054

@@ -72,15 +76,19 @@ def iterdecode(
7276
object_hook: Callable[[CBORDecoder, dict[Any, Any]], Any] | None = None,
7377
str_errors: Literal["strict", "error", "replace"] = "strict",
7478
) -> Iterator[Any]:
75-
decoder = CBORDecoder(f, tag_hook=tag_hook, object_hook=object_hook, str_errors=str_errors)
79+
decoder = CBORDecoder(
80+
f, tag_hook=tag_hook, object_hook=object_hook, str_errors=str_errors
81+
)
7682
while True:
7783
try:
7884
yield decoder.decode()
7985
except EOFError:
8086
return
8187

8288

83-
def key_to_str(d: T, dict_ids: set[int] | None = None) -> str | list[Any] | dict[str, Any] | T:
89+
def key_to_str(
90+
d: T, dict_ids: set[int] | None = None
91+
) -> str | list[Any] | dict[str, Any] | T:
8492
dict_ids = set(dict_ids or [])
8593
rval: dict[str, Any] = {}
8694
if not isinstance(d, dict):

src/surrealdb/cbor2/types.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@
1212
from surrealdb.cbor2._types import FrozenDict as FrozenDict
1313
from surrealdb.cbor2._types import undefined as undefined
1414

15-
warn("The cbor2.types module has been deprecated. Instead import everything directly from cbor2.")
15+
warn(
16+
"The cbor2.types module has been deprecated. Instead import everything directly from cbor2."
17+
)

src/surrealdb/data/cbor.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,10 @@ def default_encoder(encoder, obj):
3838
tagged = CBORTag(constants.TAG_GEOMETRY_MULTI_LINE, obj.get_coordinates())
3939

4040
elif isinstance(obj, GeometryMultiPoint):
41-
tagged = CBORTag(
42-
constants.TAG_GEOMETRY_MULTI_POINT, obj.get_coordinates()
43-
)
41+
tagged = CBORTag(constants.TAG_GEOMETRY_MULTI_POINT, obj.get_coordinates())
4442

4543
elif isinstance(obj, GeometryMultiPolygon):
46-
tagged = CBORTag(
47-
constants.TAG_GEOMETRY_MULTI_POLYGON, obj.get_coordinates()
48-
)
44+
tagged = CBORTag(constants.TAG_GEOMETRY_MULTI_POLYGON, obj.get_coordinates())
4945

5046
elif isinstance(obj, GeometryCollection):
5147
tagged = CBORTag(constants.TAG_GEOMETRY_COLLECTION, obj.geometries)

0 commit comments

Comments
 (0)