(
self, v: Any, values: Dict[str, Any], loc: 'LocStr', cls: Optional['ModelOrDc']
)
| 1051 | raise RuntimeError(f'Could not convert dictionary to {original_cls.__name__!r}') from None |
| 1052 | |
| 1053 | def _validate_singleton( |
| 1054 | self, v: Any, values: Dict[str, Any], loc: 'LocStr', cls: Optional['ModelOrDc'] |
| 1055 | ) -> 'ValidateReturn': |
| 1056 | if self.sub_fields: |
| 1057 | if self.discriminator_key is not None: |
| 1058 | return self._validate_discriminated_union(v, values, loc, cls) |
| 1059 | |
| 1060 | errors = [] |
| 1061 | |
| 1062 | if self.model_config.smart_union and is_union(get_origin(self.type_)): |
| 1063 | # 1st pass: check if the value is an exact instance of one of the Union types |
| 1064 | # (e.g. to avoid coercing a bool into an int) |
| 1065 | for field in self.sub_fields: |
| 1066 | if v.__class__ is field.outer_type_: |
| 1067 | return v, None |
| 1068 | |
| 1069 | # 2nd pass: check if the value is an instance of any subclass of the Union types |
| 1070 | for field in self.sub_fields: |
| 1071 | # This whole logic will be improved later on to support more complex `isinstance` checks |
| 1072 | # It will probably be done once a strict mode is added and be something like: |
| 1073 | # ``` |
| 1074 | # value, error = field.validate(v, values, strict=True) |
| 1075 | # if error is None: |
| 1076 | # return value, None |
| 1077 | # ``` |
| 1078 | try: |
| 1079 | if isinstance(v, field.outer_type_): |
| 1080 | return v, None |
| 1081 | except TypeError: |
| 1082 | # compound type |
| 1083 | if lenient_isinstance(v, get_origin(field.outer_type_)): |
| 1084 | value, error = field.validate(v, values, loc=loc, cls=cls) |
| 1085 | if not error: |
| 1086 | return value, None |
| 1087 | |
| 1088 | # 1st pass by default or 3rd pass with `smart_union` enabled: |
| 1089 | # check if the value can be coerced into one of the Union types |
| 1090 | for field in self.sub_fields: |
| 1091 | value, error = field.validate(v, values, loc=loc, cls=cls) |
| 1092 | if error: |
| 1093 | errors.append(error) |
| 1094 | else: |
| 1095 | return value, None |
| 1096 | return v, errors |
| 1097 | else: |
| 1098 | return self._apply_validators(v, values, loc, cls, self.validators) |
| 1099 | |
| 1100 | def _validate_discriminated_union( |
| 1101 | self, v: Any, values: Dict[str, Any], loc: 'LocStr', cls: Optional['ModelOrDc'] |
no test coverage detected