Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed/corrected type annotations #219

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

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

Expand Down