Skip to content

Commit afab411

Browse files
committed
Introspection: simplify code by leveraging that building type hint for containers now work
1 parent 2d37bb5 commit afab411

File tree

3 files changed

+17
-40
lines changed

3 files changed

+17
-40
lines changed

pyo3-macros-backend/src/introspection.rs

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -332,34 +332,12 @@ fn argument_introspection_data<'a>(
332332
params.insert("annotation", IntrospectionNode::String(annotation.into()));
333333
} else if desc.from_py_with.is_none() {
334334
// If from_py_with is set we don't know anything on the input type
335-
if let Some(ty) = desc.option_wrapped_type {
336-
// Special case to properly generate a `T | None` annotation
337-
let mut ty = ty.clone();
338-
if let Some(class_type) = class_type {
339-
replace_self(&mut ty, class_type);
340-
}
341-
elide_lifetimes(&mut ty);
342-
params.insert(
343-
"annotation",
344-
IntrospectionNode::InputType {
345-
rust_type: ty,
346-
nullable: true,
347-
},
348-
);
349-
} else {
350-
let mut ty = desc.ty.clone();
351-
if let Some(class_type) = class_type {
352-
replace_self(&mut ty, class_type);
353-
}
354-
elide_lifetimes(&mut ty);
355-
params.insert(
356-
"annotation",
357-
IntrospectionNode::InputType {
358-
rust_type: ty,
359-
nullable: false,
360-
},
361-
);
335+
let mut ty = desc.ty.clone();
336+
if let Some(class_type) = class_type {
337+
replace_self(&mut ty, class_type);
362338
}
339+
elide_lifetimes(&mut ty);
340+
params.insert("annotation", IntrospectionNode::InputType(ty));
363341
}
364342
IntrospectionNode::Map(params).into()
365343
}
@@ -368,7 +346,7 @@ enum IntrospectionNode<'a> {
368346
String(Cow<'a, str>),
369347
Bool(bool),
370348
IntrospectionId(Option<Cow<'a, Type>>),
371-
InputType { rust_type: Type, nullable: bool },
349+
InputType(Type),
372350
OutputType { rust_type: Type, is_final: bool },
373351
ConstantType(PythonIdentifier),
374352
Map(HashMap<&'static str, IntrospectionNode<'a>>),
@@ -404,11 +382,8 @@ impl IntrospectionNode<'_> {
404382
});
405383
content.push_str("\"");
406384
}
407-
Self::InputType {
408-
rust_type,
409-
nullable,
410-
} => {
411-
let mut annotation = quote! {
385+
Self::InputType(rust_type) => {
386+
let annotation = quote! {
412387
<#rust_type as #pyo3_crate_path::impl_::extract_argument::PyFunctionArgument<
413388
{
414389
#[allow(unused_imports, reason = "`Probe` trait used on negative case only")]
@@ -417,9 +392,6 @@ impl IntrospectionNode<'_> {
417392
}
418393
>>::INPUT_TYPE
419394
};
420-
if nullable {
421-
annotation = quote! { #pyo3_crate_path::inspect::TypeHint::union(&[#annotation, #pyo3_crate_path::inspect::TypeHint::builtin("None")]) };
422-
}
423395
content.push_tokens(serialize_type_hint(annotation, pyo3_crate_path));
424396
}
425397
Self::OutputType {

src/conversions/std/option.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(feature = "experimental-inspect")]
2+
use crate::inspect::TypeHint;
13
use crate::{
24
conversion::IntoPyObject, types::any::PyAnyMethods, BoundObject, FromPyObject, PyAny, Python,
35
};
@@ -10,6 +12,8 @@ where
1012
type Target = PyAny;
1113
type Output = Bound<'py, Self::Target>;
1214
type Error = T::Error;
15+
#[cfg(feature = "experimental-inspect")]
16+
const OUTPUT_TYPE: TypeHint = TypeHint::union(&[T::OUTPUT_TYPE, TypeHint::builtin("None")]);
1317

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

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

4654
fn extract(obj: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
4755
if obj.is_none() {

src/impl_/extract_argument.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,7 @@ where
108108
type Error = T::Error;
109109

110110
#[cfg(feature = "experimental-inspect")]
111-
const INPUT_TYPE: TypeHint = TypeHint::union(&[
112-
TypeHint::module_attr("typing", "Any"),
113-
TypeHint::builtin("None"),
114-
]);
111+
const INPUT_TYPE: TypeHint = TypeHint::union(&[T::INPUT_TYPE, TypeHint::builtin("None")]);
115112

116113
#[inline]
117114
fn extract(

0 commit comments

Comments
 (0)