add type stub

This commit is contained in:
peasoft 2026-01-02 22:44:06 +08:00
parent f9806368ae
commit acdcdbccd2
3 changed files with 79 additions and 63 deletions

View file

@ -1,5 +1,7 @@
# ruff: noqa: F401
# pyright: reportUnusedImport = none
import os
import typing as t
from .exceptions import * # noqa: F403
from .ext import ExtType, Timestamp
@ -8,7 +10,7 @@ version = (1, 1, 2)
__version__ = "1.1.2"
if os.environ.get("MSGPACK_PUREPYTHON"):
if os.environ.get("MSGPACK_PUREPYTHON") or t.TYPE_CHECKING:
from .fallback import Packer, Unpacker, unpackb
else:
try:
@ -17,26 +19,26 @@ else:
from .fallback import Packer, Unpacker, unpackb
def pack(o, stream, **kwargs):
def pack(o: t.Any, stream: t.BinaryIO, **kwargs: dict[str, t.Any]):
"""
Pack object `o` and write it to `stream`
See :class:`Packer` for options.
"""
packer = Packer(**kwargs)
stream.write(packer.pack(o))
packer = Packer(autoreset=True, **kwargs) # type: ignore
stream.write(t.cast(bytes, packer.pack(o)))
def packb(o, **kwargs):
def packb(o: t.Any, **kwargs: dict[str, t.Any]):
"""
Pack object `o` and return packed bytes
See :class:`Packer` for options.
"""
return Packer(**kwargs).pack(o)
return Packer(autoreset=True, **kwargs).pack(o) # type: ignore
def unpack(stream, **kwargs):
def unpack(stream: t.BinaryIO, **kwargs: dict[str, t.Any]):
"""
Unpack an object from `stream`.