Skip to content

Commit 0bd32f2

Browse files
konntermoshttCopilot
authored
test: Adds test-cases for kwargs / args (#260)
Eventually fixes #259, but this contains only a reproducer for `*args` and `**kwargs`. --------- Co-authored-by: Toshiki Teramura <toshiki.teramura@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 0265289 commit 0bd32f2

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

examples/pure/pure.pyi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,16 @@ def echo_path(path:builtins.str | os.PathLike | pathlib.Path) -> pathlib.Path: .
241241

242242
def fn_override_type(cb:collections.abc.Callable[[str]]) -> collections.abc.Callable[[str]]: ...
243243

244+
def func_with_kwargs(**kwargs) -> builtins.bool:
245+
r"""
246+
Takes a variable number of keyword arguments and does nothing
247+
"""
248+
249+
def func_with_star_arg(*args) -> builtins.str:
250+
r"""
251+
Takes a variable number of arguments and returns their string representation.
252+
"""
253+
244254
@typing.overload
245255
def overload_example_1(x:builtins.int) -> builtins.int: ...
246256

examples/pure/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,24 @@ submit! {
539539
}
540540
}
541541

542+
// These are the tests to test the treatment of `*args` and `**kwargs` in functions
543+
544+
/// Takes a variable number of arguments and returns their string representation.
545+
#[gen_stub_pyfunction]
546+
#[pyfunction]
547+
#[pyo3(signature = (*args))]
548+
fn func_with_star_arg(args: &Bound<PyTuple>) -> String {
549+
args.to_string()
550+
}
551+
552+
/// Takes a variable number of keyword arguments and does nothing
553+
#[gen_stub_pyfunction]
554+
#[pyfunction]
555+
#[pyo3(signature = (**kwargs))]
556+
fn func_with_kwargs(kwargs: Option<&Bound<PyDict>>) -> bool {
557+
kwargs.is_some()
558+
}
559+
542560
/// Initializes the Python module
543561
#[pymodule]
544562
fn pure(m: &Bound<PyModule>) -> PyResult<()> {
@@ -569,6 +587,9 @@ fn pure(m: &Bound<PyModule>) -> PyResult<()> {
569587
m.add_function(wrap_pyfunction!(fn_override_type, m)?)?;
570588
m.add_function(wrap_pyfunction!(overload_example_1, m)?)?;
571589
m.add_function(wrap_pyfunction!(overload_example_2, m)?)?;
590+
// Test-cases for `*args` and `**kwargs`
591+
m.add_function(wrap_pyfunction!(func_with_star_arg, m)?)?;
592+
m.add_function(wrap_pyfunction!(func_with_kwargs, m)?)?;
572593
Ok(())
573594
}
574595

pyo3-stub-gen-derive/src/gen_stub/signature.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,15 @@ impl ToTokens for ArgsWithSignature<'_> {
191191
::pyo3_stub_gen::type_info::ArgInfo {
192192
name: #name,
193193
r#type: <#r#type as ::pyo3_stub_gen::PyStubType>::type_input,
194-
signature: Some(pyo3_stub_gen::type_info::SignatureArg::Ident),
194+
signature: Some(pyo3_stub_gen::type_info::SignatureArg::Args),
195195
}}),
196196
Some(ArgInfo { name, r#type: TypeOrOverride::OverrideType{ type_repr, imports, .. }}) => {
197197
let imports = imports.iter().collect::<Vec<&String>>();
198198
Ok(quote! {
199199
::pyo3_stub_gen::type_info::ArgInfo {
200200
name: #name,
201201
r#type: || ::pyo3_stub_gen::TypeInfo { name: #type_repr.to_string(), import: ::std::collections::HashSet::from([#(#imports.into(),)*]) },
202-
signature: Some(pyo3_stub_gen::type_info::SignatureArg::Ident),
202+
signature: Some(pyo3_stub_gen::type_info::SignatureArg::Args),
203203
}})
204204
},
205205
None => Err(syn::Error::new(ident.span(), format!("can not find argument: {ident}")))
@@ -212,15 +212,15 @@ impl ToTokens for ArgsWithSignature<'_> {
212212
::pyo3_stub_gen::type_info::ArgInfo {
213213
name: #name,
214214
r#type: <#r#type as ::pyo3_stub_gen::PyStubType>::type_input,
215-
signature: Some(pyo3_stub_gen::type_info::SignatureArg::Ident),
215+
signature: Some(pyo3_stub_gen::type_info::SignatureArg::Keywords),
216216
}}),
217217
Some(ArgInfo { name, r#type: TypeOrOverride::OverrideType{ type_repr, imports, .. }}) => {
218218
let imports = imports.iter().collect::<Vec<&String>>();
219219
Ok(quote! {
220220
::pyo3_stub_gen::type_info::ArgInfo {
221221
name: #name,
222222
r#type: || ::pyo3_stub_gen::TypeInfo { name: #type_repr.to_string(), import: ::std::collections::HashSet::from([#(#imports.into(),)*]) },
223-
signature: Some(pyo3_stub_gen::type_info::SignatureArg::Ident),
223+
signature: Some(pyo3_stub_gen::type_info::SignatureArg::Keywords),
224224
}})
225225
},
226226
None => Err(syn::Error::new(ident.span(), format!("can not find argument: {ident}")))

0 commit comments

Comments
 (0)