Skip to content
Open
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
165 changes: 76 additions & 89 deletions src/msgspec/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import enum
from collections.abc import Callable, Iterable, Mapping
from inspect import Signature
from typing import (
Any,
Callable,
ClassVar,
Dict,
Final,
Iterable,
Literal,
Mapping,
Optional,
Tuple,
Type,
TypeVar,
Union,
overload,
)

Expand All @@ -29,29 +22,27 @@ from . import inspect, json, msgpack, structs, toml, yaml
_SM = TypeVar("_SM", bound="StructMeta")

class StructMeta(type):
__struct_fields__: ClassVar[Tuple[str, ...]]
__struct_defaults__: ClassVar[Tuple[Any, ...]]
__struct_encode_fields__: ClassVar[Tuple[str, ...]]
__match_args__: ClassVar[Tuple[str, ...]]
__struct_fields__: ClassVar[tuple[str, ...]]
__struct_defaults__: ClassVar[tuple[Any, ...]]
__struct_encode_fields__: ClassVar[tuple[str, ...]]
__match_args__: ClassVar[tuple[str, ...]]
@property
def __signature__(self) -> Signature: ...
@property
def __struct_config__(self) -> structs.StructConfig: ...
def __new__(
mcls: Type[_SM],
mcls: type[_SM],
name: str,
bases: Tuple[type, ...],
namespace: Dict[str, Any],
bases: tuple[type, ...],
namespace: dict[str, Any],
/,
*,
tag: Union[None, bool, str, int, Callable[[str], Union[str, int]]] = None,
tag_field: Union[None, str] = None,
rename: Union[
None,
Literal["lower", "upper", "camel", "pascal", "kebab"],
Callable[[str], Optional[str]],
Mapping[str, str],
] = None,
tag: None | bool | str | int | Callable[[str], str | int] = None,
tag_field: None | str = None,
rename: None
| Literal["lower", "upper", "camel", "pascal", "kebab"]
| Callable[[str], str | None]
| Mapping[str, str] = None,
omit_defaults: bool = False,
forbid_unknown_fields: bool = False,
frozen: bool = False,
Expand Down Expand Up @@ -79,29 +70,27 @@ class _NoDefault(enum.Enum):
NODEFAULT = _NoDefault.NODEFAULT

@overload
def field(*, default: T, name: Optional[str] = None) -> T: ...
def field(*, default: T, name: str | None = None) -> T: ...
@overload
def field(*, default_factory: Callable[[], T], name: Optional[str] = None) -> T: ...
def field(*, default_factory: Callable[[], T], name: str | None = None) -> T: ...
@overload
def field(*, name: Optional[str] = None) -> Any: ...
def field(*, name: str | None = None) -> Any: ...
@dataclass_transform(field_specifiers=(field,))
class Struct(metaclass=StructMeta):
__struct_fields__: ClassVar[Tuple[str, ...]]
__struct_fields__: ClassVar[tuple[str, ...]]
__struct_config__: ClassVar[structs.StructConfig]
__match_args__: ClassVar[Tuple[str, ...]]
__match_args__: ClassVar[tuple[str, ...]]
# A default __init__ so that Structs with unknown field types (say
# constructed by `defstruct`) won't error on every call to `__init__`
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
def __init_subclass__(
cls,
tag: Union[None, bool, str, int, Callable[[str], Union[str, int]]] = None,
tag_field: Union[None, str] = None,
rename: Union[
None,
Literal["lower", "upper", "camel", "pascal", "kebab"],
Callable[[str], Optional[str]],
Mapping[str, str],
] = None,
tag: None | bool | str | int | Callable[[str], str | int] = None,
tag_field: None | str = None,
rename: None
| Literal["lower", "upper", "camel", "pascal", "kebab"]
| Callable[[str], str | None]
| Mapping[str, str] = None,
omit_defaults: bool = False,
forbid_unknown_fields: bool = False,
frozen: bool = False,
Expand All @@ -117,23 +106,21 @@ class Struct(metaclass=StructMeta):
) -> None: ...
def __rich_repr__(
self,
) -> Iterable[Union[Any, Tuple[Any], Tuple[str, Any], Tuple[str, Any, Any]]]: ...
) -> Iterable[Any | tuple[Any] | tuple[str, Any] | tuple[str, Any, Any]]: ...

def defstruct(
name: str,
fields: Iterable[Union[str, Tuple[str, type], Tuple[str, type, Any]]],
fields: Iterable[str | tuple[str, type] | tuple[str, type, Any]],
*,
bases: Optional[Tuple[Type[Struct], ...]] = None,
module: Optional[str] = None,
namespace: Optional[Dict[str, Any]] = None,
tag: Union[None, bool, str, int, Callable[[str], Union[str, int]]] = None,
tag_field: Union[None, str] = None,
rename: Union[
None,
Literal["lower", "upper", "camel", "pascal", "kebab"],
Callable[[str], Optional[str]],
Mapping[str, str],
] = None,
bases: tuple[type[Struct], ...] | None = None,
module: str | None = None,
namespace: dict[str, Any] | None = None,
tag: None | bool | str | int | Callable[[str], str | int] = None,
tag_field: None | str = None,
rename: None
| Literal["lower", "upper", "camel", "pascal", "kebab"]
| Callable[[str], str | None]
| Mapping[str, str] = None,
omit_defaults: bool = False,
forbid_unknown_fields: bool = False,
frozen: bool = False,
Expand All @@ -146,69 +133,69 @@ def defstruct(
weakref: bool = False,
dict: bool = False,
cache_hash: bool = False,
) -> Type[Struct]: ...
) -> type[Struct]: ...

# Lie and say `Raw` is a subclass of `bytes`, so mypy will accept it in most
# places where an object that implements the buffer protocol is valid
class Raw(bytes):
@overload
def __new__(cls) -> "Raw": ...
def __new__(cls) -> Raw: ...
@overload
def __new__(cls, msg: Union[Buffer, str]) -> "Raw": ...
def copy(self) -> "Raw": ...
def __new__(cls, msg: Buffer | str) -> Raw: ...
def copy(self) -> Raw: ...

class Meta:
def __init__(
self,
*,
gt: Union[int, float, None] = None,
ge: Union[int, float, None] = None,
lt: Union[int, float, None] = None,
le: Union[int, float, None] = None,
multiple_of: Union[int, float, None] = None,
pattern: Union[str, None] = None,
min_length: Union[int, None] = None,
max_length: Union[int, None] = None,
tz: Union[bool, None] = None,
title: Union[str, None] = None,
description: Union[str, None] = None,
examples: Union[list, None] = None,
extra_json_schema: Union[dict, None] = None,
extra: Union[dict, None] = None,
gt: int | float | None = None,
ge: int | float | None = None,
lt: int | float | None = None,
le: int | float | None = None,
multiple_of: int | float | None = None,
pattern: str | None = None,
min_length: int | None = None,
max_length: int | None = None,
tz: bool | None = None,
title: str | None = None,
description: str | None = None,
examples: list | None = None,
extra_json_schema: dict | None = None,
extra: dict | None = None,
): ...
gt: Final[Union[int, float, None]]
ge: Final[Union[int, float, None]]
lt: Final[Union[int, float, None]]
le: Final[Union[int, float, None]]
multiple_of: Final[Union[int, float, None]]
pattern: Final[Union[str, None]]
min_length: Final[Union[int, None]]
max_length: Final[Union[int, None]]
tz: Final[Union[int, None]]
title: Final[Union[str, None]]
description: Final[Union[str, None]]
examples: Final[Union[list, None]]
extra_json_schema: Final[Union[dict, None]]
extra: Final[Union[dict, None]]
def __rich_repr__(self) -> Iterable[Tuple[str, Any]]: ...
gt: Final[int | float | None]
ge: Final[int | float | None]
lt: Final[int | float | None]
le: Final[int | float | None]
multiple_of: Final[int | float | None]
pattern: Final[str | None]
min_length: Final[int | None]
max_length: Final[int | None]
tz: Final[int | None]
title: Final[str | None]
description: Final[str | None]
examples: Final[list | None]
extra_json_schema: Final[dict | None]
extra: Final[dict | None]
def __rich_repr__(self) -> Iterable[tuple[str, Any]]: ...

def to_builtins(
obj: Any,
*,
str_keys: bool = False,
builtin_types: Union[Iterable[type], None] = None,
enc_hook: Optional[Callable[[Any], Any]] = None,
builtin_types: Iterable[type] | None = None,
enc_hook: Callable[[Any], Any] | None = None,
order: Literal[None, "deterministic", "sorted"] = None,
) -> Any: ...
@overload
def convert(
obj: Any,
type: Type[T],
type: type[T],
*,
strict: bool = True,
from_attributes: bool = False,
dec_hook: Optional[Callable[[type, Any], Any]] = None,
builtin_types: Union[Iterable[type], None] = None,
dec_hook: Callable[[type, Any], Any] | None = None,
builtin_types: Iterable[type] | None = None,
str_keys: bool = False,
) -> T: ...
@overload
Expand All @@ -218,8 +205,8 @@ def convert(
*,
strict: bool = True,
from_attributes: bool = False,
dec_hook: Optional[Callable[[type, Any], Any]] = None,
builtin_types: Union[Iterable[type], None] = None,
dec_hook: Callable[[type, Any], Any] | None = None,
builtin_types: Iterable[type] | None = None,
str_keys: bool = False,
) -> Any: ...

Expand Down
10 changes: 5 additions & 5 deletions src/msgspec/_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import re
import textwrap
from collections.abc import Iterable
from typing import Any, Callable, Optional
from collections.abc import Callable, Iterable
from typing import Any

from . import inspect as mi, to_builtins

__all__ = ("schema", "schema_components")


def schema(
type: Any, *, schema_hook: Optional[Callable[[type], dict[str, Any]]] = None
type: Any, *, schema_hook: Callable[[type], dict[str, Any]] | None = None
) -> dict[str, Any]:
"""Generate a JSON Schema for a given type.

Expand Down Expand Up @@ -48,7 +48,7 @@ def schema(
def schema_components(
types: Iterable[Any],
*,
schema_hook: Optional[Callable[[type], dict[str, Any]]] = None,
schema_hook: Callable[[type], dict[str, Any]] | None = None,
ref_template: str = "#/$defs/{name}",
) -> tuple[tuple[dict[str, Any], ...], dict[str, Any]]:
"""Generate JSON Schemas for one or more types.
Expand Down Expand Up @@ -201,7 +201,7 @@ class _SchemaGenerator:
def __init__(
self,
name_map: dict[Any, str],
schema_hook: Optional[Callable[[type], dict[str, Any]]] = None,
schema_hook: Callable[[type], dict[str, Any]] | None = None,
ref_template: str = "#/$defs/{name}",
):
self.name_map = name_map
Expand Down
2 changes: 1 addition & 1 deletion src/msgspec/_typing_utils.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing_extensions import TypeGuard
from typing import TypeGuard

from . import Struct

Expand Down
5 changes: 0 additions & 5 deletions src/msgspec/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,6 @@ def get_class_annotations(obj):
set: set,
frozenset: frozenset,
dict: dict,
typing.List: list,
typing.Tuple: tuple,
typing.Set: set,
typing.FrozenSet: frozenset,
typing.Dict: dict,
typing.Collection: list,
typing.MutableSequence: list,
typing.Sequence: list,
Expand Down
Loading
Loading