Skip to content

Commit

Permalink
Merge branch 'master' into fix-date-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm authored Feb 19, 2024
2 parents c12802b + 2eeaab8 commit 750ee19
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
16 changes: 8 additions & 8 deletions cbor2/_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from collections.abc import Callable, Mapping, Sequence
from datetime import date, datetime, timedelta, timezone
from io import BytesIO
from typing import TYPE_CHECKING, Any, TypeVar, cast, overload
from typing import IO, TYPE_CHECKING, Any, TypeVar, cast, overload

from ._types import (
CBORDecodeEOF,
Expand Down Expand Up @@ -61,12 +61,12 @@ class CBORDecoder:
"_stringref_namespace",
)

_fp: BytesIO
_fp: IO[bytes]
_fp_read: Callable[[int], bytes]

def __init__(
self,
fp: BytesIO,
fp: IO[bytes],
tag_hook: Callable[[CBORDecoder, CBORTag], Any] | None = None,
object_hook: Callable[[CBORDecoder, dict[Any, Any]], Any] | None = None,
str_errors: Literal["strict", "error", "replace"] = "strict",
Expand Down Expand Up @@ -111,11 +111,11 @@ def immutable(self) -> bool:
return self._immutable

@property
def fp(self) -> BytesIO:
def fp(self) -> IO[bytes]:
return self._fp

@fp.setter
def fp(self, value: BytesIO) -> None:
def fp(self, value: IO[bytes]) -> None:
try:
if not callable(value.read):
raise ValueError("fp.read is not callable")
Expand Down Expand Up @@ -791,7 +791,7 @@ def loads(
tag_hook: Callable[[CBORDecoder, CBORTag], Any] | None = None,
object_hook: Callable[[CBORDecoder, dict[Any, Any]], Any] | None = None,
str_errors: Literal["strict", "error", "replace"] = "strict",
) -> object:
) -> Any:
"""
Deserialize an object from a bytestring.
Expand Down Expand Up @@ -822,11 +822,11 @@ def loads(


def load(
fp: BytesIO,
fp: IO[bytes],
tag_hook: Callable[[CBORDecoder, CBORTag], Any] | None = None,
object_hook: Callable[[CBORDecoder, dict[Any, Any]], Any] | None = None,
str_errors: Literal["strict", "error", "replace"] = "strict",
) -> object:
) -> Any:
"""
Deserialize an object from an open file.
Expand Down
12 changes: 6 additions & 6 deletions cbor2/_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from functools import wraps
from io import BytesIO
from sys import modules
from typing import TYPE_CHECKING, Any, cast
from typing import IO, TYPE_CHECKING, Any, cast

from ._types import (
CBOREncodeTypeError,
Expand Down Expand Up @@ -125,12 +125,12 @@ class CBOREncoder:
"_string_references",
)

_fp: BytesIO
_fp: IO[bytes]
_fp_write: Callable[[Buffer], int]

def __init__(
self,
fp: BytesIO,
fp: IO[bytes],
datetime_as_timestamp: bool = False,
timezone: tzinfo | None = None,
value_sharing: bool = False,
Expand Down Expand Up @@ -216,11 +216,11 @@ def _find_encoder(self, obj_type: type) -> Callable[[CBOREncoder, Any], None] |
return None

@property
def fp(self) -> BytesIO:
def fp(self) -> IO[bytes]:
return self._fp

@fp.setter
def fp(self, value: BytesIO) -> None:
def fp(self, value: IO[bytes]) -> None:
try:
if not callable(value.write):
raise ValueError("fp.write is not callable")
Expand Down Expand Up @@ -749,7 +749,7 @@ def dumps(

def dump(
obj: object,
fp: BytesIO,
fp: IO[bytes],
datetime_as_timestamp: bool = False,
timezone: tzinfo | None = None,
value_sharing: bool = False,
Expand Down
4 changes: 4 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ This library adheres to `Semantic Versioning <http://semver.org/>`_.

- Fixed ``__hash__()`` of the C version of the ``CBORTag`` type crashing when there's a recursive
reference cycle
- Fixed type annotation for the file object in ``cbor2.dump()``, ``cbor2.load()``, ``CBOREncoder``
and ``CBORDecoder`` to be ``IO[bytes]`` instead of ``BytesIO``
- Changed the return type annotations of ``cbor2.load()`` and ``cbor2.load()`` to return ``Any``
instead of ``object`` so as not to force users to make type casts
- Fixed decoding of epoch-based dates being affected by the local time zone in the C extension

**5.6.1** (2024-02-01)
Expand Down

0 comments on commit 750ee19

Please sign in to comment.