kj-9/prettymaps

typehint for dataclass asdict()

Closed this issue · 6 comments

kj-9 commented

as commented here,
pylance or mypy does not infer types asdict(x).values() results for x is dataclass.

this is not ideal, since typecheck is almost disable at start of the functions.

is there anyway to workaround this?

kj-9 commented

just not using

a,b,c = asdict(x).values()

but accessor

x.a
x.b
x.c

yes. but in the function, x or a should be readable name which is much longer like transform_arg.circle.

destruction is useful to reduce typing transform_arg. all the way.

kj-9 commented

using attrgetter:
https://stackoverflow.com/questions/54785148/destructuring-dicts-and-objects-in-python

type is not infer. Any type is applied.

kj-9 commented

pyserde type check:
https://github.com/yukinarit/pyserde/blob/main/docs/en/type-check.md

kind of runtime check, not static check.

kj-9 commented

pydantic: serialization to return Any. if you want to override it, have to write down.
https://docs.pydantic.dev/latest/usage/serialization/#overriding-the-return-type-when-dumping-a-model

kj-9 commented

I decided to implement to_tuple method if needed. and add abstract class Tuplable for that common method.

kj-9 commented

namedtuple is more suitable for this.
d1a616f