Just warn-print when annots are str values?
parent
12172cc5cd
commit
994564f923
|
@ -42,18 +42,6 @@ class Struct(
|
||||||
for f in self.__struct_fields__
|
for f in self.__struct_fields__
|
||||||
}
|
}
|
||||||
|
|
||||||
# Lul, doesn't seem to work that well..
|
|
||||||
# def __repr__(self):
|
|
||||||
# # only turn on pprint when we detect a python REPL
|
|
||||||
# # at runtime B)
|
|
||||||
# if (
|
|
||||||
# hasattr(sys, 'ps1')
|
|
||||||
# # TODO: check if we're in pdb
|
|
||||||
# ):
|
|
||||||
# return self.pformat()
|
|
||||||
|
|
||||||
# return super().__repr__()
|
|
||||||
|
|
||||||
def pformat(self) -> str:
|
def pformat(self) -> str:
|
||||||
return f'Struct({pformat(self.to_dict())})'
|
return f'Struct({pformat(self.to_dict())})'
|
||||||
|
|
||||||
|
@ -63,31 +51,46 @@ class Struct(
|
||||||
|
|
||||||
) -> msgspec.Struct:
|
) -> msgspec.Struct:
|
||||||
'''
|
'''
|
||||||
Validate-typecast all self defined fields, return a copy of us
|
Validate-typecast all self defined fields, return a copy of
|
||||||
with all such fields.
|
us with all such fields.
|
||||||
|
|
||||||
This is kinda like the default behaviour in `pydantic.BaseModel`.
|
NOTE: This is kinda like the default behaviour in
|
||||||
|
`pydantic.BaseModel` except a copy of the object is
|
||||||
|
returned making it compat with `frozen=True`.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
if update:
|
if update:
|
||||||
for k, v in update.items():
|
for k, v in update.items():
|
||||||
setattr(self, k, v)
|
setattr(self, k, v)
|
||||||
|
|
||||||
# roundtrip serialize to validate
|
# NOTE: roundtrip serialize to validate
|
||||||
|
# - enode to msgpack binary format,
|
||||||
|
# - decode that back to a struct.
|
||||||
return msgspec.msgpack.Decoder(
|
return msgspec.msgpack.Decoder(
|
||||||
type=type(self)
|
type=type(self)
|
||||||
).decode(
|
).decode(
|
||||||
msgspec.msgpack.Encoder().encode(self)
|
msgspec.msgpack.Encoder().encode(self)
|
||||||
)
|
)
|
||||||
|
|
||||||
# NOTE XXX: this won't work on frozen types!
|
|
||||||
# use ``.copy()`` above in such cases.
|
|
||||||
def typecast(
|
def typecast(
|
||||||
self,
|
self,
|
||||||
# fields: list[str] | None = None,
|
# fields: list[str] | None = None,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
for fname, ftype_str in self.__annotations__.items():
|
'''
|
||||||
ftype = getattr(builtins, ftype_str)
|
Cast all fields using their declared type annotations
|
||||||
|
(kinda like what `pydantic` does by default).
|
||||||
|
|
||||||
|
NOTE: this of course won't work on frozen types, use
|
||||||
|
``.copy()`` above in such cases.
|
||||||
|
|
||||||
|
'''
|
||||||
|
annots: dict = self.__annotations__
|
||||||
|
for fname, ftype in annots.items():
|
||||||
|
if isinstance(ftype, str):
|
||||||
|
print(f'{self} has `str` annotations!?\n{annots}\n')
|
||||||
|
ftype = getattr(builtins, ftype)
|
||||||
|
|
||||||
attr = getattr(self, fname)
|
attr = getattr(self, fname)
|
||||||
setattr(
|
setattr(
|
||||||
self,
|
self,
|
||||||
|
|
Loading…
Reference in New Issue