Skip to content

Commit 0d60f15

Browse files
committed
fix clippy::match_same_arms lint
1 parent 28f25b5 commit 0d60f15

File tree

9 files changed

+46
-61
lines changed

9 files changed

+46
-61
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ float_cmp = "allow"
9292
fn_params_excessive_bools = "allow"
9393
if_not_else = "allow"
9494
match_bool = "allow"
95-
match_same_arms = "allow"
9695
missing_errors_doc = "allow"
9796
missing_panics_doc = "allow"
9897
module_name_repetitions = "allow"

src/errors/types.rs

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ impl ErrorType {
480480
}
481481

482482
pub fn message_template_python(&self) -> &'static str {
483+
#[allow(clippy::match_same_arms)] // much nicer to have the messages explicitly listed
483484
match self {
484485
Self::NoSuchAttribute {..} => "Object has no attribute '{attribute}'",
485486
Self::JsonInvalid {..} => "Invalid JSON: {error}",
@@ -636,12 +637,24 @@ impl ErrorType {
636637
};
637638
match self {
638639
Self::NoSuchAttribute { attribute, .. } => render!(tmpl, attribute),
639-
Self::JsonInvalid { error, .. } => render!(tmpl, error),
640+
Self::JsonInvalid { error, .. }
641+
| Self::GetAttributeError { error, .. }
642+
| Self::IterationError { error, .. }
643+
| Self::DatetimeObjectInvalid { error, .. }
644+
| Self::UrlParsing { error, .. }
645+
| Self::UuidParsing { error, .. } => render!(tmpl, error),
646+
Self::MappingType { error, .. }
647+
| Self::DateParsing { error, .. }
648+
| Self::DateFromDatetimeParsing { error, .. }
649+
| Self::TimeParsing { error, .. }
650+
| Self::DatetimeParsing { error, .. }
651+
| Self::DatetimeFromDateParsing { error, .. }
652+
| Self::TimeDeltaParsing { error, .. }
653+
| Self::UrlSyntaxViolation { error, .. } => render!(tmpl, error),
640654
Self::NeedsPythonObject { method_name, .. } => render!(tmpl, method_name),
641-
Self::GetAttributeError { error, .. } => render!(tmpl, error),
642-
Self::ModelType { class_name, .. } => render!(tmpl, class_name),
643-
Self::DataclassType { class_name, .. } => render!(tmpl, class_name),
644-
Self::DataclassExactType { class_name, .. } => render!(tmpl, class_name),
655+
Self::ModelType { class_name, .. }
656+
| Self::DataclassType { class_name, .. }
657+
| Self::DataclassExactType { class_name, .. } => render!(tmpl, class_name),
645658
Self::GreaterThan { gt, .. } => to_string_render!(tmpl, gt),
646659
Self::GreaterThanEqual { ge, .. } => to_string_render!(tmpl, ge),
647660
Self::LessThan { lt, .. } => to_string_render!(tmpl, lt),
@@ -666,26 +679,18 @@ impl ErrorType {
666679
let actual_length = actual_length.map_or(Cow::Borrowed("more"), |v| Cow::Owned(v.to_string()));
667680
to_string_render!(tmpl, field_type, max_length, actual_length, expected_plural,)
668681
}
669-
Self::IterationError { error, .. } => render!(tmpl, error),
670-
Self::StringTooShort { min_length, .. } => {
682+
Self::StringTooShort { min_length, .. } | Self::BytesTooShort { min_length, .. } => {
671683
let expected_plural = plural_s(*min_length);
672684
to_string_render!(tmpl, min_length, expected_plural)
673685
}
674-
Self::StringTooLong { max_length, .. } => {
686+
Self::StringTooLong { max_length, .. }
687+
| Self::BytesTooLong { max_length, .. }
688+
| Self::UrlTooLong { max_length, .. } => {
675689
let expected_plural = plural_s(*max_length);
676690
to_string_render!(tmpl, max_length, expected_plural)
677691
}
678692
Self::StringPatternMismatch { pattern, .. } => render!(tmpl, pattern),
679693
Self::Enum { expected, .. } => to_string_render!(tmpl, expected),
680-
Self::MappingType { error, .. } => render!(tmpl, error),
681-
Self::BytesTooShort { min_length, .. } => {
682-
let expected_plural = plural_s(*min_length);
683-
to_string_render!(tmpl, min_length, expected_plural)
684-
}
685-
Self::BytesTooLong { max_length, .. } => {
686-
let expected_plural = plural_s(*max_length);
687-
to_string_render!(tmpl, max_length, expected_plural)
688-
}
689694
Self::BytesInvalidEncoding {
690695
encoding,
691696
encoding_error,
@@ -709,33 +714,18 @@ impl ErrorType {
709714
..
710715
} => PydanticCustomError::format_message(message_template, context.as_ref().map(|c| c.bind(py))),
711716
Self::LiteralError { expected, .. } => render!(tmpl, expected),
712-
Self::DateParsing { error, .. } => render!(tmpl, error),
713-
Self::DateFromDatetimeParsing { error, .. } => render!(tmpl, error),
714-
Self::TimeParsing { error, .. } => render!(tmpl, error),
715-
Self::DatetimeParsing { error, .. } => render!(tmpl, error),
716-
Self::DatetimeFromDateParsing { error, .. } => render!(tmpl, error),
717-
Self::DatetimeObjectInvalid { error, .. } => render!(tmpl, error),
718717
Self::TimezoneOffset {
719718
tz_expected, tz_actual, ..
720719
} => to_string_render!(tmpl, tz_expected, tz_actual),
721-
Self::TimeDeltaParsing { error, .. } => render!(tmpl, error),
722-
Self::IsInstanceOf { class, .. } => render!(tmpl, class),
723-
Self::IsSubclassOf { class, .. } => render!(tmpl, class),
720+
Self::IsInstanceOf { class, .. } | Self::IsSubclassOf { class, .. } => render!(tmpl, class),
724721
Self::UnionTagInvalid {
725722
discriminator,
726723
tag,
727724
expected_tags,
728725
..
729726
} => render!(tmpl, discriminator, tag, expected_tags),
730727
Self::UnionTagNotFound { discriminator, .. } => render!(tmpl, discriminator),
731-
Self::UrlParsing { error, .. } => render!(tmpl, error),
732-
Self::UrlSyntaxViolation { error, .. } => render!(tmpl, error),
733-
Self::UrlTooLong { max_length, .. } => {
734-
let expected_plural = plural_s(*max_length);
735-
to_string_render!(tmpl, max_length, expected_plural)
736-
}
737728
Self::UrlScheme { expected_schemes, .. } => render!(tmpl, expected_schemes),
738-
Self::UuidParsing { error, .. } => render!(tmpl, error),
739729
Self::UuidVersion { expected_version, .. } => to_string_render!(tmpl, expected_version),
740730
Self::DecimalMaxDigits { max_digits, .. } => {
741731
let expected_plural = plural_s(*max_digits);

src/input/datetime.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ impl<'py> IntoPyObject<'py> for EitherTimedelta<'py> {
113113
fn into_pyobject(self, py: Python<'py>) -> PyResult<Self::Output> {
114114
match self {
115115
Self::Raw(duration) => duration_as_pytimedelta(py, &duration),
116-
Self::PyExact(py_timedelta) => Ok(py_timedelta),
117-
Self::PySubclass(py_timedelta) => Ok(py_timedelta),
116+
Self::PyExact(py_timedelta) | Self::PySubclass(py_timedelta) => Ok(py_timedelta),
118117
}
119118
}
120119
}

src/serializers/extra.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,8 @@ impl From<Option<&str>> for SerMode {
372372
fn from(s: Option<&str>) -> Self {
373373
match s {
374374
Some("json") => SerMode::Json,
375-
Some("python") => SerMode::Python,
375+
Some("python") | None => SerMode::Python,
376376
Some(other) => SerMode::Other(other.to_string()),
377-
None => SerMode::Python,
378377
}
379378
}
380379
}

src/serializers/infer.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ pub(crate) fn infer_to_python_known<'py>(
168168
let either_delta = EitherTimedelta::try_from(value)?;
169169
state.config.temporal_mode.timedelta_to_json(value.py(), either_delta)?
170170
}
171-
ObType::Url => serialize_via_str(value, serialize_to_python())?,
172-
ObType::MultiHostUrl => serialize_via_str(value, serialize_to_python())?,
171+
ObType::Url | ObType::MultiHostUrl | ObType::Path => serialize_via_str(value, serialize_to_python())?,
173172
ObType::Uuid => {
174173
let uuid = super::type_serializers::uuid::uuid_to_string(value)?;
175174
uuid.into_py_any(py)?
@@ -202,7 +201,6 @@ pub(crate) fn infer_to_python_known<'py>(
202201
let complex_str = type_serializers::complex::complex_to_str(v);
203202
complex_str.into_py_any(py)?
204203
}
205-
ObType::Path => serialize_via_str(value, serialize_to_python())?,
206204
ObType::Pattern => serialize_pattern(value, serialize_to_python())?,
207205
ObType::Unknown => {
208206
if let Some(fallback) = state.extra.fallback {
@@ -415,8 +413,9 @@ pub(crate) fn infer_serialize_known<'py, S: Serializer>(
415413
let either_delta = EitherTimedelta::try_from(value).map_err(py_err_se_err)?;
416414
state.config.temporal_mode.timedelta_serialize(either_delta, serializer)
417415
}
418-
ObType::Url => serialize_via_str(value, serialize_to_json(serializer)).map_err(unwrap_ser_error),
419-
ObType::MultiHostUrl => serialize_via_str(value, serialize_to_json(serializer)).map_err(unwrap_ser_error),
416+
ObType::Url | ObType::MultiHostUrl | ObType::Path => {
417+
serialize_via_str(value, serialize_to_json(serializer)).map_err(unwrap_ser_error)
418+
}
420419
ObType::PydanticSerializable => {
421420
call_pydantic_serializer(value, state, serialize_to_json(serializer)).map_err(unwrap_ser_error)
422421
}
@@ -447,7 +446,6 @@ pub(crate) fn infer_serialize_known<'py, S: Serializer>(
447446
}
448447
seq.end()
449448
}
450-
ObType::Path => serialize_via_str(value, serialize_to_json(serializer)).map_err(unwrap_ser_error),
451449
ObType::Pattern => serialize_pattern(value, serialize_to_json(serializer)).map_err(unwrap_ser_error),
452450
ObType::Unknown => {
453451
if let Some(fallback) = state.extra.fallback {

src/serializers/ob_type.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl ObTypeLookup {
156156
ObType::Enum => self.enum_object.as_ptr() as usize == ob_type,
157157
ObType::Generator => self.generator_object.as_ptr() as usize == ob_type,
158158
ObType::Path => self.path_object.as_ptr() as usize == ob_type,
159-
ObType::Pattern => self.path_object.as_ptr() as usize == ob_type,
159+
ObType::Pattern => self.pattern_object.as_ptr() as usize == ob_type,
160160
ObType::Uuid => self.uuid_object.as_ptr() as usize == ob_type,
161161
ObType::Complex => self.complex == ob_type,
162162
ObType::Unknown => false,
@@ -429,12 +429,12 @@ impl PartialEq for ObType {
429429
} else {
430430
match (self, other) {
431431
// special cases for subclasses
432-
(Self::IntSubclass, Self::Int) => true,
433-
(Self::Int, Self::IntSubclass) => true,
434-
(Self::FloatSubclass, Self::Float) => true,
435-
(Self::Float, Self::FloatSubclass) => true,
436-
(Self::StrSubclass, Self::Str) => true,
437-
(Self::Str, Self::StrSubclass) => true,
432+
(Self::IntSubclass, Self::Int)
433+
| (Self::Int, Self::IntSubclass)
434+
| (Self::FloatSubclass, Self::Float)
435+
| (Self::Float, Self::FloatSubclass)
436+
| (Self::StrSubclass, Self::Str)
437+
| (Self::Str, Self::StrSubclass) => true,
438438
_ => false,
439439
}
440440
}

src/serializers/shared.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,19 @@ impl CombinedSerializer {
199199
)
200200
.map_err(|err| py_schema_error_type!("Error building `function-wrap` serializer:\n {}", err));
201201
}
202-
// applies to lists tuples and dicts, does not override the main schema `type`
203-
Some("include-exclude-sequence" | "include-exclude-dict") => (),
204-
// applies specifically to bytes, does not override the main schema `type`
205-
Some("base64") => (),
202+
Some(
203+
// applies to lists tuples and dicts, does not override the main schema `type`
204+
"include-exclude-sequence" | "include-exclude-dict"
205+
// applies specifically to bytes, does not override the main schema `type`
206+
| "base64"
207+
)
208+
// if `schema.serialization.type` is None, fall back to `schema.type`
209+
| None => (),
206210
Some(ser_type) => {
207211
// otherwise if `schema.serialization.type` is defined, use that with `find_serializer`
208212
// instead of `schema.type`. In this case it's an error if a serializer isn't found.
209213
return Self::find_serializer(ser_type, &ser_schema, config, definitions);
210214
}
211-
// if `schema.serialization.type` is None, fall back to `schema.type`
212-
None => (),
213215
};
214216
}
215217

src/validators/model.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ pub(super) enum Revalidate {
3434
impl Revalidate {
3535
pub fn from_str(s: Option<&str>) -> PyResult<Self> {
3636
match s {
37-
None => Ok(Self::Never),
3837
Some("always") => Ok(Self::Always),
39-
Some("never") => Ok(Self::Never),
38+
Some("never") | None => Ok(Self::Never),
4039
Some("subclass-instances") => Ok(Self::SubclassInstances),
4140
Some(s) => py_schema_err!("Invalid revalidate_instances value: {}", s),
4241
}

src/validators/with_default.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,14 @@ impl BuildValidator for WithDefaultValidator {
113113
.map(|s| s.to_str())
114114
.transpose()?
115115
{
116-
Some("raise") => OnError::Raise,
116+
Some("raise") | None => OnError::Raise,
117117
Some("omit") => OnError::Omit,
118118
Some("default") => {
119119
if matches!(default, DefaultType::None) {
120120
return py_schema_err!("'on_error = default' requires a `default` or `default_factory`");
121121
}
122122
OnError::Default
123123
}
124-
None => OnError::Raise,
125124
// schema validation means other values are impossible
126125
_ => unreachable!(),
127126
};

0 commit comments

Comments
 (0)