ProtixIT / dataclass-binder

Python library to bind TOML data to dataclasses in a type-safe way
MIT License
13 stars 2 forks source link

Support `Any`? #10

Closed mthuurne closed 1 year ago

mthuurne commented 1 year ago

Currently if you use typing.Any in an annotation, this will result in the message Annotation for field '<context>' is not a type. While this is technically correct (Any is not a subclass of type), it isn't very helpful to the end user.

One solution would be to add a dedicated error message saying that Any is unsupported.

Alternatively, we could support Any by accepting any data type that the TOML parser can produce. While this doesn't provide type safety, there might be situations where providing a full type hierarchy is not feasible or not worth the effort, for example when configuring plugins or optional modules. Without Any support, the only solution would be to cut out specific fields from the parsed TOML data prior to calling the bind() method and then re-inserting them after binding, which is messier than using an Any annotation.

mthuurne commented 1 year ago

I decided to add the support for Any, as we were already cutting off part of the TOML data in production, to configure the Python logging system. Because that happened at the root of the tree and no re-insertion was needed, I didn't realize earlier that supporting Any could simplify things.