pyo3/tests/test_text_signature.rs

225 lines
5.1 KiB
Rust
Raw Normal View History

2021-12-03 00:03:32 +00:00
#![cfg(feature = "macros")]
use pyo3::prelude::*;
2021-06-24 14:18:48 +00:00
use pyo3::{types::PyType, wrap_pymodule, PyCell};
mod common;
#[test]
fn class_without_docs_or_signature() {
#[pyclass]
struct MyClass {}
2022-07-19 17:34:23 +00:00
Python::with_gil(|py| {
let typeobj = py.get_type::<MyClass>();
2022-07-19 17:34:23 +00:00
py_assert!(py, typeobj, "typeobj.__doc__ is None");
py_assert!(py, typeobj, "typeobj.__text_signature__ is None");
});
}
#[test]
fn class_with_docs() {
/// docs line1
#[pyclass]
/// docs line2
struct MyClass {}
2022-07-19 17:34:23 +00:00
Python::with_gil(|py| {
let typeobj = py.get_type::<MyClass>();
2022-07-19 17:34:23 +00:00
py_assert!(py, typeobj, "typeobj.__doc__ == 'docs line1\\ndocs line2'");
py_assert!(py, typeobj, "typeobj.__text_signature__ is None");
});
}
#[test]
#[cfg_attr(all(Py_LIMITED_API, not(Py_3_10)), ignore)]
fn class_with_docs_and_signature() {
/// docs line1
#[pyclass]
/// docs line2
#[pyo3(text_signature = "(a, b=None, *, c=42)")]
/// docs line3
struct MyClass {}
#[pymethods]
impl MyClass {
#[new]
#[args(a, b = "None", "*", c = 42)]
2019-12-14 14:16:39 +00:00
fn __new__(a: i32, b: Option<i32>, c: i32) -> Self {
let _ = (a, b, c);
2019-12-14 14:16:39 +00:00
Self {}
}
}
2022-07-19 17:34:23 +00:00
Python::with_gil(|py| {
let typeobj = py.get_type::<MyClass>();
py_assert!(
py,
typeobj,
"typeobj.__doc__ == 'docs line1\\ndocs line2\\ndocs line3'"
);
py_assert!(
py,
typeobj,
"typeobj.__text_signature__ == '(a, b=None, *, c=42)'"
);
});
}
#[test]
#[cfg_attr(all(Py_LIMITED_API, not(Py_3_10)), ignore)]
fn class_with_signature() {
#[pyclass]
#[pyo3(text_signature = "(a, b=None, *, c=42)")]
struct MyClass {}
#[pymethods]
impl MyClass {
#[new]
#[args(a, b = "None", "*", c = 42)]
2019-12-14 14:16:39 +00:00
fn __new__(a: i32, b: Option<i32>, c: i32) -> Self {
let _ = (a, b, c);
2019-12-14 14:16:39 +00:00
Self {}
}
}
2022-07-19 17:34:23 +00:00
Python::with_gil(|py| {
let typeobj = py.get_type::<MyClass>();
py_assert!(
py,
typeobj,
"typeobj.__doc__ is None or typeobj.__doc__ == ''"
);
py_assert!(
py,
typeobj,
"typeobj.__text_signature__ == '(a, b=None, *, c=42)'"
);
});
}
#[test]
fn test_function() {
#[pyfunction(a, b = "None", "*", c = 42)]
#[pyo3(text_signature = "(a, b=None, *, c=42)")]
fn my_function(a: i32, b: Option<i32>, c: i32) {
let _ = (a, b, c);
}
2022-07-19 17:34:23 +00:00
Python::with_gil(|py| {
let f = wrap_pyfunction!(my_function)(py).unwrap();
2022-07-19 17:34:23 +00:00
py_assert!(py, f, "f.__text_signature__ == '(a, b=None, *, c=42)'");
});
}
#[test]
fn test_pyfn() {
#[pymodule]
2022-03-23 07:07:28 +00:00
fn my_module(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
2021-05-16 09:41:59 +00:00
#[pyfn(m, a, b = "None", "*", c = 42)]
#[pyo3(text_signature = "(a, b=None, *, c=42)")]
fn my_function(a: i32, b: Option<i32>, c: i32) {
let _ = (a, b, c);
}
Ok(())
}
2022-07-19 17:34:23 +00:00
Python::with_gil(|py| {
let m = wrap_pymodule!(my_module)(py);
2022-07-19 17:34:23 +00:00
py_assert!(
py,
m,
"m.my_function.__text_signature__ == '(a, b=None, *, c=42)'"
);
});
}
#[test]
fn test_methods() {
#[pyclass]
struct MyClass {}
#[pymethods]
impl MyClass {
#[pyo3(text_signature = "($self, a)")]
fn method(&self, a: i32) {
let _ = a;
}
#[pyo3(text_signature = "($self, b)")]
2020-02-03 13:25:16 +00:00
fn pyself_method(_this: &PyCell<Self>, b: i32) {
let _ = b;
}
#[classmethod]
#[pyo3(text_signature = "($cls, c)")]
fn class_method(_cls: &PyType, c: i32) {
let _ = c;
}
#[staticmethod]
#[pyo3(text_signature = "(d)")]
fn static_method(d: i32) {
let _ = d;
}
}
2022-07-19 17:34:23 +00:00
Python::with_gil(|py| {
let typeobj = py.get_type::<MyClass>();
py_assert!(
py,
typeobj,
"typeobj.method.__text_signature__ == '($self, a)'"
);
py_assert!(
py,
typeobj,
"typeobj.pyself_method.__text_signature__ == '($self, b)'"
);
py_assert!(
py,
typeobj,
"typeobj.class_method.__text_signature__ == '($cls, c)'"
);
py_assert!(
py,
typeobj,
"typeobj.static_method.__text_signature__ == '(d)'"
);
});
}
#[test]
#[cfg_attr(all(Py_LIMITED_API, not(Py_3_10)), ignore)]
fn test_raw_identifiers() {
#[pyclass]
#[pyo3(text_signature = "($self)")]
struct r#MyClass {}
#[pymethods]
impl MyClass {
#[new]
fn new() -> MyClass {
MyClass {}
}
#[pyo3(text_signature = "($self)")]
fn r#method(&self) {}
}
2022-07-19 17:34:23 +00:00
Python::with_gil(|py| {
let typeobj = py.get_type::<MyClass>();
2022-07-19 17:34:23 +00:00
py_assert!(py, typeobj, "typeobj.__text_signature__ == '($self)'");
2022-07-19 17:34:23 +00:00
py_assert!(
py,
typeobj,
"typeobj.method.__text_signature__ == '($self)'"
);
});
}