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

Update decode signatures for PEP 688 #740

Merged
merged 5 commits into from
Oct 13, 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
18 changes: 9 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Install Python
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: "3.11"

Expand Down Expand Up @@ -75,7 +75,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-20.04, macos-11, windows-2019]
os: [ubuntu-latest, macos-13, windows-latest]

env:
CIBW_TEST_REQUIRES: "pytest msgpack pyyaml tomli tomli_w"
Expand All @@ -88,7 +88,7 @@ jobs:
CIBW_ENVIRONMENT: "CFLAGS=-g0"

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up QEMU
if: runner.os == 'Linux'
Expand All @@ -105,7 +105,7 @@ jobs:
uses: pypa/[email protected]

- name: Upload artifact
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
if: github.event_name == 'release' && github.event.action == 'published'
with:
path: ./wheelhouse/*.whl
Expand All @@ -116,18 +116,18 @@ jobs:
if: github.event_name == 'release' && github.event.action == 'published'

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Install Python
uses: actions/setup-python@v2
uses: actions/setup-python@v5
with:
python-version: 3.8

- name: Build source distribution
run: python setup.py sdist

- name: Upload artifact
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
path: dist/*.tar.gz

Expand All @@ -136,7 +136,7 @@ jobs:
runs-on: ubuntu-latest
if: github.event_name == 'release' && github.event.action == 'published'
steps:
- uses: actions/download-artifact@v2
- uses: actions/download-artifact@v4
with:
name: artifact
path: dist
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
build-docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Install Python
uses: actions/setup-python@v1
uses: actions/setup-python@v5
with:
python-version: 3.8

Expand Down
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: ...
11 changes: 7 additions & 4 deletions msgspec/msgpack.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ from typing import (
overload,
)

from typing_extensions import Buffer


T = TypeVar("T")

enc_hook_sig = Optional[Callable[[Any], Any]]
Expand Down Expand Up @@ -54,7 +57,7 @@ class Decoder(Generic[T]):
dec_hook: dec_hook_sig = None,
ext_hook: ext_hook_sig = None,
) -> None: ...
def decode(self, data: bytes) -> T: ...
def decode(self, data: Buffer) -> T: ...

class Encoder:
enc_hook: enc_hook_sig
Expand All @@ -76,15 +79,15 @@ class Encoder:

@overload
def decode(
buf: bytes,
buf: Buffer,
*,
strict: bool = True,
dec_hook: dec_hook_sig = None,
ext_hook: ext_hook_sig = None,
) -> Any: ...
@overload
def decode(
buf: bytes,
buf: Buffer,
*,
type: Type[T] = ...,
strict: bool = True,
Expand All @@ -93,7 +96,7 @@ def decode(
) -> T: ...
@overload
def decode(
buf: bytes,
buf: Buffer,
*,
type: Any = ...,
strict: bool = True,
Expand Down
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
Loading