Skip to content

Commit 1f125b7

Browse files
authored
change CastError to describe the isinstance failure (#5693)
* change `CastError` to describe the `isinstance` failure * special case `None` in cast error display * newsfragment * update error messages in tests
1 parent c369c0d commit 1f125b7

File tree

12 files changed

+126
-99
lines changed

12 files changed

+126
-99
lines changed

guide/src/conversions/traits.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ enum RustyEnum {
444444
# }
445445
```
446446

447-
If the input is neither a string nor an integer, the error message will be: `"'<INPUT_TYPE>' cannot be cast as 'str | int'"`.
447+
If the input is neither a string nor an integer, the error message will be: `"'<INPUT_TYPE>' is not an instance of 'str | int'"`.
448448

449449
### `#[derive(FromPyObject)]` Container Attributes
450450

newsfragments/5693.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Change `CastError` formatted message to directly describe the "is not an instance of" failure condition.

src/conversions/chrono.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -780,37 +780,37 @@ mod tests {
780780
let none = py.None().into_bound(py);
781781
assert_eq!(
782782
none.extract::<Duration>().unwrap_err().to_string(),
783-
"TypeError: 'NoneType' object cannot be cast as 'timedelta'"
783+
"TypeError: 'None' is not an instance of 'timedelta'"
784784
);
785785
assert_eq!(
786786
none.extract::<FixedOffset>().unwrap_err().to_string(),
787-
"TypeError: 'NoneType' object cannot be cast as 'tzinfo'"
787+
"TypeError: 'None' is not an instance of 'tzinfo'"
788788
);
789789
assert_eq!(
790790
none.extract::<Utc>().unwrap_err().to_string(),
791791
"ValueError: expected datetime.timezone.utc"
792792
);
793793
assert_eq!(
794794
none.extract::<NaiveTime>().unwrap_err().to_string(),
795-
"TypeError: 'NoneType' object cannot be cast as 'time'"
795+
"TypeError: 'None' is not an instance of 'time'"
796796
);
797797
assert_eq!(
798798
none.extract::<NaiveDate>().unwrap_err().to_string(),
799-
"TypeError: 'NoneType' object cannot be cast as 'date'"
799+
"TypeError: 'None' is not an instance of 'date'"
800800
);
801801
assert_eq!(
802802
none.extract::<NaiveDateTime>().unwrap_err().to_string(),
803-
"TypeError: 'NoneType' object cannot be cast as 'datetime'"
803+
"TypeError: 'None' is not an instance of 'datetime'"
804804
);
805805
assert_eq!(
806806
none.extract::<DateTime<Utc>>().unwrap_err().to_string(),
807-
"TypeError: 'NoneType' object cannot be cast as 'datetime'"
807+
"TypeError: 'None' is not an instance of 'datetime'"
808808
);
809809
assert_eq!(
810810
none.extract::<DateTime<FixedOffset>>()
811811
.unwrap_err()
812812
.to_string(),
813-
"TypeError: 'NoneType' object cannot be cast as 'datetime'"
813+
"TypeError: 'None' is not an instance of 'datetime'"
814814
);
815815
});
816816
}

src/conversions/jiff.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -580,31 +580,31 @@ mod tests {
580580
let none = py.None().into_bound(py);
581581
assert_eq!(
582582
none.extract::<Span>().unwrap_err().to_string(),
583-
"TypeError: 'NoneType' object cannot be cast as 'timedelta'"
583+
"TypeError: 'None' is not an instance of 'timedelta'"
584584
);
585585
assert_eq!(
586586
none.extract::<Offset>().unwrap_err().to_string(),
587-
"TypeError: 'NoneType' object cannot be cast as 'tzinfo'"
587+
"TypeError: 'None' is not an instance of 'tzinfo'"
588588
);
589589
assert_eq!(
590590
none.extract::<TimeZone>().unwrap_err().to_string(),
591-
"TypeError: 'NoneType' object cannot be cast as 'tzinfo'"
591+
"TypeError: 'None' is not an instance of 'tzinfo'"
592592
);
593593
assert_eq!(
594594
none.extract::<Time>().unwrap_err().to_string(),
595-
"TypeError: 'NoneType' object cannot be cast as 'time'"
595+
"TypeError: 'None' is not an instance of 'time'"
596596
);
597597
assert_eq!(
598598
none.extract::<Date>().unwrap_err().to_string(),
599-
"TypeError: 'NoneType' object cannot be cast as 'date'"
599+
"TypeError: 'None' is not an instance of 'date'"
600600
);
601601
assert_eq!(
602602
none.extract::<DateTime>().unwrap_err().to_string(),
603-
"TypeError: 'NoneType' object cannot be cast as 'datetime'"
603+
"TypeError: 'None' is not an instance of 'datetime'"
604604
);
605605
assert_eq!(
606606
none.extract::<Zoned>().unwrap_err().to_string(),
607-
"TypeError: 'NoneType' object cannot be cast as 'datetime'"
607+
"TypeError: 'None' is not an instance of 'datetime'"
608608
);
609609
});
610610
}

src/conversions/smallvec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ mod tests {
138138
let sv: PyResult<SmallVec<[u64; 8]>> = dict.extract();
139139
assert_eq!(
140140
sv.unwrap_err().to_string(),
141-
"TypeError: 'dict' object cannot be cast as 'Sequence'"
141+
"TypeError: 'dict' object is not an instance of 'Sequence'"
142142
);
143143
});
144144
}

0 commit comments

Comments
 (0)