1from __future__ import annotations
2
3from . import StructMeta
4
5
6def is_struct(obj: object) -> bool:
7 """Check whether ``obj`` is a `msgspec.Struct`-like instance.
8
9 Parameters
10 ----------
11 obj:
12 Object to check.
13
14 Returns
15 -------
16 bool
17 `True` if ``obj`` is an instance of a class whose metaclass is
18 `msgspec.StructMeta` (or a subclass of it), and `False` otherwise.
19 Static type checkers treat a successful ``is_struct(obj)`` check as
20 narrowing ``obj`` to `msgspec.Struct` within the true branch, even if
21 the runtime class does not literally inherit `msgspec.Struct`.
22 """
23 return isinstance(type(obj), StructMeta)
24
25
26def is_struct_type(tp: object) -> bool:
27 """Check whether ``tp`` is a `msgspec.Struct`-like class.
28
29 Parameters
30 ----------
31 tp:
32 Object to check, typically a class object.
33
34 Returns
35 -------
36 bool
37 `True` if ``tp`` is a class whose metaclass is `msgspec.StructMeta`
38 (or a subclass of it), and `False` otherwise. Static type checkers
39 treat a successful ``is_struct_type(tp)`` check as narrowing
40 ``tp`` to `type[msgspec.Struct]` within the true branch.
41 """
42 return isinstance(tp, StructMeta)