From 478e38eaddeebc4b79ec39db5f7257f0a756b8ea Mon Sep 17 00:00:00 2001 From: sydney-runkle Date: Wed, 15 Nov 2023 12:01:43 -0600 Subject: [PATCH 1/2] fixing issue with custom init and unions, adding appropriate test --- src/validators/model.rs | 5 +++- tests/validators/test_model_init.py | 45 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/validators/model.rs b/src/validators/model.rs index 0299ce5d8..dc929ecf9 100644 --- a/src/validators/model.rs +++ b/src/validators/model.rs @@ -257,7 +257,10 @@ impl ModelValidator { // this work with from_attributes, and would essentially allow you to // handle init vars by adding them to the __init__ signature. if let Some(kwargs) = input.as_kwargs(py) { - return Ok(self.class.call(py, (), Some(kwargs))?); + return Ok(self + .class + .call(py, (), Some(kwargs)) + .map_err(|e| convert_err(py, e, input))?); } } diff --git a/tests/validators/test_model_init.py b/tests/validators/test_model_init.py index 5521f8da4..b0c28dc86 100644 --- a/tests/validators/test_model_init.py +++ b/tests/validators/test_model_init.py @@ -479,3 +479,48 @@ def _wrap_validator(cls, v, validator, info): gc.collect() assert ref() is None + + +def test_model_custom_init_with_union() -> None: + class A: + def __init__(self, **kwargs): + assert 'a' in kwargs + self.a = kwargs.get('a') + + class B: + def __init__(self, **kwargs): + assert 'b' in kwargs + self.b = kwargs.get('b') + + schema = { + 'type': 'union', + 'choices': [ + { + 'type': 'model', + 'cls': A, + 'schema': { + 'type': 'model-fields', + 'fields': {'a': {'type': 'model-field', 'schema': {'type': 'bool'}}}, + 'model_name': 'A', + }, + 'custom_init': True, + 'ref': '__main__.A:4947206928', + }, + { + 'type': 'model', + 'cls': B, + 'schema': { + 'type': 'model-fields', + 'fields': {'b': {'type': 'model-field', 'schema': {'type': 'bool'}}}, + 'model_name': 'B', + }, + 'custom_init': True, + 'ref': '__main__.B:4679932848', + }, + ], + } + + validator = SchemaValidator(schema) + + assert validator.validate_python({'a': False}).a is False + assert validator.validate_python({'b': True}).b is True From d48a9c72516f6b82468ab6abe6cfb5069e2a4404 Mon Sep 17 00:00:00 2001 From: sydney-runkle Date: Wed, 15 Nov 2023 12:08:11 -0600 Subject: [PATCH 2/2] linting --- src/validators/model.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/validators/model.rs b/src/validators/model.rs index dc929ecf9..a571ccd9e 100644 --- a/src/validators/model.rs +++ b/src/validators/model.rs @@ -257,10 +257,10 @@ impl ModelValidator { // this work with from_attributes, and would essentially allow you to // handle init vars by adding them to the __init__ signature. if let Some(kwargs) = input.as_kwargs(py) { - return Ok(self + return self .class .call(py, (), Some(kwargs)) - .map_err(|e| convert_err(py, e, input))?); + .map_err(|e| convert_err(py, e, input)); } }