Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/5634.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Introspection: properly generate annotations for `Option<T>` in both input and output.
44 changes: 8 additions & 36 deletions pyo3-macros-backend/src/introspection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,34 +339,12 @@ fn argument_introspection_data<'a>(
params.insert("annotation", IntrospectionNode::String(annotation.into()));
} else if desc.from_py_with.is_none() {
// If from_py_with is set we don't know anything on the input type
if let Some(ty) = desc.option_wrapped_type {
// Special case to properly generate a `T | None` annotation
let mut ty = ty.clone();
if let Some(class_type) = class_type {
replace_self(&mut ty, class_type);
}
elide_lifetimes(&mut ty);
params.insert(
"annotation",
IntrospectionNode::InputType {
rust_type: ty,
nullable: true,
},
);
} else {
let mut ty = desc.ty.clone();
if let Some(class_type) = class_type {
replace_self(&mut ty, class_type);
}
elide_lifetimes(&mut ty);
params.insert(
"annotation",
IntrospectionNode::InputType {
rust_type: ty,
nullable: false,
},
);
let mut ty = desc.ty.clone();
if let Some(class_type) = class_type {
replace_self(&mut ty, class_type);
}
elide_lifetimes(&mut ty);
params.insert("annotation", IntrospectionNode::InputType(ty));
}
IntrospectionNode::Map(params).into()
}
Expand All @@ -375,7 +353,7 @@ enum IntrospectionNode<'a> {
String(Cow<'a, str>),
Bool(bool),
IntrospectionId(Option<Cow<'a, Type>>),
InputType { rust_type: Type, nullable: bool },
InputType(Type),
OutputType { rust_type: Type, is_final: bool },
ConstantType(PythonIdentifier),
Map(HashMap<&'static str, IntrospectionNode<'a>>),
Expand Down Expand Up @@ -411,11 +389,8 @@ impl IntrospectionNode<'_> {
});
content.push_str("\"");
}
Self::InputType {
rust_type,
nullable,
} => {
let mut annotation = quote! {
Self::InputType(rust_type) => {
let annotation = quote! {
<#rust_type as #pyo3_crate_path::impl_::extract_argument::PyFunctionArgument<
{
#[allow(unused_imports, reason = "`Probe` trait used on negative case only")]
Expand All @@ -424,9 +399,6 @@ impl IntrospectionNode<'_> {
}
>>::INPUT_TYPE
};
if nullable {
annotation = quote! { #pyo3_crate_path::inspect::TypeHint::union(&[#annotation, #pyo3_crate_path::inspect::TypeHint::builtin("None")]) };
}
content.push_tokens(serialize_type_hint(annotation, pyo3_crate_path));
}
Self::OutputType {
Expand Down
8 changes: 8 additions & 0 deletions src/conversions/std/option.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "experimental-inspect")]
use crate::inspect::TypeHint;
use crate::{
conversion::IntoPyObject, types::any::PyAnyMethods, BoundObject, FromPyObject, PyAny, Python,
};
Expand All @@ -10,6 +12,8 @@ where
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = T::Error;
#[cfg(feature = "experimental-inspect")]
const OUTPUT_TYPE: TypeHint = TypeHint::union(&[T::OUTPUT_TYPE, TypeHint::builtin("None")]);

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
self.map_or_else(
Expand All @@ -30,6 +34,8 @@ where
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = <&'a T as IntoPyObject<'py>>::Error;
#[cfg(feature = "experimental-inspect")]
const OUTPUT_TYPE: TypeHint = <Option<&T>>::OUTPUT_TYPE;

#[inline]
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
Expand All @@ -42,6 +48,8 @@ where
T: FromPyObject<'a, 'py>,
{
type Error = T::Error;
#[cfg(feature = "experimental-inspect")]
const INPUT_TYPE: TypeHint = TypeHint::union(&[T::INPUT_TYPE, TypeHint::builtin("None")]);

fn extract(obj: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
if obj.is_none() {
Expand Down
5 changes: 1 addition & 4 deletions src/impl_/extract_argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ where
type Error = T::Error;

#[cfg(feature = "experimental-inspect")]
const INPUT_TYPE: TypeHint = TypeHint::union(&[
TypeHint::module_attr("typing", "Any"),
TypeHint::builtin("None"),
]);
const INPUT_TYPE: TypeHint = TypeHint::union(&[T::INPUT_TYPE, TypeHint::builtin("None")]);

#[inline]
fn extract(
Expand Down
Loading