Skip to content

Comments

docs: Improves IntoPyObject documentation in the guides#5835

Open
Cheukting wants to merge 12 commits intoPyO3:mainfrom
Cheukting:doc-5709
Open

docs: Improves IntoPyObject documentation in the guides#5835
Cheukting wants to merge 12 commits intoPyO3:mainfrom
Cheukting:doc-5709

Conversation

@Cheukting
Copy link
Contributor

closes #5709

@Cheukting Cheukting changed the title docs: Improves IntoPyObject documentation in the guides [WIP] docs: Improves IntoPyObject documentation in the guides Feb 22, 2026
Copy link
Member

@Icxolu Icxolu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this! I spotted a few things and left comments below.

# }
```

Similar to `FromPyObject`, the argument passed to `get_item` can also be configured:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For IntoPyObject the item attribute refers to the key used in the set_item/__setitem__ call.

```

This tries to convert a mapping with the key `"key"`.
The `item` can take any valid literal that implements `ToBorrowedObject`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToBorrowedObject looks like something old that does not exist anymore. Today I think it just takes any valid literal.

Comment on lines 697 to 698
You can also use `#[pyo3(from_item_all)]` on a struct to convert every field to be used with `get_item` method.
In this case, you don't need to use `#[pyo3(item)]` on each field.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from_item_all has no functionality for IntoPyObject since it exclusively uses set_item. It is just accepted for compatibility with FromPyObject. (Same is true for for other attributes as well, for example attribute or default)

For `enum`s each variant is converted according to the rules for `struct`s above.

```rust,no_run
```rust
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only makes sense to run these if there is main which does something, otherwise it is better to leave these as no_run for a faster test suite.

Comment on lines 880 to 887
- `pyo3(default)`, `pyo3(default = ...)`
- if the argument is set, uses the given default value.
- in this case, the argument must be a Rust expression returning a value of the desired Rust type.
- if the argument is not set, [`Default::default`](https://doc.rust-lang.org/std/default/trait.Default.html#tymethod.default) is used.
- note that the default value is only used if the field is not set.
If the field is set and the conversion function from Rust to Python fails, an exception is raised and the default value is not used.
- this attribute is only supported on named fields.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one has no effect for IntoPyObject and we should not document it.


#### `#[derive(IntoPyObject)]`/`#[derive(IntoPyObjectRef)]` Field Attributes

- `pyo3(item)`, `pyo3(item("key"))`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pyo3(item) is actually the default and only pyo3(item("key")) has the effect to use a different key

…er field attributes in `IntoPyObject` traits.
@Cheukting
Copy link
Contributor Author

It is updated considering all your feedback and suggestions @Icxolu please feel free to check again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Doc Improvements: IntoPyObject actually supports item / attribute attributes

2 participants