Skip to content

Commit c19c579

Browse files
committed
update migration guide
1 parent b40bb14 commit c19c579

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

guide/src/migration.md

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,26 @@ let tup = PyTuple::new(py, [1, 2, 3]);
4545
<details open>
4646
<summary><small>Click to expand</small></summary>
4747

48-
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.
48+
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.
4949

5050
Before:
5151

5252
```rust,ignore
5353
# use pyo3::prelude::*;
5454
# use pyo3::types::{PyDict, IntoPyDict};
55-
# use pyo3::types::dict::PyDictItem;
56-
impl<T, I> IntoPyDict for I
55+
# use std::collections::HashMap;
56+
57+
struct MyMap<K, V>(HashMap<K, V>);
58+
59+
impl<K, V> IntoPyDict for MyMap<K, V>
5760
where
58-
T: PyDictItem,
59-
I: IntoIterator<Item = T>,
61+
K: ToPyObject,
62+
V: ToPyObject,
6063
{
6164
fn into_py_dict_bound(self, py: Python<'_>) -> Bound<'_, PyDict> {
62-
let dict = PyDict::new(py);
63-
for item in self {
64-
dict.set_item(item.key(), item.value())
65+
let dict = PyDict::new_bound(py);
66+
for (key, value) in self.0 {
67+
dict.set_item(key, value)
6568
.expect("Failed to set_item on dict");
6669
}
6770
dict
@@ -71,22 +74,25 @@ where
7174

7275
After:
7376

74-
```rust,ignore
77+
```rust
7578
# use pyo3::prelude::*;
7679
# use pyo3::types::{PyDict, IntoPyDict};
77-
# use pyo3::types::dict::PyDictItem;
78-
impl<T, I> IntoPyDict for I
80+
# use std::collections::HashMap;
81+
82+
# #[allow(dead_code)]
83+
# struct MyMap<K, V>(HashMap<K, V>);
84+
85+
impl<'py, K, V> IntoPyDict<'py> for MyMap<K, V>
7986
where
80-
T: PyDictItem,
81-
I: IntoIterator<Item = T>,
87+
K: IntoPyObject<'py>,
88+
V: IntoPyObject<'py>,
8289
{
83-
fn into_py_dict(self, py: Python<'_>) -> Bound<'_, PyDict> {
90+
fn into_py_dict(self, py: Python<'py>) -> PyResult<Bound<'_, PyDict>> {
8491
let dict = PyDict::new(py);
85-
for item in self {
86-
dict.set_item(item.key(), item.value())
87-
.expect("Failed to set_item on dict");
92+
for (key, value) in self.0 {
93+
dict.set_item(key, value)?;
8894
}
89-
dict
95+
Ok(dict)
9096
}
9197
}
9298
```

newsfragments/4493.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
`IntoPyDict::into_py_dict` is now fallible due to `IntoPyObject` migration.

src/types/dict.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ where
588588
}
589589

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

0 commit comments

Comments
 (0)