Skip to content

Commit

Permalink
Work around mypy limitations (#191)
Browse files Browse the repository at this point in the history
Currently mypy doesn't interpret things other than classes as being
compatible with `Type[T]` (they literally have to be instances of
`type`). This makes it hard to properly type the `decode` methods or
`Decoder` constructors, as passing type-like-things (e.g. `Union[int,
str]`) will error under mypy. `pyright` doesn't have this limitation.

For now we add a fallback `overload` to these methods that infers the
decoder type as `Decoder[Any]`. This means that `mypy` will no longer
error, but will require an explicit annotation to infer the type of the
output of `decode`.

If/once the `TypeForm` PEP lands, this hack can be removed. Until then
`pyright` support for these annotations will be much better than `mypy`
support.
  • Loading branch information
jcrist authored Sep 19, 2022
1 parent e1f6aea commit 1605eb7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
15 changes: 15 additions & 0 deletions msgspec/json.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Encoder:
class Decoder(Generic[T]):
type: Type[T]
dec_hook: dec_hook_sig

@overload
def __init__(
self: Decoder[Any],
Expand All @@ -46,6 +47,13 @@ class Decoder(Generic[T]):
*,
dec_hook: dec_hook_sig = None,
) -> None: ...
@overload
def __init__(
self: Decoder[Any],
type: Any = ...,
*,
dec_hook: dec_hook_sig = None,
) -> None: ...
def decode(self, data: bytes) -> T: ...

@overload
Expand All @@ -61,6 +69,13 @@ def decode(
type: Type[T] = ...,
dec_hook: dec_hook_sig = None,
) -> T: ...
@overload
def decode(
buf: bytes,
*,
type: Any = ...,
dec_hook: dec_hook_sig = None,
) -> Any: ...
def encode(obj: Any, *, enc_hook: enc_hook_sig = None) -> bytes: ...
def schema(type: Any) -> Dict[str, Any]: ...
def schema_components(
Expand Down
16 changes: 16 additions & 0 deletions msgspec/msgpack.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ class Decoder(Generic[T]):
dec_hook: dec_hook_sig = None,
ext_hook: ext_hook_sig = None,
) -> None: ...
@overload
def __init__(
self: Decoder[Any],
type: Any = ...,
*,
dec_hook: dec_hook_sig = None,
ext_hook: ext_hook_sig = None,
) -> None: ...
def decode(self, data: bytes) -> T: ...

class Encoder:
Expand Down Expand Up @@ -63,4 +71,12 @@ def decode(
dec_hook: dec_hook_sig = None,
ext_hook: ext_hook_sig = None,
) -> T: ...
@overload
def decode(
buf: bytes,
*,
type: Any = ...,
dec_hook: dec_hook_sig = None,
ext_hook: ext_hook_sig = None,
) -> Any: ...
def encode(obj: Any, *, enc_hook: enc_hook_sig = None) -> bytes: ...
28 changes: 27 additions & 1 deletion tests/basic_typing_examples.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# fmt: off
from __future__ import annotations

import datetime
import pickle
from typing import List, Any, Type
from typing import List, Any, Type, Union
import msgspec

def check___version__() -> None:
Expand Down Expand Up @@ -391,6 +393,14 @@ def check_msgpack_Decoder_decode_typed() -> None:
reveal_type(o) # assert ("List" in typ or "list" in typ) and "int" in typ


def check_msgpack_Decoder_decode_union() -> None:
# Pyright doesn't require the annotation, but mypy does until TypeForm
# is supported. This is mostly checking that no error happens here.
dec: msgspec.msgpack.Decoder[Union[int, str]] = msgspec.msgpack.Decoder(Union[int, str])
o = dec.decode(b'')
reveal_type(o) # assert ("int" in typ and "str" in typ)


def check_msgpack_Decoder_decode_type_comment() -> None:
dec = msgspec.msgpack.Decoder() # type: msgspec.msgpack.Decoder[List[int]]
b = msgspec.msgpack.encode([1, 2, 3])
Expand All @@ -414,6 +424,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_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


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

Expand Down Expand Up @@ -495,6 +510,12 @@ def check_json_Decoder_decode_type_comment() -> None:
reveal_type(o) # assert ("List" in typ or "list" in typ) and "int" in typ


def check_json_Decoder_decode_union() -> None:
dec: msgspec.json.Decoder[Union[int, str]] = msgspec.json.Decoder(Union[int, str])
o = dec.decode(b'')
reveal_type(o) # assert ("int" in typ and "str" in typ)


def check_json_decode_any() -> None:
b = msgspec.json.encode([1, 2, 3])
o = msgspec.json.decode(b)
Expand All @@ -509,6 +530,11 @@ def check_json_decode_typed() -> None:
reveal_type(o) # assert ("List" in typ or "list" in typ) and "int" in typ


def check_json_decode_typed_union() -> None:
o: Union[int, str] = msgspec.json.decode(b"", type=Union[int, str])
reveal_type(o) # assert "int" in typ and "str" in typ


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

Expand Down

0 comments on commit 1605eb7

Please sign in to comment.