Skip to content

Commit d2382d8

Browse files
authored
Upgrade pyo3 to 0.22 (#1180)
* Upgrade pyo3 to 0.22 * libcst_native: add optional signature Newer versions of pyo3 warn about missing signatures
1 parent 20837f7 commit d2382d8

8 files changed

Lines changed: 46 additions & 97 deletions

File tree

native/Cargo.lock

Lines changed: 18 additions & 75 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

native/libcst/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ trace = ["peg/trace"]
3636

3737
[dependencies]
3838
paste = "1.0.15"
39-
pyo3 = { version = "0.20", optional = true }
39+
pyo3 = { version = "0.22", optional = true }
4040
thiserror = "1.0.63"
4141
peg = "0.8.4"
4242
chic = "1.2.2"

native/libcst/src/nodes/expression.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,6 +2524,7 @@ impl<'r, 'a> Inflate<'a> for DeflatedNamedExpr<'r, 'a> {
25242524
#[cfg(feature = "py")]
25252525
mod py {
25262526

2527+
use pyo3::types::PyAnyMethods;
25272528
use pyo3::types::PyModule;
25282529

25292530
use super::*;
@@ -2535,7 +2536,7 @@ mod py {
25352536
match self {
25362537
Self::Starred(s) => s.try_into_py(py),
25372538
Self::Simple { value, comma } => {
2538-
let libcst = PyModule::import(py, "libcst")?;
2539+
let libcst = PyModule::import_bound(py, "libcst")?;
25392540
let kwargs = [
25402541
Some(("value", value.try_into_py(py)?)),
25412542
comma
@@ -2547,11 +2548,11 @@ mod py {
25472548
.filter(|x| x.is_some())
25482549
.map(|x| x.as_ref().unwrap())
25492550
.collect::<Vec<_>>()
2550-
.into_py_dict(py);
2551+
.into_py_dict_bound(py);
25512552
Ok(libcst
25522553
.getattr("Element")
25532554
.expect("no Element found in libcst")
2554-
.call((), Some(kwargs))?
2555+
.call((), Some(&kwargs))?
25552556
.into())
25562557
}
25572558
}
@@ -2571,7 +2572,7 @@ mod py {
25712572
whitespace_before_colon,
25722573
..
25732574
} => {
2574-
let libcst = PyModule::import(py, "libcst")?;
2575+
let libcst = PyModule::import_bound(py, "libcst")?;
25752576
let kwargs = [
25762577
Some(("key", key.try_into_py(py)?)),
25772578
Some(("value", value.try_into_py(py)?)),
@@ -2592,11 +2593,11 @@ mod py {
25922593
.filter(|x| x.is_some())
25932594
.map(|x| x.as_ref().unwrap())
25942595
.collect::<Vec<_>>()
2595-
.into_py_dict(py);
2596+
.into_py_dict_bound(py);
25962597
Ok(libcst
25972598
.getattr("DictElement")
25982599
.expect("no Element found in libcst")
2599-
.call((), Some(kwargs))?
2600+
.call((), Some(&kwargs))?
26002601
.into())
26012602
}
26022603
}

native/libcst/src/nodes/parser_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ fn parser_config_asdict<'py>(py: Python<'py>, config: PyRef<'py, ParserConfig>)
125125
("version", config.version.clone_ref(py)),
126126
("future_imports", config.future_imports.clone_ref(py)),
127127
]
128-
.into_py_dict(py)
128+
.into_py_dict_bound(py)
129129
}
130130

131131
pub fn init_module(_py: Python, m: &PyModule) -> PyResult<()> {

native/libcst/src/nodes/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pub mod py {
170170
.map(|x| x.try_into_py(py))
171171
.collect::<PyResult<Vec<_>>>()?
172172
.into_iter();
173-
Ok(PyTuple::new(py, converted).into())
173+
Ok(PyTuple::new_bound(py, converted).into())
174174
}
175175
}
176176

native/libcst/src/parser/errors.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub enum ParserError<'a> {
2828
#[cfg(feature = "py")]
2929
mod py_error {
3030

31-
use pyo3::types::{IntoPyDict, PyModule};
31+
use pyo3::types::{IntoPyDict, PyAnyMethods, PyModule};
3232
use pyo3::{IntoPy, PyErr, PyErrArguments, Python};
3333

3434
use super::ParserError;
@@ -65,13 +65,14 @@ mod py_error {
6565
("raw_line", (line + 1).into_py(py)),
6666
("raw_column", col.into_py(py)),
6767
]
68-
.into_py_dict(py);
69-
let libcst = PyModule::import(py, "libcst").expect("libcst cannot be imported");
70-
PyErr::from_value(
68+
.into_py_dict_bound(py);
69+
let libcst =
70+
PyModule::import_bound(py, "libcst").expect("libcst cannot be imported");
71+
PyErr::from_value_bound(
7172
libcst
7273
.getattr("ParserSyntaxError")
7374
.expect("ParserSyntaxError not found")
74-
.call((), Some(kwargs))
75+
.call((), Some(&kwargs))
7576
.expect("failed to instantiate"),
7677
)
7778
})
@@ -86,7 +87,7 @@ mod py_error {
8687
("raw_line", self.raw_line.into_py(py)),
8788
("raw_column", self.raw_column.into_py(py)),
8889
]
89-
.into_py_dict(py)
90+
.into_py_dict_bound(py)
9091
.into_py(py)
9192
}
9293
}

native/libcst/src/py.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use pyo3::prelude::*;
88

99
#[pymodule]
1010
#[pyo3(name = "native")]
11-
pub fn libcst_native(_py: Python, m: &PyModule) -> PyResult<()> {
11+
pub fn libcst_native(_py: Python, m: &Bound<PyModule>) -> PyResult<()> {
1212
#[pyfn(m)]
13+
#[pyo3(signature = (source, encoding=None))]
1314
fn parse_module(source: String, encoding: Option<&str>) -> PyResult<PyObject> {
1415
let m = crate::parse_module(source.as_str(), encoding)?;
1516
Python::with_gil(|py| m.try_into_py(py))

native/libcst_derive/src/into_py.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ fn impl_into_py_enum(ast: &DeriveInput, e: &DataEnum) -> TokenStream {
3838
let kwargs_toks = fields_to_kwargs(&var.fields, true);
3939
toks.push(quote! {
4040
Self::#varname { #(#fieldnames,)* .. } => {
41-
let libcst = pyo3::types::PyModule::import(py, "libcst")?;
41+
use pyo3::types::PyAnyMethods;
42+
43+
let libcst = pyo3::types::PyModule::import_bound(py, "libcst")?;
4244
let kwargs = #kwargs_toks ;
4345
Ok(libcst
4446
.getattr(stringify!(#varname))
4547
.expect(stringify!(no #varname found in libcst))
46-
.call((), Some(kwargs))?
48+
.call((), Some(&kwargs))?
4749
.into())
4850
}
4951
})
@@ -87,12 +89,13 @@ fn impl_into_py_struct(ast: &DeriveInput, e: &DataStruct) -> TokenStream {
8789
#[automatically_derived]
8890
impl#generics crate::nodes::traits::py::TryIntoPy<pyo3::PyObject> for #ident #generics {
8991
fn try_into_py(self, py: pyo3::Python) -> pyo3::PyResult<pyo3::PyObject> {
90-
let libcst = pyo3::types::PyModule::import(py, "libcst")?;
92+
use pyo3::types::PyAnyMethods;
93+
let libcst = pyo3::types::PyModule::import_bound(py, "libcst")?;
9194
let kwargs = #kwargs_toks ;
9295
Ok(libcst
9396
.getattr(stringify!(#ident))
9497
.expect(stringify!(no #ident found in libcst))
95-
.call((), Some(kwargs))?
98+
.call((), Some(&kwargs))?
9699
.into())
97100
}
98101
}
@@ -162,15 +165,15 @@ fn fields_to_kwargs(fields: &Fields, is_enum: bool) -> quote::__private::TokenSt
162165
#(#optional_rust_varnames.map(|x| x.try_into_py(py)).transpose()?.map(|x| (stringify!(#optional_py_varnames), x)),)*
163166
};
164167
if empty_kwargs {
165-
quote! { pyo3::types::PyDict::new(py) }
168+
quote! { pyo3::types::PyDict::new_bound(py) }
166169
} else {
167170
quote! {
168171
[ #kwargs_pairs #optional_pairs ]
169172
.iter()
170173
.filter(|x| x.is_some())
171174
.map(|x| x.as_ref().unwrap())
172175
.collect::<Vec<_>>()
173-
.into_py_dict(py)
176+
.into_py_dict_bound(py)
174177
}
175178
}
176179
}

0 commit comments

Comments
 (0)