Skip to content

Commit

Permalink
Use positional-only args in certain function sigs
Browse files Browse the repository at this point in the history
Previously the type signatures noted for the json and msgpack modules
weren't strictly accurate. The implementation only accepted positional
args for certain parameters, but the signatures defined them as
positional-or-keyword parameters. This PR rectifies that.

Note that this is _not_ a change in behavior, this is only a change in
the signatures that tools like `mypy` or `pyright` would use to check
your code.
  • Loading branch information
jcrist committed Oct 14, 2024
1 parent f4a0c63 commit 6ee467e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
19 changes: 11 additions & 8 deletions msgspec/json.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ class Encoder:
uuid_format: Literal["canonical", "hex"] = "canonical",
order: Literal[None, "deterministic", "sorted"] = None,
): ...
def encode(self, obj: Any) -> bytes: ...
def encode_lines(self, items: Iterable) -> bytes: ...
def encode(self, obj: Any, /) -> bytes: ...
def encode_lines(self, items: Iterable, /) -> bytes: ...
def encode_into(
self, obj: Any, buffer: bytearray, offset: Optional[int] = 0
self, obj: Any, buffer: bytearray, offset: Optional[int] = 0, /
) -> None: ...

class Decoder(Generic[T]):
Expand Down Expand Up @@ -75,19 +75,21 @@ class Decoder(Generic[T]):
dec_hook: dec_hook_sig = None,
float_hook: float_hook_sig = None,
) -> None: ...
def decode(self, data: Union[Buffer, str]) -> T: ...
def decode_lines(self, data: Union[Buffer, str]) -> list[T]: ...
def decode(self, buf: Union[Buffer, str], /) -> T: ...
def decode_lines(self, buf: Union[Buffer, str], /) -> list[T]: ...

@overload
def decode(
buf: Union[Buffer, str],
/,
*,
strict: bool = True,
dec_hook: dec_hook_sig = None,
) -> Any: ...
@overload
def decode(
buf: Union[Buffer, str],
/,
*,
type: Type[T] = ...,
strict: bool = True,
Expand All @@ -96,12 +98,13 @@ def decode(
@overload
def decode(
buf: Union[Buffer, str],
/,
*,
type: Any = ...,
strict: bool = True,
dec_hook: dec_hook_sig = None,
) -> Any: ...
def encode(obj: Any, *, enc_hook: enc_hook_sig = None, order: Literal[None, "deterministic", "sorted"] = None) -> bytes: ...
def encode(obj: Any, /, *, enc_hook: enc_hook_sig = None, order: Literal[None, "deterministic", "sorted"] = None) -> bytes: ...
def schema(type: Any, *, schema_hook: schema_hook_sig = None) -> Dict[str, Any]: ...
def schema_components(
types: Iterable[Any],
Expand All @@ -110,6 +113,6 @@ def schema_components(
ref_template: str = "#/$defs/{name}"
) -> Tuple[Tuple[Dict[str, Any], ...], Dict[str, Any]]: ...
@overload
def format(buf: str, *, indent: int = 2) -> str: ...
def format(buf: str, /, *, indent: int = 2) -> str: ...
@overload
def format(buf: Buffer, *, 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 @@ -57,7 +57,7 @@ class Decoder(Generic[T]):
dec_hook: dec_hook_sig = None,
ext_hook: ext_hook_sig = None,
) -> None: ...
def decode(self, data: Buffer) -> T: ...
def decode(self, buf: Buffer, /) -> T: ...

class Encoder:
enc_hook: enc_hook_sig
Expand All @@ -72,14 +72,15 @@ class Encoder:
uuid_format: Literal["canonical", "hex", "bytes"] = "canonical",
order: Literal[None, "deterministic", "sorted"] = None,
): ...
def encode(self, obj: Any) -> bytes: ...
def encode(self, obj: Any, /) -> bytes: ...
def encode_into(
self, obj: Any, buffer: bytearray, offset: Optional[int] = 0
self, obj: Any, buffer: bytearray, offset: Optional[int] = 0, /
) -> None: ...

@overload
def decode(
buf: Buffer,
/,
*,
strict: bool = True,
dec_hook: dec_hook_sig = None,
Expand All @@ -88,6 +89,7 @@ def decode(
@overload
def decode(
buf: Buffer,
/,
*,
type: Type[T] = ...,
strict: bool = True,
Expand All @@ -97,10 +99,11 @@ def decode(
@overload
def decode(
buf: Buffer,
/,
*,
type: Any = ...,
strict: bool = True,
dec_hook: dec_hook_sig = None,
ext_hook: ext_hook_sig = None,
) -> Any: ...
def encode(obj: Any, *, enc_hook: enc_hook_sig = None, order: Literal[None, "deterministic", "sorted"] = None) -> bytes: ...
def encode(obj: Any, /, *, enc_hook: enc_hook_sig = None, order: Literal[None, "deterministic", "sorted"] = None) -> bytes: ...

0 comments on commit 6ee467e

Please sign in to comment.