Skip to content

Commit

Permalink
use Buffer everywhere, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist committed Oct 13, 2024
1 parent fb3f976 commit 863b675
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
4 changes: 2 additions & 2 deletions msgspec/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ from typing import (
overload,
)

from typing_extensions import dataclass_transform
from typing_extensions import dataclass_transform, Buffer

from . import inspect, json, msgpack, structs, toml, yaml

Expand Down Expand Up @@ -108,7 +108,7 @@ class Raw(bytes):
@overload
def __new__(cls) -> "Raw": ...
@overload
def __new__(cls, msg: Union[bytes, str]) -> "Raw": ...
def __new__(cls, msg: Union[Buffer, str]) -> "Raw": ...
def copy(self) -> "Raw": ...

class Meta:
Expand Down
14 changes: 8 additions & 6 deletions msgspec/json.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ from typing import (
overload,
)

from typing_extensions import Buffer

T = TypeVar("T")

enc_hook_sig = Optional[Callable[[Any], Any]]
Expand Down Expand Up @@ -73,27 +75,27 @@ class Decoder(Generic[T]):
dec_hook: dec_hook_sig = None,
float_hook: float_hook_sig = None,
) -> None: ...
def decode(self, data: Union[bytes, str]) -> T: ...
def decode_lines(self, data: Union[bytes, str]) -> list[T]: ...
def decode(self, data: Union[Buffer, str]) -> T: ...
def decode_lines(self, data: Union[Buffer, str]) -> list[T]: ...

@overload
def decode(
buf: Union[bytes, str],
buf: Union[Buffer, str],
*,
strict: bool = True,
dec_hook: dec_hook_sig = None,
) -> Any: ...
@overload
def decode(
buf: Union[bytes, str],
buf: Union[Buffer, str],
*,
type: Type[T] = ...,
strict: bool = True,
dec_hook: dec_hook_sig = None,
) -> T: ...
@overload
def decode(
buf: Union[bytes, str],
buf: Union[Buffer, str],
*,
type: Any = ...,
strict: bool = True,
Expand All @@ -110,4 +112,4 @@ def schema_components(
@overload
def format(buf: str, *, indent: int = 2) -> str: ...
@overload
def format(buf: bytes, *, indent: int = 2) -> bytes: ...
def format(buf: Buffer, *, indent: int = 2) -> bytes: ...
15 changes: 11 additions & 4 deletions msgspec/toml.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
from __future__ import annotations

import datetime as _datetime
from typing import Any, Callable, Optional, Type, TypeVar, Union, overload, Literal
from typing import TYPE_CHECKING, overload, TypeVar, Any

from . import (
DecodeError as _DecodeError,
convert as _convert,
to_builtins as _to_builtins,
)

if TYPE_CHECKING:
from typing import Callable, Optional, Type, Union, Literal
from typing_extensions import Buffer


__all__ = ("encode", "decode")


Expand Down Expand Up @@ -103,7 +110,7 @@ def encode(

@overload
def decode(
buf: Union[bytes, str],
buf: Union[Buffer, str],
*,
strict: bool = True,
dec_hook: Optional[Callable[[type, Any], Any]] = None,
Expand All @@ -113,7 +120,7 @@ def decode(

@overload
def decode(
buf: Union[bytes, str],
buf: Union[Buffer, str],
*,
type: Type[T] = ...,
strict: bool = True,
Expand All @@ -124,7 +131,7 @@ def decode(

@overload
def decode(
buf: Union[bytes, str],
buf: Union[Buffer, str],
*,
type: Any = ...,
strict: bool = True,
Expand Down
11 changes: 9 additions & 2 deletions msgspec/yaml.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
from __future__ import annotations

import datetime as _datetime
from typing import Any, Callable, Optional, Type, TypeVar, Union, overload, Literal
from typing import TYPE_CHECKING, overload, TypeVar, Any

from . import (
DecodeError as _DecodeError,
convert as _convert,
to_builtins as _to_builtins,
)

if TYPE_CHECKING:
from typing import Callable, Optional, Type, Union, Literal
from typing_extensions import Buffer


__all__ = ("encode", "decode")


Expand Down Expand Up @@ -96,7 +103,7 @@ def encode(

@overload
def decode(
buf: Union[bytes, str],
buf: Union[Buffer, str],
*,
strict: bool = True,
dec_hook: Optional[Callable[[type, Any], Any]] = None,
Expand Down
17 changes: 17 additions & 0 deletions tests/basic_typing_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,11 @@ def check_msgpack_decode_typed() -> None:
reveal_type(o) # assert ("List" in typ or "list" in typ) and "int" in typ


def check_msgpack_decode_from_buffer() -> None:
msg = msgspec.msgpack.encode([1, 2, 3])
msgspec.toml.decode(memoryview(msg))


def check_msgpack_decode_typed_union() -> None:
o: Union[int, str] = msgspec.msgpack.decode(b"", type=Union[int, str])
reveal_type(o) # assert "int" in typ and "str" in typ
Expand Down Expand Up @@ -835,6 +840,10 @@ def check_json_decode_from_str() -> None:
reveal_type(o) # assert ("List" in typ or "list" in typ) and "int" in typ


def check_json_decode_from_buffer() -> None:
msgspec.json.decode(memoryview(b"[1, 2, 3]"))


def check_json_encode_enc_hook() -> None:
msgspec.json.encode(object(), enc_hook=lambda x: None)

Expand Down Expand Up @@ -929,6 +938,10 @@ def check_yaml_decode_from_str() -> None:
reveal_type(o) # assert "list" in typ.lower() and "int" in typ


def check_yaml_decode_from_buffer() -> None:
msgspec.yaml.decode(memoryview(b"[1, 2, 3]"))


def check_yaml_encode_enc_hook() -> None:
msgspec.yaml.encode(object(), enc_hook=lambda x: None)

Expand Down Expand Up @@ -977,6 +990,10 @@ def check_toml_decode_from_str() -> None:
reveal_type(o) # assert "dict" in typ.lower() and "int" in typ


def check_toml_decode_from_buffer() -> None:
msgspec.toml.decode(memoryview(b"a = 1"))


def check_toml_encode_enc_hook() -> None:
msgspec.toml.encode(object(), enc_hook=lambda x: None)

Expand Down

0 comments on commit 863b675

Please sign in to comment.