Skip to content

Basic introspection of #[derive(FromPyObject)] - #5339

Merged
davidhewitt merged 1 commit into
PyO3:mainfrom
Tpt:tpt/from-py-object
Aug 21, 2025
Merged

davidhewitt merged 1 commit into
PyO3:mainfrom
Tpt:tpt/from-py-object

Conversation

@Tpt

@Tpt Tpt commented Aug 20, 2025

Copy link
Copy Markdown
Contributor

Struct fields are not supported yet. Doing it cleanly might require a mix of typing.Protocol and typing.TypedDict

Comment thread src/impl_/pyclass.rs
const DOC: &'static CStr;

#[cfg(feature = "experimental-inspect")]
const TYPE_NAME: &'static str;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Useful to initialize FromPyObject::INPUT_TYPE in the blanket impl impl<T> FromPyObject<'_> for T

}
ContainerType::Struct(_) => {
// TODO: implement using a Protocol?
builder.push_str("_typeshed.Incomplete")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

_typeshed.Incomplete is an alias of typing.Any that hints that the type should be edited to something smaller: https://typing.python.org/en/latest/guides/writing_stubs.html#incomplete-stubs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A protocol seems like the right approach; we want to describe the shape of what we expect without any knowledge of its actual type, given we fully duck-type the extraction.

Struct fields are not supported yet

@davidhewitt davidhewitt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, very nice to see all these pieces coming together, thanks!

}
ContainerType::Struct(_) => {
// TODO: implement using a Protocol?
builder.push_str("_typeshed.Incomplete")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A protocol seems like the right approach; we want to describe the shape of what we expect without any knowledge of its actual type, given we fully duck-type the extraction.

Comment on lines +496 to +497
// We don't know what from_py_with is doing
builder.push_str("_typeshed.Incomplete")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be interesting to discuss how we make this work, I guess some manually-applied type hint would be the only way.

@davidhewitt
davidhewitt added this pull request to the merge queue Aug 21, 2025
Merged via the queue into PyO3:main with commit 56efcdd Aug 21, 2025
42 checks passed
@Tpt
Tpt deleted the tpt/from-py-object branch August 22, 2025 07:27
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.

2 participants