Skip to content

Commit b2d6244

Browse files
feat: add cargo-fmt and clippy to pre-commit hooks (#21)
* feat: add cargo-fmt and clippy to pre-commit hooks * fix: align cargo-fmt and clippy pre-commit hooks with CI * fix: add --all-targets to clippy in both pre-commit and CI
1 parent 73a6976 commit b2d6244

4 files changed

Lines changed: 58 additions & 42 deletions

File tree

.github/workflows/static-checks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
run: cargo fmt --check
5555

5656
- name: Run clippy
57-
run: cargo clippy --no-default-features -- -D warnings
57+
run: cargo clippy --all-targets --no-default-features -- -D warnings
5858

5959
cargo-deny:
6060
name: Cargo Deny

.pre-commit-config.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ repos:
3939
entry: uv run --frozen --offline lint-imports
4040
language: system
4141
pass_filenames: false
42+
- repo: local
43+
hooks:
44+
- id: cargo-fmt
45+
name: cargo-fmt
46+
always_run: true
47+
entry: uv run --frozen cargo fmt --check
48+
language: system
49+
pass_filenames: false
50+
- repo: local
51+
hooks:
52+
- id: clippy
53+
name: clippy
54+
always_run: true
55+
entry: uv run --frozen cargo clippy --all-targets --no-default-features -- -D warnings
56+
language: system
57+
pass_filenames: false
4258
- repo: https://github.com/codespell-project/codespell
4359
rev: v2.4.2
4460
hooks:

src/convert.rs

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ mod tests {
111111

112112
#[test]
113113
fn test_py_to_yaml_none() {
114-
pyo3::prepare_freethreaded_python();
115-
Python::with_gil(|py| {
114+
pyo3::Python::initialize();
115+
Python::attach(|py| {
116116
let none = py.None().into_bound(py);
117117
let val = py_to_yaml_value(&none).unwrap();
118118
assert_eq!(val, Value::Null);
@@ -121,8 +121,8 @@ mod tests {
121121

122122
#[test]
123123
fn test_py_to_yaml_bool() {
124-
pyo3::prepare_freethreaded_python();
125-
Python::with_gil(|py| {
124+
pyo3::Python::initialize();
125+
Python::attach(|py| {
126126
let t = true.into_pyobject(py).unwrap();
127127
assert_eq!(py_to_yaml_value(t.as_any()).unwrap(), Value::Bool(true));
128128
let f = false.into_pyobject(py).unwrap();
@@ -132,8 +132,8 @@ mod tests {
132132

133133
#[test]
134134
fn test_py_to_yaml_int() {
135-
pyo3::prepare_freethreaded_python();
136-
Python::with_gil(|py| {
135+
pyo3::Python::initialize();
136+
Python::attach(|py| {
137137
let i = 42i64.into_pyobject(py).unwrap().into_any();
138138
let val = py_to_yaml_value(&i).unwrap();
139139
assert_eq!(val, Value::Number(42.into()));
@@ -142,8 +142,8 @@ mod tests {
142142

143143
#[test]
144144
fn test_py_to_yaml_large_unsigned_int() {
145-
pyo3::prepare_freethreaded_python();
146-
Python::with_gil(|py| {
145+
pyo3::Python::initialize();
146+
Python::attach(|py| {
147147
// i64::MAX + 1 is a valid u64 but overflows i64
148148
let large = (i64::MAX as u64 + 1).into_pyobject(py).unwrap().into_any();
149149
let val = py_to_yaml_value(&large).unwrap();
@@ -156,21 +156,21 @@ mod tests {
156156

157157
#[test]
158158
fn test_py_to_yaml_float_finite() {
159-
pyo3::prepare_freethreaded_python();
160-
Python::with_gil(|py| {
161-
let f = 3.14f64.into_pyobject(py).unwrap().into_any();
159+
pyo3::Python::initialize();
160+
Python::attach(|py| {
161+
let f = 3.15f64.into_pyobject(py).unwrap().into_any();
162162
let val = py_to_yaml_value(&f).unwrap();
163163
match val {
164-
Value::Number(n) => assert_eq!(n.as_f64().unwrap(), 3.14),
164+
Value::Number(n) => assert_eq!(n.as_f64().unwrap(), 3.15),
165165
_ => panic!("expected Number"),
166166
}
167167
});
168168
}
169169

170170
#[test]
171171
fn test_py_to_yaml_float_nan() {
172-
pyo3::prepare_freethreaded_python();
173-
Python::with_gil(|py| {
172+
pyo3::Python::initialize();
173+
Python::attach(|py| {
174174
let nan = f64::NAN.into_pyobject(py).unwrap().into_any();
175175
let val = py_to_yaml_value(&nan).unwrap();
176176
match val {
@@ -182,8 +182,8 @@ mod tests {
182182

183183
#[test]
184184
fn test_py_to_yaml_float_inf() {
185-
pyo3::prepare_freethreaded_python();
186-
Python::with_gil(|py| {
185+
pyo3::Python::initialize();
186+
Python::attach(|py| {
187187
let inf = f64::INFINITY.into_pyobject(py).unwrap().into_any();
188188
let val = py_to_yaml_value(&inf).unwrap();
189189
match val {
@@ -195,8 +195,8 @@ mod tests {
195195

196196
#[test]
197197
fn test_py_to_yaml_float_neg_inf() {
198-
pyo3::prepare_freethreaded_python();
199-
Python::with_gil(|py| {
198+
pyo3::Python::initialize();
199+
Python::attach(|py| {
200200
let neg_inf = f64::NEG_INFINITY.into_pyobject(py).unwrap().into_any();
201201
let val = py_to_yaml_value(&neg_inf).unwrap();
202202
match val {
@@ -211,18 +211,18 @@ mod tests {
211211

212212
#[test]
213213
fn test_py_to_yaml_string() {
214-
pyo3::prepare_freethreaded_python();
215-
Python::with_gil(|py| {
214+
pyo3::Python::initialize();
215+
Python::attach(|py| {
216216
let s = "hello".into_pyobject(py).unwrap().into_any();
217217
assert_eq!(py_to_yaml_value(&s).unwrap(), Value::String("hello".into()));
218218
});
219219
}
220220

221221
#[test]
222222
fn test_py_to_yaml_list() {
223-
pyo3::prepare_freethreaded_python();
224-
Python::with_gil(|py| {
225-
let list = PyList::new(py, &[1i64, 2, 3]).unwrap();
223+
pyo3::Python::initialize();
224+
Python::attach(|py| {
225+
let list = PyList::new(py, [1i64, 2, 3]).unwrap();
226226
let val = py_to_yaml_value(list.as_any()).unwrap();
227227
assert_eq!(
228228
val,
@@ -237,8 +237,8 @@ mod tests {
237237

238238
#[test]
239239
fn test_py_to_yaml_dict() {
240-
pyo3::prepare_freethreaded_python();
241-
Python::with_gil(|py| {
240+
pyo3::Python::initialize();
241+
Python::attach(|py| {
242242
let dict = PyDict::new(py);
243243
dict.set_item("key", "value").unwrap();
244244
let val = py_to_yaml_value(dict.as_any()).unwrap();
@@ -250,53 +250,53 @@ mod tests {
250250

251251
#[test]
252252
fn test_py_to_yaml_unsupported_type_rejected() {
253-
pyo3::prepare_freethreaded_python();
254-
Python::with_gil(|py| {
253+
pyo3::Python::initialize();
254+
Python::attach(|py| {
255255
let set = py.eval(pyo3::ffi::c_str!("set()"), None, None).unwrap();
256256
assert!(py_to_yaml_value(&set).is_err());
257257
});
258258
}
259259

260260
#[test]
261261
fn test_yaml_to_py_null() {
262-
pyo3::prepare_freethreaded_python();
263-
Python::with_gil(|py| {
262+
pyo3::Python::initialize();
263+
Python::attach(|py| {
264264
let obj = yaml_value_to_py(py, &Value::Null).unwrap();
265265
assert!(obj.bind(py).is_none());
266266
});
267267
}
268268

269269
#[test]
270270
fn test_yaml_to_py_bool() {
271-
pyo3::prepare_freethreaded_python();
272-
Python::with_gil(|py| {
271+
pyo3::Python::initialize();
272+
Python::attach(|py| {
273273
let obj = yaml_value_to_py(py, &Value::Bool(true)).unwrap();
274274
assert!(obj.extract::<bool>(py).unwrap());
275275
});
276276
}
277277

278278
#[test]
279279
fn test_yaml_to_py_number_i64() {
280-
pyo3::prepare_freethreaded_python();
281-
Python::with_gil(|py| {
280+
pyo3::Python::initialize();
281+
Python::attach(|py| {
282282
let obj = yaml_value_to_py(py, &Value::Number(42.into())).unwrap();
283283
assert_eq!(obj.extract::<i64>(py).unwrap(), 42);
284284
});
285285
}
286286

287287
#[test]
288288
fn test_yaml_to_py_string() {
289-
pyo3::prepare_freethreaded_python();
290-
Python::with_gil(|py| {
289+
pyo3::Python::initialize();
290+
Python::attach(|py| {
291291
let obj = yaml_value_to_py(py, &Value::String("hello".into())).unwrap();
292292
assert_eq!(obj.extract::<String>(py).unwrap(), "hello");
293293
});
294294
}
295295

296296
#[test]
297297
fn test_yaml_to_py_sequence() {
298-
pyo3::prepare_freethreaded_python();
299-
Python::with_gil(|py| {
298+
pyo3::Python::initialize();
299+
Python::attach(|py| {
300300
let seq = Value::Sequence(vec![Value::Number(1.into()), Value::Number(2.into())]);
301301
let obj = yaml_value_to_py(py, &seq).unwrap();
302302
let list = obj.extract::<Vec<i64>>(py).unwrap();
@@ -306,8 +306,8 @@ mod tests {
306306

307307
#[test]
308308
fn test_yaml_to_py_tagged_strips_tag() {
309-
pyo3::prepare_freethreaded_python();
310-
Python::with_gil(|py| {
309+
pyo3::Python::initialize();
310+
Python::attach(|py| {
311311
let tagged = Value::Tagged(Box::new(serde_yaml::value::TaggedValue {
312312
tag: serde_yaml::value::Tag::new("!custom"),
313313
value: Value::String("inner".into()),

src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ mod tests {
268268

269269
#[test]
270270
fn test_route_negative_int_error_message() {
271-
pyo3::prepare_freethreaded_python();
272-
Python::with_gil(|py| {
271+
pyo3::Python::initialize();
272+
Python::attach(|py| {
273273
let neg = (-1i64).into_pyobject(py).unwrap().into_any();
274274
let parts = vec![neg];
275275
let list = pyo3::types::PyList::new(py, &parts).unwrap();

0 commit comments

Comments
 (0)