Skip to content

Commit 4bef324

Browse files
IcxoluLilyFirefly
andauthored
deprecate PyObject type alias (#5325)
* deprecate `PyObject` type alias * fix typo Co-authored-by: Lily Acorn <code@lilyf.org> --------- Co-authored-by: Lily Acorn <code@lilyf.org>
1 parent dd9e404 commit 4bef324

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+229
-215
lines changed

guide/src/async-await.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use pyo3::prelude::*;
1313
1414
#[pyfunction]
1515
#[pyo3(signature=(seconds, result=None))]
16-
async fn sleep(seconds: f64, result: Option<PyObject>) -> Option<PyObject> {
16+
async fn sleep(seconds: f64, result: Option<Py<PyAny>>) -> Option<Py<PyAny>> {
1717
let (tx, rx) = oneshot::channel();
1818
thread::spawn(move || {
1919
thread::sleep(Duration::from_secs_f64(seconds));

guide/src/class.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl SubSubClass {
413413
}
414414

415415
#[staticmethod]
416-
fn factory_method(py: Python<'_>, val: usize) -> PyResult<PyObject> {
416+
fn factory_method(py: Python<'_>, val: usize) -> PyResult<Py<PyAny>> {
417417
let base = PyClassInitializer::from(BaseClass::new());
418418
let sub = base.add_subclass(SubClass { val2: val });
419419
if val % 2 == 0 {
@@ -748,7 +748,7 @@ To create a constructor which takes a positional class argument, you can combine
748748
# use pyo3::prelude::*;
749749
# use pyo3::types::PyType;
750750
# #[pyclass]
751-
# struct BaseClass(PyObject);
751+
# struct BaseClass(Py<PyAny>);
752752
#
753753
#[pymethods]
754754
impl BaseClass {

guide/src/class/protocols.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ given signatures should be interpreted as follows:
5959
#[pymethods]
6060
impl NotHashable {
6161
#[classattr]
62-
const __hash__: Option<PyObject> = None;
62+
const __hash__: Option<Py<PyAny>> = None;
6363
}
6464
```
6565
</details>
@@ -162,15 +162,15 @@ use std::sync::Mutex;
162162
163163
#[pyclass]
164164
struct MyIterator {
165-
iter: Mutex<Box<dyn Iterator<Item = PyObject> + Send>>,
165+
iter: Mutex<Box<dyn Iterator<Item = Py<PyAny>> + Send>>,
166166
}
167167
168168
#[pymethods]
169169
impl MyIterator {
170170
fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
171171
slf
172172
}
173-
fn __next__(slf: PyRefMut<'_, Self>) -> Option<PyObject> {
173+
fn __next__(slf: PyRefMut<'_, Self>) -> Option<Py<PyAny>> {
174174
slf.iter.lock().unwrap().next()
175175
}
176176
}
@@ -283,7 +283,7 @@ Use the `#[pyclass(sequence)]` annotation to instruct PyO3 to fill the `sq_lengt
283283
#[pymethods]
284284
impl NoContains {
285285
#[classattr]
286-
const __contains__: Option<PyObject> = None;
286+
const __contains__: Option<Py<PyAny>> = None;
287287
}
288288
```
289289
</details>
@@ -439,7 +439,7 @@ use pyo3::gc::PyVisit;
439439
440440
#[pyclass]
441441
struct ClassWithGCSupport {
442-
obj: Option<PyObject>,
442+
obj: Option<Py<PyAny>>,
443443
}
444444
445445
#[pymethods]

guide/src/conversions/tables.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Mapping of Rust types to Python types
22

3-
When writing functions callable from Python (such as a `#[pyfunction]` or in a `#[pymethods]` block), the trait `FromPyObject` is required for function arguments, and `IntoPy<PyObject>` is required for function return values.
3+
When writing functions callable from Python (such as a `#[pyfunction]` or in a `#[pymethods]` block), the trait `FromPyObject` is required for function arguments, and `IntoPyObject` is required for function return values.
44

55
Consult the tables in the following section to find the Rust types provided by PyO3 which implement these traits.
66

@@ -54,7 +54,6 @@ It is also worth remembering the following special types:
5454
| `Python<'py>` | A GIL token, used to pass to PyO3 constructors to prove ownership of the GIL. |
5555
| `Bound<'py, T>` | A Python object connected to the GIL lifetime. This provides access to most of PyO3's APIs. |
5656
| `Py<T>` | A Python object isolated from the GIL lifetime. This can be sent to other threads. |
57-
| `PyObject` | An alias for `Py<PyAny>` |
5857
| `PyRef<T>` | A `#[pyclass]` borrowed immutably. |
5958
| `PyRefMut<T>` | A `#[pyclass]` borrowed mutably. |
6059

guide/src/conversions/traits.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ forward the implementation to the inner type.
580580
581581
// newtype tuple structs are implicitly `transparent`
582582
#[derive(IntoPyObject)]
583-
struct TransparentTuple(PyObject);
583+
struct TransparentTuple(Py<PyAny>);
584584
585585
#[derive(IntoPyObject)]
586586
#[pyo3(transparent)]
@@ -599,7 +599,7 @@ For `enum`s each variant is converted according to the rules for `struct`s above
599599
600600
#[derive(IntoPyObject)]
601601
enum Enum<'a, 'py, K: Hash + Eq, V> { // enums are supported and convert using the same
602-
TransparentTuple(PyObject), // rules on the variants as the structs above
602+
TransparentTuple(Py<PyAny>), // rules on the variants as the structs above
603603
#[pyo3(transparent)]
604604
TransparentStruct { inner: Bound<'py, PyAny> },
605605
Tuple(&'a str, HashMap<K, V>),
@@ -645,7 +645,7 @@ demonstrated below.
645645
```rust,no_run
646646
# use pyo3::prelude::*;
647647
# #[allow(dead_code)]
648-
struct MyPyObjectWrapper(PyObject);
648+
struct MyPyObjectWrapper(Py<PyAny>);
649649
650650
impl<'py> IntoPyObject<'py> for MyPyObjectWrapper {
651651
type Target = PyAny; // the Python type
@@ -741,7 +741,6 @@ In the example above we used `BoundObject::into_any` and `BoundObject::unbind` t
741741
[`FromPyObject`]: {{#PYO3_DOCS_URL}}/pyo3/conversion/trait.FromPyObject.html
742742
[`IntoPyObject`]: {{#PYO3_DOCS_URL}}/pyo3/conversion/trait.IntoPyObject.html
743743
[`IntoPyObjectExt`]: {{#PYO3_DOCS_URL}}/pyo3/conversion/trait.IntoPyObjectExt.html
744-
[`PyObject`]: {{#PYO3_DOCS_URL}}/pyo3/type.PyObject.html
745744

746745
[`PyRef`]: {{#PYO3_DOCS_URL}}/pyo3/pycell/struct.PyRef.html
747746
[`PyRefMut`]: {{#PYO3_DOCS_URL}}/pyo3/pycell/struct.PyRefMut.html

guide/src/function/signature.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Arguments of type `Python` must not be part of the signature:
8282
# use pyo3::prelude::*;
8383
#[pyfunction]
8484
#[pyo3(signature = (lambda))]
85-
pub fn simple_python_bound_function(py: Python<'_>, lambda: PyObject) -> PyResult<()> {
85+
pub fn simple_python_bound_function(py: Python<'_>, lambda: Py<PyAny>) -> PyResult<()> {
8686
Ok(())
8787
}
8888
```

guide/src/migration.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ For this reason we chose to rename these to more modern terminology introduced i
1616
- `pyo3::prepare_freethreaded_python` is now called `Python::initialize`.
1717
</details>
1818

19+
### Deprecation of `PyObject` type alias
20+
<details open>
21+
<summary><small>Click to expand</small></summary>
22+
23+
The type alias `PyObject` (aka `Py<PyAny>`) is often confused with the identically named FFI definition `pyo3::ffi::PyObject`. For this reason we are deprecating its usage. To migrate simply replace its usage by the target type `Py<PyAny>`.
24+
25+
</details>
26+
1927
### Deprecation of `GILProtected`
2028
<details open>
2129
<summary><small>Click to expand</small></summary>
@@ -186,6 +194,7 @@ impl ToPyObject for MyPyObjectWrapper {
186194

187195
After:
188196
```rust,no_run
197+
# #![allow(deprecated)]
189198
# use pyo3::prelude::*;
190199
# #[allow(dead_code)]
191200
# struct MyPyObjectWrapper(PyObject);

guide/src/python-from-rust/function-calls.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ Both of these APIs take `args` and `kwargs` arguments (for positional and keywor
1212
* [`call1`]({{#PYO3_DOCS_URL}}/pyo3/types/trait.PyAnyMethods.html#tymethod.call1) and [`call_method1`]({{#PYO3_DOCS_URL}}/pyo3/types/trait.PyAnyMethods.html#tymethod.call_method1) to call only with positional `args`.
1313
* [`call0`]({{#PYO3_DOCS_URL}}/pyo3/types/trait.PyAnyMethods.html#tymethod.call0) and [`call_method0`]({{#PYO3_DOCS_URL}}/pyo3/types/trait.PyAnyMethods.html#tymethod.call_method0) to call with no arguments.
1414

15-
For convenience the [`Py<T>`](../types.md#pyt-and-pyobject) smart pointer also exposes these same six API methods, but needs a `Python` token as an additional first argument to prove the GIL is held.
15+
For convenience the [`Py<T>`](../types.md#pyt) smart pointer also exposes these same six API methods, but needs a `Python` token as an additional first argument to prove the GIL is held.
1616

17-
The example below calls a Python function behind a `PyObject` (aka `Py<PyAny>`) reference:
17+
The example below calls a Python function behind a `Py<PyAny>` reference:
1818

1919
```rust
2020
use pyo3::prelude::*;

guide/src/python-typing-hints.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl MyClass {
230230
pub fn __class_getitem__(
231231
cls: &Bound<'_, PyType>,
232232
key: &Bound<'_, PyAny>,
233-
) -> PyResult<PyObject> {
233+
) -> PyResult<Py<PyAny>> {
234234
/* implementation details */
235235
}
236236
}

guide/src/trait-bounds.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ How could we expose this solver to Python thanks to PyO3 ?
4444
## Implementation of the trait bounds for the Python class
4545

4646
If a Python class implements the same three methods as the `Model` trait, it seems logical it could be adapted to use the solver.
47-
However, it is not possible to pass a `PyObject` to it as it does not implement the Rust trait (even if the Python model has the required methods).
47+
However, it is not possible to pass a `Py<PyAny>` to it as it does not implement the Rust trait (even if the Python model has the required methods).
4848

4949
In order to implement the trait, we must write a wrapper around the calls in Rust to the Python model.
5050
The method signatures must be the same as the trait, keeping in mind that the Rust trait cannot be changed for the purpose of making the code available in Python.

0 commit comments

Comments
 (0)