-
-
Notifications
You must be signed in to change notification settings - Fork 114
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
detailed_validation=False doesn't work for GenConverter instance #245
Comments
I added a few tests for structuring attrs classes but couldn't repro it, so I'll need some more information ;) |
Okay, took longer than I would have hoped, but I have a MWE for you: from typing import Optional, Union
import cattrs
from attrs import define, field
CONVERTER = cattrs.GenConverter(detailed_validation=False)
@define(auto_attribs=True, kw_only=True, frozen=False, hash=None, slots=True)
class classA:
# Either "A" or "B"
id_: str = field()
@define(auto_attribs=True, kw_only=True, frozen=False, hash=None, slots=True)
class classB(classA):
# Not always present during structuring:
other_field: Optional[str] = field(default=None)
AOrB = Union[classA, classB]
@define(auto_attribs=True, kw_only=True, frozen=False, hash=None, slots=True)
class Nested:
a_or_b: AOrB = field()
def structure_a_or_b(dict_: dict, _):
if dict_["id_"] == "A":
cls = classA
else:
cls = classB
return CONVERTER.structure(dict_, cls)
CONVERTER.register_structure_hook(AOrB, structure_a_or_b)
if __name__ == "__main__":
a_dict = {"a_or_b": {"id_": "A"}}
b_dict = {"a_or_b": {"id_": "B"}}
a_inst = CONVERTER.structure(a_dict, Nested)
b_inst = CONVERTER.structure(b_dict, Nested) Output:
And if you toggle |
Ah, the problem is that you're using You're not the first person to come asking for this, so adding explicit support for |
Thanks for getting back to me, and thanks for the reference! Looking forward to a future release with |
22.1.0
3.9.11
20.04.4
LTSDescription
Using
detailed_validation=False
with aGenConverter
breaks my application after upgrading tocattrs
22.1.0
, but everything works swimingly withdetailed_validation=True
.What I Did
Changed from:
to:
At the moment, I don't have time to get a minimum reproducible example put together, and I can't share my code or full stack traces.
However, I can shore portions of it. It seems that the error occurs in the generated code that
cattrs
creates for myGenConverter
instance. Specifically, it looks like the method signature for instantiating myattrs
classes is different underdetailed_validation=True
vs.False
, but I'm not 100% sure.Here's where the error occurs in
cattrs
:The error message seems to relate to bad arguments to the class signature:
If I find time, I'll try to get a minimal example together that I can share, but don't hold your breath :)
The text was updated successfully, but these errors were encountered: