Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I flag attributes as being 'ignored' on both serialize and deserialize #52

Closed
johnsturgeon opened this issue Jun 11, 2021 · 3 comments

Comments

@johnsturgeon
Copy link

I only want attributes to be ephemeral . I want to have them as part of the data class, but only used at run time, and I don't want them serialized.

How can I do that?

@Fatal1ty
Copy link
Owner

You can use hooks to exclude attributes from a dictionary or mark the fields as init-only variables.

@johnsturgeon
Copy link
Author

Ultimately I did end up using 'hooks', I used a post serialization hook to exclude my ivars from the resulting dict:

  def __post_serialize__(self, d: Dict[Any, Any]) -> Dict[Any, Any]:
        d.pop('kills')
        d.pop('wins')
        d.pop('damage')
        return d

@Fatal1ty
Copy link
Owner

You can also use __pre_deserialize__ hook and fields with defaults if you want to skip specific keys on deserializarion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants