Skip to content

Commit

Permalink
Add support for dataclasses with InitVar
Browse files Browse the repository at this point in the history
- Ignore init-only variables as they go out of scope when object is
encoded (serialized)
- Warn on encoding (serialization) of dataclass with InitVar without
default value: decoding (deserialization) is impossible!
  • Loading branch information
tokarenko committed Aug 4, 2023
1 parent 229f05a commit 9962470
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions chili/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ def create_schema(cls: Type) -> TypeSchema:
# ignore class vars as they are not object properties
if p_origin and p_origin is ClassVar:
continue

# ignore init-only variables as they go out of scope when object is encoded (serialized)
if type(p_type) is InitVar:
if not name in attributes:
# not optional init-only variables make decoding (deserialization) impossible as they do not persist in encoding (serialization)
warnings.warn(f"Dataclass field '{name}' is InitVar without default value. Decoding is impossible!")
continue

if name in attributes:
prop_value = attributes[name]
Expand Down

0 comments on commit 9962470

Please sign in to comment.