Skip to content

Commit

Permalink
Generate the return type of #[pymethods]
Browse files Browse the repository at this point in the history
  • Loading branch information
CLOVIS-AI committed Jun 15, 2022
1 parent 3418571 commit 4070ad4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
15 changes: 13 additions & 2 deletions pyo3-macros-backend/src/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! The generated structures are read-only.

use proc_macro2::{Ident, Literal, TokenStream, TokenTree};
use quote::{format_ident, quote};
use quote::{format_ident, quote, ToTokens};
use syn::spanned::Spanned;
use syn::Type;
use crate::method::FnType;
Expand Down Expand Up @@ -89,6 +89,7 @@ pub(crate) fn generate_fields_inspection(

let field_info_name = format_ident!("{}_info", ident_prefix);
let field_args_name = format_ident!("{}_args", ident_prefix);
let field_type_fn_name = format_ident!("{}_output_fn", ident_prefix);

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

let output = quote! {
fn #field_type_fn_name() -> _pyo3::inspect::types::TypeInfo {
<#field_type as _pyo3::conversion::IntoPy<_>>::type_output()
}

const #field_args_name: [_pyo3::inspect::fields::ArgumentInfo<'static>; 0] = []; //TODO

const #field_info_name: _pyo3::inspect::fields::FieldInfo<'static> = _pyo3::inspect::fields::FieldInfo {
name: #field_name,
kind: #field_kind,
py_type: ::std::option::Option::None, //TODO
py_type: Some(#field_type_fn_name),
arguments: &#field_args_name,
};
};
Expand Down
1 change: 1 addition & 0 deletions tests/test_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fn simple_info() {
#[test]
fn types() {
assert_eq!("bool", format!("{}", <bool>::type_output()));
assert_eq!("bool", format!("{}", <bool as IntoPy<_>>::type_output()));
assert_eq!("bytes", format!("{}", <&[u8]>::type_output()));
assert_eq!("str", format!("{}", <String>::type_output()));
assert_eq!("str", format!("{}", <char>::type_output()));
Expand Down

0 comments on commit 4070ad4

Please sign in to comment.