Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/static-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
run: cargo fmt --check

- name: Run clippy
run: cargo clippy --no-default-features -- -D warnings
run: cargo clippy --all-targets --no-default-features -- -D warnings

cargo-deny:
name: Cargo Deny
Expand Down
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ repos:
entry: uv run --frozen --offline lint-imports
language: system
pass_filenames: false
- repo: local
hooks:
- id: cargo-fmt
name: cargo-fmt
always_run: true
entry: uv run --frozen cargo fmt --check
language: system
pass_filenames: false
- repo: local
hooks:
- id: clippy
name: clippy
always_run: true
entry: uv run --frozen cargo clippy --all-targets --no-default-features -- -D warnings
language: system
pass_filenames: false
- repo: https://github.com/codespell-project/codespell
rev: v2.4.2
hooks:
Expand Down
78 changes: 39 additions & 39 deletions src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ mod tests {

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

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

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

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

#[test]
fn test_py_to_yaml_float_finite() {
pyo3::prepare_freethreaded_python();
Python::with_gil(|py| {
let f = 3.14f64.into_pyobject(py).unwrap().into_any();
pyo3::Python::initialize();
Python::attach(|py| {
let f = 3.15f64.into_pyobject(py).unwrap().into_any();
let val = py_to_yaml_value(&f).unwrap();
match val {
Value::Number(n) => assert_eq!(n.as_f64().unwrap(), 3.14),
Value::Number(n) => assert_eq!(n.as_f64().unwrap(), 3.15),
_ => panic!("expected Number"),
}
});
}

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

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

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

#[test]
fn test_py_to_yaml_string() {
pyo3::prepare_freethreaded_python();
Python::with_gil(|py| {
pyo3::Python::initialize();
Python::attach(|py| {
let s = "hello".into_pyobject(py).unwrap().into_any();
assert_eq!(py_to_yaml_value(&s).unwrap(), Value::String("hello".into()));
});
}

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

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

#[test]
fn test_py_to_yaml_unsupported_type_rejected() {
pyo3::prepare_freethreaded_python();
Python::with_gil(|py| {
pyo3::Python::initialize();
Python::attach(|py| {
let set = py.eval(pyo3::ffi::c_str!("set()"), None, None).unwrap();
assert!(py_to_yaml_value(&set).is_err());
});
}

#[test]
fn test_yaml_to_py_null() {
pyo3::prepare_freethreaded_python();
Python::with_gil(|py| {
pyo3::Python::initialize();
Python::attach(|py| {
let obj = yaml_value_to_py(py, &Value::Null).unwrap();
assert!(obj.bind(py).is_none());
});
}

#[test]
fn test_yaml_to_py_bool() {
pyo3::prepare_freethreaded_python();
Python::with_gil(|py| {
pyo3::Python::initialize();
Python::attach(|py| {
let obj = yaml_value_to_py(py, &Value::Bool(true)).unwrap();
assert!(obj.extract::<bool>(py).unwrap());
});
}

#[test]
fn test_yaml_to_py_number_i64() {
pyo3::prepare_freethreaded_python();
Python::with_gil(|py| {
pyo3::Python::initialize();
Python::attach(|py| {
let obj = yaml_value_to_py(py, &Value::Number(42.into())).unwrap();
assert_eq!(obj.extract::<i64>(py).unwrap(), 42);
});
}

#[test]
fn test_yaml_to_py_string() {
pyo3::prepare_freethreaded_python();
Python::with_gil(|py| {
pyo3::Python::initialize();
Python::attach(|py| {
let obj = yaml_value_to_py(py, &Value::String("hello".into())).unwrap();
assert_eq!(obj.extract::<String>(py).unwrap(), "hello");
});
}

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

#[test]
fn test_yaml_to_py_tagged_strips_tag() {
pyo3::prepare_freethreaded_python();
Python::with_gil(|py| {
pyo3::Python::initialize();
Python::attach(|py| {
let tagged = Value::Tagged(Box::new(serde_yaml::value::TaggedValue {
tag: serde_yaml::value::Tag::new("!custom"),
value: Value::String("inner".into()),
Expand Down
4 changes: 2 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ mod tests {

#[test]
fn test_route_negative_int_error_message() {
pyo3::prepare_freethreaded_python();
Python::with_gil(|py| {
pyo3::Python::initialize();
Python::attach(|py| {
let neg = (-1i64).into_pyobject(py).unwrap().into_any();
let parts = vec![neg];
let list = pyo3::types::PyList::new(py, &parts).unwrap();
Expand Down
Loading