MCPcopy Index your code
hub / github.com/msgspec/msgspec / fields

Function fields

src/msgspec/structs.py:64–114  ·  view source on GitHub ↗

Get information about the fields in a Struct. Parameters ---------- type_or_instance: A struct type or instance. Returns ------- tuple[FieldInfo]

(type_or_instance: Struct | type[Struct])

Source from the content-addressed store, hash-verified

62
63
64def fields(type_or_instance: Struct | type[Struct]) -> tuple[FieldInfo]:
65 """Get information about the fields in a Struct.
66
67 Parameters
68 ----------
69 type_or_instance:
70 A struct type or instance.
71
72 Returns
73 -------
74 tuple[FieldInfo]
75 """
76 obj = type_or_instance
77
78 # Struct class
79 if isinstance(obj, StructMeta):
80 annotated_cls = cls = obj
81 # Struct instance
82 elif isinstance(type(obj), StructMeta):
83 annotated_cls = cls = type(obj)
84 # Generic alias
85 else:
86 annotated_cls = obj
87 cls = getattr(obj, "__origin__", obj)
88 if not isinstance(cls, StructMeta):
89 raise TypeError("Must be called with a struct type or instance")
90
91 hints = _get_class_annotations(annotated_cls)
92 npos = len(cls.__struct_fields__) - len(cls.__struct_defaults__)
93 fields = []
94 for name, encode_name, default_obj in zip(
95 cls.__struct_fields__,
96 cls.__struct_encode_fields__,
97 (NODEFAULT,) * npos + cls.__struct_defaults__,
98 ):
99 default = default_factory = NODEFAULT
100 if isinstance(default_obj, _Factory):
101 default_factory = default_obj.factory
102 elif default_obj is not NODEFAULT:
103 default = default_obj
104
105 field = FieldInfo(
106 name=name,
107 encode_name=encode_name,
108 type=hints[name],
109 default=default,
110 default_factory=default_factory,
111 )
112 fields.append(field)
113
114 return tuple(fields)

Callers

nothing calls this directly

Calls 2

_get_class_annotationsFunction · 0.85
FieldInfoClass · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…