Skip to content

Commit 57cf61c

Browse files
diliopfacebook-github-bot
authored andcommitted
fix "most" deprecation warnings (#275)
Summary: X-link: facebookincubator/antlir#284 Pull Request resolved: #275 Fix most deprecation warnings introduced by `pyo3 = 0.24.2` Reviewed By: dtolnay Differential Revision: D76439928 fbshipit-source-id: 43b48d39bb95f880172f582dac92cc2ec0275984
1 parent ef1e28b commit 57cf61c

File tree

29 files changed

+161
-172
lines changed

29 files changed

+161
-172
lines changed

monarch_extension/src/client.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl WorkerResponse {
9898
fn result(&self, py: Python<'_>) -> PyResult<PyObject> {
9999
if let Some(result) = &self.result {
100100
if result.is_err() {
101-
Ok(PyNone::get_bound(py).into_py(py))
101+
Ok(PyNone::get(py).into_py(py))
102102
} else {
103103
// TODO: Use better shared error class
104104
let rvalue = result
@@ -113,15 +113,15 @@ impl WorkerResponse {
113113
Ok(unsafe { rvalue.try_to_object_unsafe(py)?.unbind() })
114114
}
115115
} else {
116-
Ok(PyNone::get_bound(py).into_py(py))
116+
Ok(PyNone::get(py).into_py(py))
117117
}
118118
}
119119

120120
fn exception(&self, py: Python<'_>) -> PyResult<PyObject> {
121121
match self.result.as_ref() {
122-
Some(Ok(_)) => Ok(PyNone::get_bound(py).into_py(py)),
122+
Some(Ok(_)) => Ok(PyNone::get(py).into_py(py)),
123123
Some(Err(exc)) => Ok(PyException::exception_to_py(py, exc)?),
124-
None => Ok(PyNone::get_bound(py).into_py(py)),
124+
None => Ok(PyNone::get(py).into_py(py)),
125125
}
126126
}
127127

@@ -145,13 +145,19 @@ pub struct PyWorldState {
145145
#[pymethods]
146146
impl PyWorldState {
147147
#[getter]
148-
fn labels(self_: PyRef<Self>, py: Python) -> PyObject {
149-
self_.inner.labels.clone().into_py_dict_bound(py).into()
148+
fn labels(self_: PyRef<Self>, py: Python) -> PyResult<PyObject> {
149+
Ok(self_
150+
.inner
151+
.labels
152+
.clone()
153+
.into_py_dict(py)?
154+
.into_any()
155+
.unbind())
150156
}
151157

152158
#[getter]
153159
fn procs(self_: PyRef<Self>, py: Python) -> PyResult<PyObject> {
154-
let proc_dict = PyDict::new_bound(py);
160+
let proc_dict = PyDict::new(py);
155161
for (proc_id, proc_info) in self_.inner.procs.clone() {
156162
proc_dict.set_item(proc_id.to_string(), PyProcInfo::from(proc_info).into_py(py))?;
157163
}
@@ -203,23 +209,25 @@ impl PySystemSnapshotFilter {
203209
}
204210

205211
#[getter]
206-
fn world_labels(self_: PyRef<Self>, py: Python) -> PyObject {
207-
self_
212+
fn world_labels(self_: PyRef<Self>, py: Python) -> PyResult<PyObject> {
213+
Ok(self_
208214
.inner
209215
.world_labels
210216
.clone()
211-
.into_py_dict_bound(py)
212-
.into()
217+
.into_py_dict(py)?
218+
.into_any()
219+
.unbind())
213220
}
214221

215222
#[getter]
216-
fn proc_labels(self_: PyRef<Self>, py: Python) -> PyObject {
217-
self_
223+
fn proc_labels(self_: PyRef<Self>, py: Python) -> PyResult<PyObject> {
224+
Ok(self_
218225
.inner
219226
.proc_labels
220227
.clone()
221-
.into_py_dict_bound(py)
222-
.into()
228+
.into_py_dict(py)?
229+
.into_any()
230+
.unbind())
223231
}
224232
}
225233

@@ -668,7 +676,7 @@ impl ClientActor {
668676
action,
669677
}
670678
.into_py(py)),
671-
Ok(None) => Ok(PyNone::get_bound(py).into_py(py)),
679+
Ok(None) => Ok(PyNone::get(py).into_py(py)),
672680
Err(err) => {
673681
if let Some(ControllerError::Failed(controller_id, err_msg)) =
674682
err.downcast_ref::<ControllerError>()
@@ -726,7 +734,7 @@ impl ClientActor {
726734
.into_py(py),
727735
})
728736
.collect::<Vec<PyObject>>();
729-
Ok(PyList::new_bound(py, messages))
737+
PyList::new(py, messages)
730738
}
731739

732740
/// Get the status of all the worlds from the system.
@@ -748,7 +756,7 @@ impl ClientActor {
748756
.await
749757
})??;
750758
Python::with_gil(|py| {
751-
let py_dict = PyDict::new_bound(py);
759+
let py_dict = PyDict::new(py);
752760
for (world, status) in worlds {
753761
py_dict.set_item(world.to_string(), status.to_string())?;
754762
}
@@ -782,7 +790,7 @@ impl ClientActor {
782790

783791
// Convert the snapshot to a Python dictionary
784792
let result: PyResult<PyObject> = Python::with_gil(|py| {
785-
let worlds_dict = PyDict::new_bound(py);
793+
let worlds_dict = PyDict::new(py);
786794
for (world, status) in snapshot.worlds {
787795
worlds_dict.set_item(
788796
world.to_string(),

monarch_extension/src/convert.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct MessageParser<'a> {
4646
fn create_function(obj: Bound<'_, PyAny>) -> PyResult<ResolvableFunction> {
4747
let cloudpickle = obj
4848
.py()
49-
.import_bound("monarch.common.function")?
49+
.import("monarch.common.function")?
5050
.getattr("ResolvableFromCloudpickle")?;
5151
if obj.is_instance(&cloudpickle)? {
5252
Ok(ResolvableFunction::Cloudpickle(Cloudpickle::new(
@@ -97,17 +97,17 @@ impl<'a> MessageParser<'a> {
9797
let tree_flatten = self
9898
.current
9999
.py()
100-
.import_bound("torch.utils._pytree")?
100+
.import("torch.utils._pytree")?
101101
.getattr("tree_flatten")?;
102102
let output_tuple: (Bound<'a, PyAny>, Bound<'a, PyAny>) =
103103
tree_flatten.call1((self.attr(name)?,))?.extract()?;
104104
let referenceable = self
105105
.current
106106
.py()
107-
.import_bound("monarch.common.reference")?
107+
.import("monarch.common.reference")?
108108
.getattr("Referenceable")?;
109109
let mut flat: Vec<Option<Ref>> = vec![];
110-
for x in output_tuple.0.iter()? {
110+
for x in output_tuple.0.try_iter()? {
111111
let v: Bound<'a, PyAny> = x?;
112112
if v.is_instance(&referenceable)? {
113113
flat.push(Some(create_ref(v)?));
@@ -119,12 +119,10 @@ impl<'a> MessageParser<'a> {
119119
Ok(flat)
120120
}
121121
#[allow(non_snake_case)]
122-
123122
fn parseRef(&self, name: &str) -> PyResult<Ref> {
124123
create_ref(self.attr(name)?)
125124
}
126125
#[allow(non_snake_case)]
127-
128126
fn parseOptionalRef(&self, name: &str) -> PyResult<Option<Ref>> {
129127
let obj = self.attr(name)?;
130128
if obj.is_none() {
@@ -141,7 +139,7 @@ impl<'a> MessageParser<'a> {
141139
#[allow(non_snake_case)]
142140
fn parseRefList(&self, name: &str) -> PyResult<Vec<Ref>> {
143141
self.attr(name)?
144-
.iter()?
142+
.try_iter()?
145143
.map(|x| {
146144
let v = x?;
147145
let vr: PyResult<u64> = v.extract();
@@ -159,7 +157,6 @@ impl<'a> MessageParser<'a> {
159157
create_function(self.attr(name)?)
160158
}
161159
#[allow(non_snake_case)]
162-
163160
fn parseOptionalFunction(&self, name: &str) -> PyResult<Option<ResolvableFunction>> {
164161
let f = self.attr(name)?;
165162
if f.is_none() {
@@ -179,7 +176,7 @@ impl<'a> MessageParser<'a> {
179176
}
180177
#[allow(non_snake_case)]
181178
fn parseWorkerMessageList(&self, name: &str) -> PyResult<Vec<WorkerMessage>> {
182-
self.attr(name)?.iter()?.map(|x| convert(x?)).collect()
179+
self.attr(name)?.try_iter()?.map(|x| convert(x?)).collect()
183180
}
184181
fn parse_error_reason(&self, name: &str) -> PyResult<Option<(Option<ActorId>, String)>> {
185182
let err = self.attr(name)?;
@@ -203,7 +200,7 @@ static CONVERT_MAP: OnceLock<HashMap<u64, FnType>> = OnceLock::new();
203200

204201
fn create_map(py: Python) -> HashMap<u64, FnType> {
205202
let messages = py
206-
.import_bound("monarch.common.messages")
203+
.import("monarch.common.messages")
207204
.expect("import monarch.common.messages");
208205
let mut m: HashMap<u64, FnType> = HashMap::new();
209206
let key = |name: &str| {

monarch_extension/src/debugger.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub fn get_bytes_from_write_action(
6262
action: DebuggerAction,
6363
) -> PyResult<Bound<'_, PyBytes>> {
6464
if let DebuggerAction::Write { bytes } = action {
65-
Ok(PyBytes::new_bound(py, &bytes))
65+
Ok(PyBytes::new(py, &bytes))
6666
} else {
6767
Err(PyRuntimeError::new_err(format!(
6868
"Cannot extract bytes from non-write debugger action {:?}",
@@ -115,7 +115,7 @@ impl PdbActor {
115115
)?;
116116
match result {
117117
Ok(Some(DebuggerMessage::Action { action })) => Ok(action.into_py(py)),
118-
Ok(None) => Ok(PyNone::get_bound(py).into_py(py)),
118+
Ok(None) => Ok(PyNone::get(py).into_py(py)),
119119
Err(err) => Err(PyRuntimeError::new_err(err.to_string())),
120120
}
121121
}

monarch_extension/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ fn get_or_add_new_module<'py>(
4242
if let Some(submodule) = submodule {
4343
current_module = submodule.extract()?;
4444
} else {
45-
let new_module = PyModule::new_bound(current_module.py(), part)?;
45+
let new_module = PyModule::new(current_module.py(), part)?;
4646
current_module.add_submodule(&new_module)?;
4747
current_module
4848
.py()
49-
.import_bound("sys")?
49+
.import("sys")?
5050
.getattr("modules")?
5151
.set_item(
5252
format!("monarch._rust_bindings.{}", parts.join(".")),

monarch_extension/src/mesh_controller.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ impl _Controller {
187187
) -> PyResult<()> {
188188
let failures = self.history.add_invocation(
189189
seq.into(),
190-
uses.iter()?
190+
uses.try_iter()?
191191
.map(|x| Ref::from_py_object(&x?))
192192
.collect::<PyResult<Vec<Ref>>>()?,
193-
defs.iter()?
193+
defs.try_iter()?
194194
.map(|x| Ref::from_py_object(&x?))
195195
.collect::<PyResult<Vec<Ref>>>()?,
196196
);

monarch_extension/src/tensor_worker.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,7 @@ fn wire_values_to_args(py: Python<'_>, args: Vec<WireValue>) -> PyResult<PyObjec
12651265
}
12661266
})
12671267
.collect::<Result<Vec<_>, PyErr>>()?;
1268-
Ok(PyTuple::new_bound(py, py_ags).to_object(py))
1268+
Ok(PyTuple::new(py, py_ags)?.into())
12691269
}
12701270

12711271
fn wire_values_to_kwargs(py: Python<'_>, kwargs: HashMap<String, WireValue>) -> PyResult<PyObject> {
@@ -1378,7 +1378,7 @@ pub(crate) fn worker_message_to_py(py: Python<'_>, message: &WorkerMessage) -> P
13781378
/// during packaging.
13791379
#[pyfunction]
13801380
fn worker_main(py: Python<'_>) -> PyResult<()> {
1381-
let argv: Vec<String> = py.import_bound("sys")?.getattr("argv")?.extract()?;
1381+
let argv: Vec<String> = py.import("sys")?.getattr("argv")?.extract()?;
13821382
Python::allow_threads(py, move || {
13831383
let args = BinaryArgs::parse_from(argv);
13841384

monarch_hyperactor/src/actor.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl PickledMessage {
8686

8787
#[getter]
8888
fn message<'a>(&self, py: Python<'a>) -> Bound<'a, PyBytes> {
89-
PyBytes::new_bound(py, self.message.as_ref())
89+
PyBytes::new(py, self.message.as_ref())
9090
}
9191

9292
fn serialize(&self) -> PyResult<PySerialized> {
@@ -144,7 +144,7 @@ impl PickledMessageClientActor {
144144
.into_iter()
145145
.map(|message| message.into_py(py))
146146
.collect::<Vec<PyObject>>();
147-
Ok(PyList::new_bound(py, messages))
147+
PyList::new(py, messages)
148148
}
149149

150150
fn world_status<'py>(&mut self, py: Python<'py>) -> PyResult<PyObject> {
@@ -154,7 +154,7 @@ impl PickledMessageClientActor {
154154
instance.lock().await.world_status(Default::default()).await
155155
})??;
156156
Python::with_gil(|py| {
157-
let py_dict = PyDict::new_bound(py);
157+
let py_dict = PyDict::new(py);
158158
for (world, status) in worlds {
159159
py_dict.set_item(world.to_string(), status.to_string())?;
160160
}
@@ -234,7 +234,7 @@ impl PythonMessage {
234234

235235
#[getter]
236236
fn message<'a>(&self, py: Python<'a>) -> Bound<'a, PyBytes> {
237-
PyBytes::new_bound(py, self.message.as_ref())
237+
PyBytes::new(py, self.message.as_ref())
238238
}
239239

240240
#[getter]
@@ -351,7 +351,7 @@ fn get_task_locals(py: Python) -> &'static pyo3_async_runtimes::TaskLocals {
351351
let (tx, rx) = std::sync::mpsc::channel();
352352
let _ = std::thread::spawn(move || {
353353
Python::with_gil(|py| {
354-
let asyncio = Python::import_bound(py, "asyncio").unwrap();
354+
let asyncio = Python::import(py, "asyncio").unwrap();
355355
let event_loop = asyncio.call_method0("new_event_loop").unwrap();
356356
asyncio
357357
.call_method1("set_event_loop", (event_loop.clone(),))
@@ -466,7 +466,7 @@ impl Handler<PythonMessage> for PythonActor {
466466
inner: this.mailbox_for_py().clone(),
467467
};
468468
let awaitable = tokio::task::block_in_place(|| {
469-
self.actor.call_method_bound(
469+
self.actor.call_method(
470470
py,
471471
"handle",
472472
(
@@ -519,7 +519,7 @@ impl Handler<Cast<PythonMessage>> for PythonActor {
519519
};
520520

521521
let awaitable = tokio::task::block_in_place(|| {
522-
self.actor.call_method_bound(
522+
self.actor.call_method(
523523
py,
524524
"handle_cast",
525525
(

monarch_hyperactor/src/mailbox.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ impl PyMailbox {
5454
inner: Arc::new(tokio::sync::Mutex::new(receiver)),
5555
},
5656
)?;
57-
Ok(PyTuple::new_bound(
58-
py,
59-
vec![handle.into_any(), receiver.into_any()],
60-
))
57+
PyTuple::new(py, vec![handle.into_any(), receiver.into_any()])
6158
}
6259

6360
fn open_once_port<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyTuple>> {
@@ -74,10 +71,7 @@ impl PyMailbox {
7471
inner: std::sync::Mutex::new(Some(receiver)),
7572
},
7673
)?;
77-
Ok(PyTuple::new_bound(
78-
py,
79-
vec![handle.into_any(), receiver.into_any()],
80-
))
74+
PyTuple::new(py, vec![handle.into_any(), receiver.into_any()])
8175
}
8276

8377
pub(super) fn post<'py>(

monarch_hyperactor/src/ndslice.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl PySlice {
120120
);
121121
i += step;
122122
}
123-
Ok(PyTuple::new_bound(py, result).into_py(py))
123+
Ok(PyTuple::new(py, result)?.into_py(py))
124124
}
125125
}
126126
}
@@ -133,16 +133,16 @@ impl PySlice {
133133
self.inner.len()
134134
}
135135

136-
fn __getnewargs_ex__<'py>(&self, py: Python<'py>) -> Bound<'py, PyTuple> {
137-
let kwargs = PyDict::new_bound(py);
136+
fn __getnewargs_ex__<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyTuple>> {
137+
let kwargs = PyDict::new(py);
138138
kwargs.set_item("offset", self.inner.offset()).unwrap();
139139
kwargs.set_item("sizes", self.inner.sizes()).unwrap();
140140
kwargs.set_item("strides", self.inner.strides()).unwrap();
141141

142-
PyTuple::new_bound(
142+
PyTuple::new(
143143
py,
144144
vec![
145-
PyTuple::empty_bound(py).unbind().into_any(),
145+
PyTuple::empty(py).unbind().into_any(),
146146
kwargs.unbind().into_any(),
147147
],
148148
)
@@ -166,7 +166,7 @@ impl PySlice {
166166
#[staticmethod]
167167
fn from_list(py: Python<'_>, ranks: Vec<usize>) -> PyResult<PyObject> {
168168
if ranks.is_empty() {
169-
return Ok(PyList::empty_bound(py).unbind().into_any());
169+
return Ok(PyList::empty(py).unbind().into_any());
170170
}
171171
let mut ranks = ranks;
172172
ranks.sort();

monarch_hyperactor/src/proc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl PyProc {
125125
.collect::<Vec<_>>();
126126
// TODO: i don't think returning this list is of much use for
127127
// anything?
128-
Ok(PyList::new_bound(py, aborted_actors))
128+
PyList::new(py, aborted_actors)
129129
}
130130

131131
#[pyo3(signature = (actor, name=None))]

0 commit comments

Comments
 (0)