-
-
Notifications
You must be signed in to change notification settings - Fork 111
-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
Excluding attributes on an attrs model from unstructuring breaks auto unstructuring #169
Comments
Hello, I'm on my honeymoon atm so my access to a computer is limited. Does it work if you remove the |
No worries!
Yes, if I remove the If I leave the def _gen_conv() -> GenConverter:
_conv = GenConverter()
_conv.register_unstructure_hook(frozenset, lambda v: list(v))
_conv.register_unstructure_hook(UUID, lambda v: str(v))
attr.resolve_types(Breaks) # Force resolution of the types so it doesn't fall back to string types
_conv.register_unstructure_hook(Breaks, make_dict_unstructure_fn(Breaks, _conv, exclude_me=override(omit=True)))
return _conv Evidence that it's only finding a str typeSetting a breakpoint here, and looking at def unstructure(self, obj: Any, unstructure_as=None) -> Any:
return self._unstructure_func.dispatch(
obj.__class__ if unstructure_as is None else unstructure_as
)(obj) |
Yeah, I will add annotation resolution to the gen functions in the next release. In the meantime, you can work around the issue by calling it yourself, as mentioned :) |
This should work now on master, the gen functions will call resolve_types for you. |
Awesome thanks, will give it a try. |
@Tinche Any chances that we can have a new release soon that includes this patch? Thanks in advace |
Yeah, there will be a release this week! |
Just saw this issue, seems similar to something I reported on attrs python-attrs/attrs#901 @Tinche any thoughts on that? |
This should now work with the latest release, forgot to close. |
…1 in /packages/@jsii/python-runtime (#3315) Updates the requirements on [cattrs](https://github.com/python-attrs/cattrs) to permit the latest version. <details> <summary>Changelog</summary> <p><em>Sourced from <a href="https://github.com/python-attrs/cattrs/blob/main/HISTORY.rst">cattrs's changelog</a>.</em></p> <blockquote> <h2>1.10.0 (2022-01-04)</h2> <ul> <li>Add PEP 563 (string annotations) support for dataclasses. (<code>[#195](python-attrs/cattrs#195) <https://github.com/python-attrs/cattrs/issues/195></code>_)</li> <li>Fix handling of dictionaries with string Enum keys for bson, orjson, and tomlkit.</li> <li>Rename the <code>cattr.gen.make_dict_unstructure_fn.omit_if_default</code> parameter to <code>_cattrs_omit_if_default</code>, for consistency. The <code>omit_if_default</code> parameters to <code>GenConverter</code> and <code>override</code> are unchanged.</li> <li>Following the changes in <code>attrs</code> 21.3.0, add a <code>cattrs</code> package mirroring the existing <code>cattr</code> package. Both package names may be used as desired, and the <code>cattr</code> package isn't going away.</li> </ul> <h2>1.9.0 (2021-12-06)</h2> <ul> <li>Python 3.10 support, including support for the new union syntax (<code>A | B</code> vs <code>Union[A, B]</code>).</li> <li>The <code>GenConverter</code> can now properly structure generic classes with generic collection fields. (<code>[#149](python-attrs/cattrs#149) <https://github.com/python-attrs/cattrs/issues/149></code>_)</li> <li><code>omit=True</code> now also affects generated structuring functions. (<code>[#166](python-attrs/cattrs#166) <https://github.com/python-attrs/cattrs/issues/166></code>_)</li> <li><code>cattr.gen.{make_dict_structure_fn, make_dict_unstructure_fn}</code> now resolve type annotations automatically when PEP 563 is used. (<code>[#169](python-attrs/cattrs#169) <https://github.com/python-attrs/cattrs/issues/169></code>_)</li> <li>Protocols are now unstructured as their runtime types. (<code>[#177](python-attrs/cattrs#177) <https://github.com/python-attrs/cattrs/pull/177></code>_)</li> <li>Fix an issue generating structuring functions with renaming and <code>_cattrs_forbid_extra_keys=True</code>. (<code>[#190](python-attrs/cattrs#190) <https://github.com/python-attrs/cattrs/issues/190></code>_)</li> </ul> <h2>1.8.0 (2021-08-13)</h2> <ul> <li>Fix <code>GenConverter</code> mapping structuring for unannotated dicts on Python 3.8. (<code>[#151](python-attrs/cattrs#151) <https://github.com/python-attrs/cattrs/issues/151></code>_)</li> <li>The source code for generated un/structuring functions is stored in the <code>linecache</code> cache, which enables more informative stack traces when un/structuring errors happen using the <code>GenConverter</code>. This behavior can optionally be disabled to save memory.</li> <li>Support using the attr converter callback during structure. By default, this is a method of last resort, but it can be elevated to the default by setting <code>prefer_attrib_converters=True</code> on <code>Converter</code> or <code>GenConverter</code>. (<code>[#138](python-attrs/cattrs#138) <https://github.com/python-attrs/cattrs/issues/138></code>_)</li> <li>Fix structuring recursive classes. (<code>[#159](python-attrs/cattrs#159) <https://github.com/python-attrs/cattrs/issues/159></code>_)</li> <li>Converters now support un/structuring hook factories. This is the most powerful and complex venue for customizing un/structuring. This had previously been an internal feature.</li> <li>The <code>Common Usage Examples <https://cattrs.readthedocs.io/en/latest/usage.html#using-factory-hooks></code>_ documentation page now has a section on advanced hook factory usage.</li> <li><code>cattr.override</code> now supports the <code>omit</code> parameter, which makes <code>cattrs</code> skip the atribute entirely when unstructuring.</li> <li>The <code>cattr.preconf.bson</code> module is now tested against the <code>bson</code> module bundled with the <code>pymongo</code> package, because that package is much more popular than the standalone PyPI <code>bson</code> package.</li> </ul> <h2>1.7.1 (2021-05-28)</h2> <ul> <li><code>Literal</code> s are not supported on Python 3.9.0 (supported on 3.9.1 and later), so we skip importing them there. (<code>[#150](python-attrs/cattrs#150) <https://github.com/python-attrs/cattrs/issues/150></code>_)</li> </ul> <h2>1.7.0 (2021-05-26)</h2> <ul> <li><code>cattr.global_converter</code> (which provides <code>cattr.unstructure</code>, <code>cattr.structure</code> etc.) is now an instance of <code>cattr.GenConverter</code>.</li> <li><code>Literal</code> s are now supported and validated when structuring.</li> <li>Fix dependency metadata information for <code>attrs</code>. (<code>[
Description
I have a field on an attrs model that I want excluded when unstructuring. When registering an unstructure hook using
make_dict_unstructure_fn
andfield=override(Skip=True)
(as read in the documentation) the other unstructure hooks aren't being triggered (UUID, nested models, etc). The same issue happens if the example usingregister_unstructure_hook_factory
is used instead.What I Did
Sample program to demonstrate the issue. Running
json.dumps
on the unstructuredBreaks
will break complaining thatObject of type UUID is not JSON serializable
(or if the ID is commented out thenObject of type Nested
), whereas it works properly onWorks
What I would expect
I'm looking for a good way to exclude one or more attributes from unstructuring an attrs model without needing to wrap them in a function like this:
The text was updated successfully, but these errors were encountered: