Skip to content

Commit

Permalink
Improve default value for None in text_signature
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Mar 26, 2023
1 parent ebedcfb commit a4a58f5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
1 change: 1 addition & 0 deletions newsfragments/3066.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve default value for `None` in `text_signature`.
33 changes: 22 additions & 11 deletions pyo3-macros-backend/src/pyfunction/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,19 +588,30 @@ impl<'a> FunctionSignature<'a> {
fn default_value_for_parameter(&self, parameter: &str) -> String {
let mut default = "...".to_string();
if let Some(fn_arg) = self.arguments.iter().find(|arg| arg.name == parameter) {
if let Some(syn::Expr::Lit(syn::ExprLit { lit, .. })) = fn_arg.default.as_ref() {
match lit {
syn::Lit::Str(s) => default = s.token().to_string(),
syn::Lit::Char(c) => default = c.token().to_string(),
syn::Lit::Int(i) => default = i.base10_digits().to_string(),
syn::Lit::Float(f) => default = f.base10_digits().to_string(),
syn::Lit::Bool(b) => {
default = if b.value() {
"True".to_string()
} else {
"False".to_string()
if let Some(arg_default) = fn_arg.default.as_ref() {
match arg_default {
// literal values
syn::Expr::Lit(syn::ExprLit { lit, .. }) => match lit {
syn::Lit::Str(s) => default = s.token().to_string(),
syn::Lit::Char(c) => default = c.token().to_string(),
syn::Lit::Int(i) => default = i.base10_digits().to_string(),
syn::Lit::Float(f) => default = f.base10_digits().to_string(),
syn::Lit::Bool(b) => {
default = if b.value() {
"True".to_string()
} else {
"False".to_string()

Check warning on line 603 in pyo3-macros-backend/src/pyfunction/signature.rs

View check run for this annotation

Codecov / codecov/patch

pyo3-macros-backend/src/pyfunction/signature.rs#L603

Added line #L603 was not covered by tests
}
}
_ => {}

Check warning on line 606 in pyo3-macros-backend/src/pyfunction/signature.rs

View check run for this annotation

Codecov / codecov/patch

pyo3-macros-backend/src/pyfunction/signature.rs#L606

Added line #L606 was not covered by tests
},
// None
syn::Expr::Path(syn::ExprPath {
qself: None, path, ..
}) if path.is_ident("None") => {
default = "None".to_string();
}
// others, unsupported yet so defaults to `...`
_ => {}
}
}
Expand Down
26 changes: 19 additions & 7 deletions tests/test_text_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,26 +154,38 @@ fn test_auto_test_signature_function() {

Python::with_gil(|py| {
let f = wrap_pyfunction!(my_function)(py).unwrap();
py_assert!(py, f, "f.__text_signature__ == '(a, b, c)'");
py_assert!(
py,
f,
"f.__text_signature__ == '(a, b, c)', f.__text_signature__"
);

let f = wrap_pyfunction!(my_function_2)(py).unwrap();
py_assert!(py, f, "f.__text_signature__ == '($module, a, b, c)'");
py_assert!(
py,
f,
"f.__text_signature__ == '($module, a, b, c)', f.__text_signature__"
);

let f = wrap_pyfunction!(my_function_3)(py).unwrap();
py_assert!(py, f, "f.__text_signature__ == '(a, /, b=..., *, c=5)'");
py_assert!(
py,
f,
"f.__text_signature__ == '(a, /, b=None, *, c=5)', f.__text_signature__"
);

let f = wrap_pyfunction!(my_function_4)(py).unwrap();
py_assert!(
py,
f,
"f.__text_signature__ == '(a, /, b=..., *args, c, d=5, **kwargs)'"
"f.__text_signature__ == '(a, /, b=None, *args, c, d=5, **kwargs)', f.__text_signature__"
);

let f = wrap_pyfunction!(my_function_5)(py).unwrap();
py_assert!(
py,
f,
"f.__text_signature__ == '(a=1, /, b=..., c=1.5, d=5, e=\"pyo3\", f=\\'f\\', h=True)', f.__text_signature__"
"f.__text_signature__ == '(a=1, /, b=None, c=1.5, d=5, e=\"pyo3\", f=\\'f\\', h=True)', f.__text_signature__"
);
});
}
Expand Down Expand Up @@ -228,12 +240,12 @@ fn test_auto_test_signature_method() {
py_assert!(
py,
cls,
"cls.method_2.__text_signature__ == '($self, a, /, b=..., *, c=5)'"
"cls.method_2.__text_signature__ == '($self, a, /, b=None, *, c=5)'"
);
py_assert!(
py,
cls,
"cls.method_3.__text_signature__ == '($self, a, /, b=..., *args, c, d=5, **kwargs)'"
"cls.method_3.__text_signature__ == '($self, a, /, b=None, *args, c, d=5, **kwargs)'"
);
py_assert!(
py,
Expand Down

0 comments on commit a4a58f5

Please sign in to comment.