Skip to content

Commit 003d275

Browse files
committed
Inspect arguments: name, type and mutability
1 parent 53f2e94 commit 003d275

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pyo3-macros-backend/src/inspect.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,35 @@ pub(crate) fn generate_fields_inspection(
150150
.map(|it| it.to_token_stream())
151151
.unwrap_or_else(|| cls.to_token_stream());
152152

153+
let mut args: Vec<TokenStream> = vec![];
154+
for arg in &field.spec.args {
155+
let name = Literal::string(&*arg.name.to_string());
156+
let typ = generate_type(arg.ty)
157+
.map(|it| it.to_token_stream())
158+
.unwrap_or_else(|| cls.to_token_stream());
159+
160+
let is_mutable = arg.mutability.is_some();
161+
162+
args.push(quote! {
163+
_pyo3::inspect::fields::ArgumentInfo {
164+
name: #name,
165+
kind: _pyo3::inspect::fields::ArgumentKind::PositionOrKeyword, //TODO
166+
py_type: ::std::option::Option::Some(|| <#typ as _pyo3::conversion::FromPyObject>::type_input()),
167+
default_value: false,
168+
is_modified: #is_mutable,
169+
}
170+
});
171+
}
172+
let args_size = Literal::usize_suffixed(args.len());
173+
153174
let output = quote! {
154175
fn #field_type_fn_name() -> _pyo3::inspect::types::TypeInfo {
155176
<#field_type as _pyo3::conversion::IntoPy<_>>::type_output()
156177
}
157178

158-
const #field_args_name: [_pyo3::inspect::fields::ArgumentInfo<'static>; 0] = []; //TODO
179+
const #field_args_name: [_pyo3::inspect::fields::ArgumentInfo<'static>; #args_size] = [
180+
#(#args),*
181+
];
159182

160183
const #field_info_name: _pyo3::inspect::fields::FieldInfo<'static> = _pyo3::inspect::fields::FieldInfo {
161184
name: #field_name,

tests/test_interface.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ struct Complicated {
229229
#[allow(unused_variables)]
230230
#[pymethods]
231231
impl Complicated {
232+
#[new]
232233
fn new(foo: PyObject, parent: PyObject) -> Self {
233234
unreachable!("This is just a stub")
234235
}

0 commit comments

Comments
 (0)