Skip to content

Commit 4070ad4

Browse files
committed
Generate the return type of #[pymethods]
1 parent 3418571 commit 4070ad4

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

pyo3-macros-backend/src/inspect.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! The generated structures are read-only.
55
66
use proc_macro2::{Ident, Literal, TokenStream, TokenTree};
7-
use quote::{format_ident, quote};
7+
use quote::{format_ident, quote, ToTokens};
88
use syn::spanned::Spanned;
99
use syn::Type;
1010
use crate::method::FnType;
@@ -89,6 +89,7 @@ pub(crate) fn generate_fields_inspection(
8989

9090
let field_info_name = format_ident!("{}_info", ident_prefix);
9191
let field_args_name = format_ident!("{}_args", ident_prefix);
92+
let field_type_fn_name = format_ident!("{}_output_fn", ident_prefix);
9293

9394
let field_name = TokenTree::Literal(Literal::string(&*field.method_name));
9495
let field_kind = match &field.spec.tp {
@@ -101,14 +102,24 @@ pub(crate) fn generate_fields_inspection(
101102
FnType::FnModule => todo!("FnModule is not currently supported"),
102103
FnType::ClassAttribute => quote!(_pyo3::inspect::fields::FieldKind::ClassAttribute),
103104
};
105+
let field_type = match &field.spec.output {
106+
Type::Path(path) if path.path.get_ident().filter(|i| i.to_string() == "Self").is_some() => {
107+
cls.to_token_stream()
108+
}
109+
other => other.to_token_stream(),
110+
};
104111

105112
let output = quote! {
113+
fn #field_type_fn_name() -> _pyo3::inspect::types::TypeInfo {
114+
<#field_type as _pyo3::conversion::IntoPy<_>>::type_output()
115+
}
116+
106117
const #field_args_name: [_pyo3::inspect::fields::ArgumentInfo<'static>; 0] = []; //TODO
107118

108119
const #field_info_name: _pyo3::inspect::fields::FieldInfo<'static> = _pyo3::inspect::fields::FieldInfo {
109120
name: #field_name,
110121
kind: #field_kind,
111-
py_type: ::std::option::Option::None, //TODO
122+
py_type: Some(#field_type_fn_name),
112123
arguments: &#field_args_name,
113124
};
114125
};

tests/test_interface.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ fn simple_info() {
3939
#[test]
4040
fn types() {
4141
assert_eq!("bool", format!("{}", <bool>::type_output()));
42+
assert_eq!("bool", format!("{}", <bool as IntoPy<_>>::type_output()));
4243
assert_eq!("bytes", format!("{}", <&[u8]>::type_output()));
4344
assert_eq!("str", format!("{}", <String>::type_output()));
4445
assert_eq!("str", format!("{}", <char>::type_output()));

0 commit comments

Comments
 (0)