Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/serializers/type_serializers/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::borrow::Cow;

use crate::build_tools::py_schema_err;
use crate::definitions::DefinitionsBuilder;
use crate::tools::SchemaDict;
use crate::tools::{SchemaDict, UNION_ERR_SMALLVEC_CAPACITY};
use crate::PydanticSerializationUnexpectedValue;

use super::{
Expand Down Expand Up @@ -79,7 +79,7 @@ impl TypeSerializer for UnionSerializer {
// try the serializers in left to right order with error_on fallback=true
let mut new_extra = extra.clone();
new_extra.check = SerCheck::Strict;
let mut errors: SmallVec<[PyErr; 16]> = SmallVec::new();
let mut errors: SmallVec<[PyErr; UNION_ERR_SMALLVEC_CAPACITY]> = SmallVec::new();

for comb_serializer in &self.choices {
match comb_serializer.to_python(value, include, exclude, &new_extra) {
Expand Down Expand Up @@ -114,7 +114,7 @@ impl TypeSerializer for UnionSerializer {
fn json_key<'a>(&self, key: &'a Bound<'_, PyAny>, extra: &Extra) -> PyResult<Cow<'a, str>> {
let mut new_extra = extra.clone();
new_extra.check = SerCheck::Strict;
let mut errors: SmallVec<[PyErr; 16]> = SmallVec::new();
let mut errors: SmallVec<[PyErr; UNION_ERR_SMALLVEC_CAPACITY]> = SmallVec::new();

for comb_serializer in &self.choices {
match comb_serializer.json_key(key, &new_extra) {
Expand Down Expand Up @@ -157,7 +157,7 @@ impl TypeSerializer for UnionSerializer {
let py = value.py();
let mut new_extra = extra.clone();
new_extra.check = SerCheck::Strict;
let mut errors: SmallVec<[PyErr; 16]> = SmallVec::new();
let mut errors: SmallVec<[PyErr; UNION_ERR_SMALLVEC_CAPACITY]> = SmallVec::new();

for comb_serializer in &self.choices {
match comb_serializer.to_python(value, include, exclude, &new_extra) {
Expand Down
2 changes: 2 additions & 0 deletions src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,5 @@ pub(crate) fn new_py_string<'py>(py: Python<'py>, s: &str, cache_str: StringCach
pystring_fast_new(py, s, ascii_only)
}
}

pub(crate) const UNION_ERR_SMALLVEC_CAPACITY: usize = 4;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might have just called this SMALL_UNION_THRESHOLD, but this is fine too 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll rename in another PR - thinking of moving this to common, which we introduce in another one of the various union PRs :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See a04e297

4 changes: 2 additions & 2 deletions src/validators/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::errors::{ErrorType, ToErrorValue, ValError, ValLineError, ValResult};
use crate::input::{BorrowInput, Input, ValidatedDict};
use crate::lookup_key::LookupKey;
use crate::py_gc::PyGcTraverse;
use crate::tools::SchemaDict;
use crate::tools::{SchemaDict, UNION_ERR_SMALLVEC_CAPACITY};

use super::custom_error::CustomError;
use super::literal::LiteralLookup;
Expand Down Expand Up @@ -249,7 +249,7 @@ struct ChoiceLineErrors<'a> {

enum MaybeErrors<'a> {
Custom(&'a CustomError),
Errors(SmallVec<[ChoiceLineErrors<'a>; 4]>),
Errors(SmallVec<[ChoiceLineErrors<'a>; UNION_ERR_SMALLVEC_CAPACITY]>),
}

impl<'a> MaybeErrors<'a> {
Expand Down