Skip to content

Commit 6d2ff40

Browse files
c
1 parent 3e5d022 commit 6d2ff40

1 file changed

Lines changed: 85 additions & 79 deletions

File tree

  • crates/polars-python/src/io

crates/polars-python/src/io/mod.rs

Lines changed: 85 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl PyScanOptions<'_> {
5959
.call_method0("_default")
6060
.unwrap();
6161

62-
let out = Self(ob).extract_cast_options()?;
62+
let out = extract_cast_options_impl(ob)?;
6363

6464
// The default policy should match ERROR_ON_MISMATCH (but this can change).
6565
debug_assert_eq!(&out, &CastColumnsPolicy::ERROR_ON_MISMATCH);
@@ -70,100 +70,106 @@ impl PyScanOptions<'_> {
7070
return Ok(out.clone());
7171
}
7272

73-
let integer_upcast = match &*ob
74-
.getattr(intern!(py, "integer_cast"))?
75-
.extract::<PyBackedStr>()?
76-
{
77-
"upcast" => true,
78-
"forbid" => false,
73+
extract_cast_options_impl(ob)
74+
}
75+
}
76+
77+
fn extract_cast_options_impl(ob: Bound<'_, PyAny>) -> PyResult<CastColumnsPolicy> {
78+
let py = ob.py();
79+
80+
let integer_upcast = match &*ob
81+
.getattr(intern!(py, "integer_cast"))?
82+
.extract::<PyBackedStr>()?
83+
{
84+
"upcast" => true,
85+
"forbid" => false,
86+
v => {
87+
return Err(PyValueError::new_err(format!(
88+
"unknown option for integer_cast: {}",
89+
v
90+
)));
91+
},
92+
};
93+
94+
let mut float_upcast = false;
95+
let mut float_downcast = false;
96+
97+
let float_cast_object = ob.getattr(intern!(py, "float_cast"))?;
98+
99+
parse_multiple_options("float_cast", float_cast_object, |v| {
100+
match v {
101+
"forbid" => {},
102+
"upcast" => float_upcast = true,
103+
"downcast" => float_downcast = true,
79104
v => {
80105
return Err(PyValueError::new_err(format!(
81-
"unknown option for integer_cast: {}",
106+
"unknown option for float_cast: {}",
82107
v
83108
)));
84109
},
85-
};
86-
87-
let mut float_upcast = false;
88-
let mut float_downcast = false;
89-
90-
let float_cast_object = ob.getattr(intern!(py, "float_cast"))?;
91-
92-
parse_multiple_options("float_cast", float_cast_object, |v| {
93-
match v {
94-
"forbid" => {},
95-
"upcast" => float_upcast = true,
96-
"downcast" => float_downcast = true,
97-
v => {
98-
return Err(PyValueError::new_err(format!(
99-
"unknown option for float_cast: {}",
100-
v
101-
)));
102-
},
103-
}
104-
105-
Ok(())
106-
})?;
107-
108-
let mut datetime_nanoseconds_downcast = false;
109-
let mut datetime_convert_timezone = false;
110-
111-
let datetime_cast_object = ob.getattr(intern!(py, "datetime_cast"))?;
110+
}
112111

113-
parse_multiple_options("datetime_cast", datetime_cast_object, |v| {
114-
match v {
115-
"forbid" => {},
116-
"nanosecond-downcast" => datetime_nanoseconds_downcast = true,
117-
"convert-timezone" => datetime_convert_timezone = true,
118-
v => {
119-
return Err(PyValueError::new_err(format!(
120-
"unknown option for datetime_cast: {}",
121-
v
122-
)));
123-
},
124-
};
112+
Ok(())
113+
})?;
125114

126-
Ok(())
127-
})?;
115+
let mut datetime_nanoseconds_downcast = false;
116+
let mut datetime_convert_timezone = false;
128117

129-
let missing_struct_fields = match &*ob
130-
.getattr(intern!(py, "missing_struct_fields"))?
131-
.extract::<PyBackedStr>()?
132-
{
133-
"insert" => MissingColumnsPolicy::Insert,
134-
"raise" => MissingColumnsPolicy::Raise,
135-
v => {
136-
return Err(PyValueError::new_err(format!(
137-
"unknown option for missing_struct_fields: {}",
138-
v
139-
)));
140-
},
141-
};
118+
let datetime_cast_object = ob.getattr(intern!(py, "datetime_cast"))?;
142119

143-
let extra_struct_fields = match &*ob
144-
.getattr(intern!(py, "extra_struct_fields"))?
145-
.extract::<PyBackedStr>()?
146-
{
147-
"ignore" => ExtraColumnsPolicy::Ignore,
148-
"raise" => ExtraColumnsPolicy::Raise,
120+
parse_multiple_options("datetime_cast", datetime_cast_object, |v| {
121+
match v {
122+
"forbid" => {},
123+
"nanosecond-downcast" => datetime_nanoseconds_downcast = true,
124+
"convert-timezone" => datetime_convert_timezone = true,
149125
v => {
150126
return Err(PyValueError::new_err(format!(
151-
"unknown option for extra_struct_fields: {}",
127+
"unknown option for datetime_cast: {}",
152128
v
153129
)));
154130
},
155131
};
156132

157-
Ok(CastColumnsPolicy {
158-
integer_upcast,
159-
float_upcast,
160-
float_downcast,
161-
datetime_nanoseconds_downcast,
162-
datetime_convert_timezone,
163-
missing_struct_fields,
164-
extra_struct_fields,
165-
})
166-
}
133+
Ok(())
134+
})?;
135+
136+
let missing_struct_fields = match &*ob
137+
.getattr(intern!(py, "missing_struct_fields"))?
138+
.extract::<PyBackedStr>()?
139+
{
140+
"insert" => MissingColumnsPolicy::Insert,
141+
"raise" => MissingColumnsPolicy::Raise,
142+
v => {
143+
return Err(PyValueError::new_err(format!(
144+
"unknown option for missing_struct_fields: {}",
145+
v
146+
)));
147+
},
148+
};
149+
150+
let extra_struct_fields = match &*ob
151+
.getattr(intern!(py, "extra_struct_fields"))?
152+
.extract::<PyBackedStr>()?
153+
{
154+
"ignore" => ExtraColumnsPolicy::Ignore,
155+
"raise" => ExtraColumnsPolicy::Raise,
156+
v => {
157+
return Err(PyValueError::new_err(format!(
158+
"unknown option for extra_struct_fields: {}",
159+
v
160+
)));
161+
},
162+
};
163+
164+
Ok(CastColumnsPolicy {
165+
integer_upcast,
166+
float_upcast,
167+
float_downcast,
168+
datetime_nanoseconds_downcast,
169+
datetime_convert_timezone,
170+
missing_struct_fields,
171+
extra_struct_fields,
172+
})
167173
}
168174

169175
fn parse_multiple_options(

0 commit comments

Comments
 (0)