Skip to content

Commit

Permalink
Just warn-print when annots are str values?
Browse files Browse the repository at this point in the history
  • Loading branch information
goodboy committed Jun 20, 2023
1 parent 7a3030a commit cee03a0
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions piker/data/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@ def to_dict(self) -> dict:
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:
return f'Struct({pformat(self.to_dict())})'

Expand All @@ -63,31 +51,46 @@ def copy(

) -> msgspec.Struct:
'''
Validate-typecast all self defined fields, return a copy of us
with all such fields.
Validate-typecast all self defined fields, return a copy of
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:
for k, v in update.items():
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(
type=type(self)
).decode(
msgspec.msgpack.Encoder().encode(self)
)

# NOTE XXX: this won't work on frozen types!
# use ``.copy()`` above in such cases.
def typecast(
self,
# fields: list[str] | 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)
setattr(
self,
Expand Down

0 comments on commit cee03a0

Please sign in to comment.