Skip to content

Commit cee03a0

Browse files
committed
Just warn-print when annots are str values?
1 parent 7a3030a commit cee03a0

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

piker/data/types.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,6 @@ def to_dict(self) -> dict:
4242
for f in self.__struct_fields__
4343
}
4444

45-
# Lul, doesn't seem to work that well..
46-
# def __repr__(self):
47-
# # only turn on pprint when we detect a python REPL
48-
# # at runtime B)
49-
# if (
50-
# hasattr(sys, 'ps1')
51-
# # TODO: check if we're in pdb
52-
# ):
53-
# return self.pformat()
54-
55-
# return super().__repr__()
56-
5745
def pformat(self) -> str:
5846
return f'Struct({pformat(self.to_dict())})'
5947

@@ -63,31 +51,46 @@ def copy(
6351

6452
) -> msgspec.Struct:
6553
'''
66-
Validate-typecast all self defined fields, return a copy of us
67-
with all such fields.
54+
Validate-typecast all self defined fields, return a copy of
55+
us with all such fields.
6856
69-
This is kinda like the default behaviour in `pydantic.BaseModel`.
57+
NOTE: This is kinda like the default behaviour in
58+
`pydantic.BaseModel` except a copy of the object is
59+
returned making it compat with `frozen=True`.
7060
7161
'''
7262
if update:
7363
for k, v in update.items():
7464
setattr(self, k, v)
7565

76-
# roundtrip serialize to validate
66+
# NOTE: roundtrip serialize to validate
67+
# - enode to msgpack binary format,
68+
# - decode that back to a struct.
7769
return msgspec.msgpack.Decoder(
7870
type=type(self)
7971
).decode(
8072
msgspec.msgpack.Encoder().encode(self)
8173
)
8274

83-
# NOTE XXX: this won't work on frozen types!
84-
# use ``.copy()`` above in such cases.
8575
def typecast(
8676
self,
8777
# fields: list[str] | None = None,
78+
8879
) -> None:
89-
for fname, ftype_str in self.__annotations__.items():
90-
ftype = getattr(builtins, ftype_str)
80+
'''
81+
Cast all fields using their declared type annotations
82+
(kinda like what `pydantic` does by default).
83+
84+
NOTE: this of course won't work on frozen types, use
85+
``.copy()`` above in such cases.
86+
87+
'''
88+
annots: dict = self.__annotations__
89+
for fname, ftype in annots.items():
90+
if isinstance(ftype, str):
91+
print(f'{self} has `str` annotations!?\n{annots}\n')
92+
ftype = getattr(builtins, ftype)
93+
9194
attr = getattr(self, fname)
9295
setattr(
9396
self,

0 commit comments

Comments
 (0)