@@ -59,26 +59,16 @@ impl Constructor for PyInterpolation {
5959 type Args = InterpolationArgs ;
6060
6161 fn py_new ( _cls : & Py < PyType > , args : Self :: Args , vm : & VirtualMachine ) -> PyResult < Self > {
62- let conversion = match args. conversion {
63- OptionalArg :: Present ( c) => {
64- if vm. is_none ( & c) {
65- vm. ctx . none ( )
66- } else {
67- let s = c. downcast :: < PyStr > ( ) . map_err ( |_| {
68- vm. new_type_error (
69- "Interpolation() argument 'conversion' must be str or None" ,
70- )
71- } ) ?;
72- let s_str = s. as_str ( ) ;
73- if s_str. len ( ) != 1 || !matches ! ( s_str. chars( ) . next( ) , Some ( 's' | 'r' | 'a' ) ) {
74- return Err ( vm. new_value_error (
75- "Interpolation() argument 'conversion' must be one of 's', 'a' or 'r'" ,
76- ) ) ;
77- }
78- s. into ( )
79- }
62+ let conversion: PyObjectRef = if let Some ( s) = args. conversion {
63+ let s_str = s. as_str ( ) ;
64+ if s_str. len ( ) != 1 || !matches ! ( s_str. chars( ) . next( ) , Some ( 's' | 'r' | 'a' ) ) {
65+ return Err ( vm. new_value_error (
66+ "Interpolation() argument 'conversion' must be one of 's', 'a' or 'r'" ,
67+ ) ) ;
8068 }
81- OptionalArg :: Missing => vm. ctx . none ( ) ,
69+ s. into ( )
70+ } else {
71+ vm. ctx . none ( )
8272 } ;
8373
8474 let expression = args
@@ -103,8 +93,12 @@ pub struct InterpolationArgs {
10393 value : PyObjectRef ,
10494 #[ pyarg( any, optional) ]
10595 expression : OptionalArg < PyStrRef > ,
106- #[ pyarg( any, optional) ]
107- conversion : OptionalArg < PyObjectRef > ,
96+ #[ pyarg(
97+ any,
98+ optional,
99+ error_msg = "Interpolation() argument 'conversion' must be str or None"
100+ ) ]
101+ conversion : Option < PyStrRef > ,
108102 #[ pyarg( any, optional) ]
109103 format_spec : OptionalArg < PyStrRef > ,
110104}
0 commit comments