Skip to content

Commit acdcdbc

Browse files
committed
add type stub
1 parent f980636 commit acdcdbc

File tree

3 files changed

+79
-63
lines changed

3 files changed

+79
-63
lines changed

msgpack/__init__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ruff: noqa: F401
2+
# pyright: reportUnusedImport = none
23
import os
4+
import typing as t
35

46
from .exceptions import * # noqa: F403
57
from .ext import ExtType, Timestamp
@@ -8,7 +10,7 @@
810
__version__ = "1.1.2"
911

1012

11-
if os.environ.get("MSGPACK_PUREPYTHON"):
13+
if os.environ.get("MSGPACK_PUREPYTHON") or t.TYPE_CHECKING:
1214
from .fallback import Packer, Unpacker, unpackb
1315
else:
1416
try:
@@ -17,26 +19,26 @@
1719
from .fallback import Packer, Unpacker, unpackb
1820

1921

20-
def pack(o, stream, **kwargs):
22+
def pack(o: t.Any, stream: t.BinaryIO, **kwargs: dict[str, t.Any]):
2123
"""
2224
Pack object `o` and write it to `stream`
2325
2426
See :class:`Packer` for options.
2527
"""
26-
packer = Packer(**kwargs)
27-
stream.write(packer.pack(o))
28+
packer = Packer(autoreset=True, **kwargs) # type: ignore
29+
stream.write(t.cast(bytes, packer.pack(o)))
2830

2931

30-
def packb(o, **kwargs):
32+
def packb(o: t.Any, **kwargs: dict[str, t.Any]):
3133
"""
3234
Pack object `o` and return packed bytes
3335
3436
See :class:`Packer` for options.
3537
"""
36-
return Packer(**kwargs).pack(o)
38+
return Packer(autoreset=True, **kwargs).pack(o) # type: ignore
3739

3840

39-
def unpack(stream, **kwargs):
41+
def unpack(stream: t.BinaryIO, **kwargs: dict[str, t.Any]):
4042
"""
4143
Unpack an object from `stream`.
4244

msgpack/ext.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import datetime
22
import struct
33
from collections import namedtuple
4+
import typing as t
45

56

67
class ExtType(namedtuple("ExtType", "code data")):
78
"""ExtType represents ext type in msgpack."""
9+
code: int
10+
data: bytes
811

9-
def __new__(cls, code, data):
12+
def __new__(cls, code: int, data: bytes):
1013
if not isinstance(code, int):
1114
raise TypeError("code must be int")
1215
if not isinstance(data, bytes):
1316
raise TypeError("data must be bytes")
1417
if not 0 <= code <= 127:
1518
raise ValueError("code must be 0~127")
16-
return super().__new__(cls, code, data)
19+
return super().__new__(cls, code, data) # type: ignore
1720

1821

1922
class Timestamp:
@@ -28,7 +31,7 @@ class Timestamp:
2831

2932
__slots__ = ["seconds", "nanoseconds"]
3033

31-
def __init__(self, seconds, nanoseconds=0):
34+
def __init__(self, seconds: int, nanoseconds=0):
3235
"""Initialize a Timestamp object.
3336
3437
:param int seconds:
@@ -54,21 +57,21 @@ def __repr__(self):
5457
"""String representation of Timestamp."""
5558
return f"Timestamp(seconds={self.seconds}, nanoseconds={self.nanoseconds})"
5659

57-
def __eq__(self, other):
60+
def __eq__(self, other: t.Any):
5861
"""Check for equality with another Timestamp object"""
5962
if type(other) is self.__class__:
6063
return self.seconds == other.seconds and self.nanoseconds == other.nanoseconds
6164
return False
6265

63-
def __ne__(self, other):
66+
def __ne__(self, other: t.Any):
6467
"""not-equals method (see :func:`__eq__()`)"""
6568
return not self.__eq__(other)
6669

6770
def __hash__(self):
6871
return hash((self.seconds, self.nanoseconds))
6972

7073
@staticmethod
71-
def from_bytes(b):
74+
def from_bytes(b: bytes):
7275
"""Unpack bytes into a `Timestamp` object.
7376
7477
Used for pure-Python msgpack unpacking.
@@ -116,7 +119,7 @@ def to_bytes(self):
116119
return data
117120

118121
@staticmethod
119-
def from_unix(unix_sec):
122+
def from_unix(unix_sec: int | float):
120123
"""Create a Timestamp from posix timestamp in seconds.
121124
122125
:param unix_float: Posix timestamp in seconds.
@@ -135,7 +138,7 @@ def to_unix(self):
135138
return self.seconds + self.nanoseconds / 1e9
136139

137140
@staticmethod
138-
def from_unix_nano(unix_ns):
141+
def from_unix_nano(unix_ns: int):
139142
"""Create a Timestamp from posix timestamp in nanoseconds.
140143
141144
:param int unix_ns: Posix timestamp in nanoseconds.
@@ -162,7 +165,7 @@ def to_datetime(self):
162165
)
163166

164167
@staticmethod
165-
def from_datetime(dt):
168+
def from_datetime(dt: datetime.datetime):
166169
"""Create a Timestamp from datetime with tzinfo.
167170
168171
:rtype: Timestamp

0 commit comments

Comments
 (0)