Skip to content

Commit

Permalink
refactor LookupKey logic to reduce redundancy (#1635)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt authored Feb 13, 2025
1 parent 7da4798 commit 741961c
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 184 deletions.
2 changes: 1 addition & 1 deletion src/common/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use pyo3::{PyTraverseError, PyVisit};
use crate::lookup_key::LookupKey;
use crate::py_gc::PyGcTraverse;

#[derive(Debug, Clone)]
#[derive(Debug)]
pub enum Discriminator {
/// use `LookupKey` to find the tag, same as we do to find values in typed_dict aliases
LookupKey(LookupKey),
Expand Down
27 changes: 0 additions & 27 deletions src/errors/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use pyo3::types::{PyList, PyTuple};
use serde::ser::SerializeSeq;
use serde::{Serialize, Serializer};

use crate::lookup_key::{LookupPath, PathItem};

/// Used to store individual items of the error location, e.g. a string for key/field names
/// or a number for array indices.
#[derive(Clone, Eq, PartialEq, IntoPyObjectRef)]
Expand Down Expand Up @@ -71,20 +69,6 @@ impl From<usize> for LocItem {
}
}

/// eventually it might be good to combine PathItem and LocItem
impl From<PathItem> for LocItem {
fn from(path_item: PathItem) -> Self {
match path_item {
PathItem::S(s, _) => s.into(),
PathItem::Pos(val) => val.into(),
PathItem::Neg(val) => {
let neg_value = -(val as i64);
neg_value.into()
}
}
}
}

impl Serialize for LocItem {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down Expand Up @@ -137,17 +121,6 @@ impl<'py> IntoPyObject<'py> for &'_ Location {
}
}

impl From<&LookupPath> for Location {
fn from(lookup_path: &LookupPath) -> Self {
let v = lookup_path
.iter()
.rev()
.map(|path_item| path_item.clone().into())
.collect();
Self::List(v)
}
}

impl fmt::Display for Location {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand Down
2 changes: 1 addition & 1 deletion src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod validation_exception;
mod value_exception;

pub use self::line_error::{InputValue, ToErrorValue, ValError, ValLineError, ValResult};
pub use self::location::LocItem;
pub use self::location::{LocItem, Location};
pub use self::types::{list_all_errors, ErrorType, ErrorTypeDefaults, Number};
pub use self::validation_exception::{PyLineError, ValidationError};
pub use self::value_exception::{PydanticCustomError, PydanticKnownError, PydanticOmit, PydanticUseDefault};
Expand Down
Loading

0 comments on commit 741961c

Please # to comment.