From 003d27572f4d5c036ee759affcced1c2f0a7d62a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Wed, 15 Jun 2022 14:45:50 +0200 Subject: [PATCH] Inspect arguments: name, type and mutability --- pyo3-macros-backend/src/inspect.rs | 25 ++++++++++++++++++++++++- tests/test_interface.rs | 1 + 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/pyo3-macros-backend/src/inspect.rs b/pyo3-macros-backend/src/inspect.rs index 3046aa0e91b..58068741174 100644 --- a/pyo3-macros-backend/src/inspect.rs +++ b/pyo3-macros-backend/src/inspect.rs @@ -150,12 +150,35 @@ pub(crate) fn generate_fields_inspection( .map(|it| it.to_token_stream()) .unwrap_or_else(|| cls.to_token_stream()); + let mut args: Vec = vec![]; + for arg in &field.spec.args { + let name = Literal::string(&*arg.name.to_string()); + let typ = generate_type(arg.ty) + .map(|it| it.to_token_stream()) + .unwrap_or_else(|| cls.to_token_stream()); + + let is_mutable = arg.mutability.is_some(); + + args.push(quote! { + _pyo3::inspect::fields::ArgumentInfo { + name: #name, + kind: _pyo3::inspect::fields::ArgumentKind::PositionOrKeyword, //TODO + py_type: ::std::option::Option::Some(|| <#typ as _pyo3::conversion::FromPyObject>::type_input()), + default_value: false, + is_modified: #is_mutable, + } + }); + } + let args_size = Literal::usize_suffixed(args.len()); + 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_args_name: [_pyo3::inspect::fields::ArgumentInfo<'static>; #args_size] = [ + #(#args),* + ]; const #field_info_name: _pyo3::inspect::fields::FieldInfo<'static> = _pyo3::inspect::fields::FieldInfo { name: #field_name, diff --git a/tests/test_interface.rs b/tests/test_interface.rs index 86694db1cf7..8157baff46e 100644 --- a/tests/test_interface.rs +++ b/tests/test_interface.rs @@ -229,6 +229,7 @@ struct Complicated { #[allow(unused_variables)] #[pymethods] impl Complicated { + #[new] fn new(foo: PyObject, parent: PyObject) -> Self { unreachable!("This is just a stub") }