Skip to content

Commit

Permalink
update migration guide
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Oct 1, 2024
1 parent b40bb14 commit c19c579
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
42 changes: 24 additions & 18 deletions guide/src/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,26 @@ let tup = PyTuple::new(py, [1, 2, 3]);
<details open>
<summary><small>Click to expand</small></summary>

The `IntoPyDict::into_py_dict_bound` method has been renamed to `IntoPyDict::into_py_dict`. If you implemented `IntoPyDict` for your type, you should implement `into_py_dict` instead of `into_py_dict_bound`. The old name is still available but deprecated.
The `IntoPyDict::into_py_dict_bound` method has been renamed to `IntoPyDict::into_py_dict` and is now fallible. If you implemented `IntoPyDict` for your type, you should implement `into_py_dict` instead of `into_py_dict_bound`. The old name is still available but deprecated.

Before:

```rust,ignore
# use pyo3::prelude::*;
# use pyo3::types::{PyDict, IntoPyDict};
# use pyo3::types::dict::PyDictItem;
impl<T, I> IntoPyDict for I
# use std::collections::HashMap;
struct MyMap<K, V>(HashMap<K, V>);
impl<K, V> IntoPyDict for MyMap<K, V>
where
T: PyDictItem,
I: IntoIterator<Item = T>,
K: ToPyObject,
V: ToPyObject,
{
fn into_py_dict_bound(self, py: Python<'_>) -> Bound<'_, PyDict> {
let dict = PyDict::new(py);
for item in self {
dict.set_item(item.key(), item.value())
let dict = PyDict::new_bound(py);
for (key, value) in self.0 {
dict.set_item(key, value)
.expect("Failed to set_item on dict");
}
dict
Expand All @@ -71,22 +74,25 @@ where

After:

```rust,ignore
```rust
# use pyo3::prelude::*;
# use pyo3::types::{PyDict, IntoPyDict};
# use pyo3::types::dict::PyDictItem;
impl<T, I> IntoPyDict for I
# use std::collections::HashMap;

# #[allow(dead_code)]
# struct MyMap<K, V>(HashMap<K, V>);

impl<'py, K, V> IntoPyDict<'py> for MyMap<K, V>
where
T: PyDictItem,
I: IntoIterator<Item = T>,
K: IntoPyObject<'py>,
V: IntoPyObject<'py>,
{
fn into_py_dict(self, py: Python<'_>) -> Bound<'_, PyDict> {
fn into_py_dict(self, py: Python<'py>) -> PyResult<Bound<'_, PyDict>> {
let dict = PyDict::new(py);
for item in self {
dict.set_item(item.key(), item.value())
.expect("Failed to set_item on dict");
for (key, value) in self.0 {
dict.set_item(key, value)?;
}
dict
Ok(dict)
}
}
```
Expand Down
1 change: 1 addition & 0 deletions newsfragments/4493.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`IntoPyDict::into_py_dict` is now fallible due to `IntoPyObject` migration.
2 changes: 1 addition & 1 deletion src/types/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ where
}

/// Represents a tuple which can be used as a PyDict item.
pub trait PyDictItem<'py> {
trait PyDictItem<'py> {
type K: IntoPyObject<'py>;
type V: IntoPyObject<'py>;
fn unpack(self) -> (Self::K, Self::V);
Expand Down

0 comments on commit c19c579

Please sign in to comment.