Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix docstrings generated by [pyo3(get, set)] #755

Merged
merged 1 commit into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* `PyClass`, `PyClassShell`, `PyObjectLayout`, `PyClassInitializer`. [#683](https://github.com/PyO3/pyo3/pull/683)
* Implemented `IntoIterator` for `PySet` and `PyFrozenSet`. [#716](https://github.com/PyO3/pyo3/pull/716)
* `FromPyObject` is now automatically implemented for `T: Clone` pyclasses. [#730](https://github.com/PyO3/pyo3/pull/730)
* `#[pyo3(get)]` and `#[pyo3(set)]` will now use the Rust doc-comment from the field for the Python property. [#755](https://github.com/PyO3/pyo3/pull/755)

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions pyo3-derive-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,8 @@ fn impl_descriptors(
.map(|desc| {
let name = field.ident.as_ref().unwrap();

// FIXME better doc?
let doc = syn::LitStr::new(&name.to_string(), name.span());
let doc = utils::get_doc(&field.attrs, None, true)
.unwrap_or_else(|_| syn::LitStr::new(&name.to_string(), name.span()));

let field_ty = &field.ty;
match *desc {
Expand Down
3 changes: 3 additions & 0 deletions src/class/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ impl PySetterDef {
.expect("Method name must not contain NULL byte")
.into_raw();
}
if dst.doc.is_null() {
dst.doc = self.doc.as_ptr() as *mut libc::c_char;
}
dst.set = Some(self.meth);
}
}
Expand Down
29 changes: 28 additions & 1 deletion tests/test_class_basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@ fn empty_class() {
/// Line3
// this is not doc string
#[pyclass]
struct ClassWithDocs {}
struct ClassWithDocs {
/// Property field
#[pyo3(get, set)]
value: i32,

/// Read-only property field
#[pyo3(get)]
readonly: i32,

/// Write-only property field
#[pyo3(set)]
writeonly: i32,
}

#[test]
fn class_with_docstr() {
Expand All @@ -36,6 +48,21 @@ fn class_with_docstr() {
typeobj,
"assert typeobj.__doc__ == 'Line1\\nLine2\\n Line3'"
);
py_run!(
py,
typeobj,
"assert typeobj.value.__doc__ == 'Property field'"
);
py_run!(
py,
typeobj,
"assert typeobj.readonly.__doc__ == 'Read-only property field'"
);
py_run!(
py,
typeobj,
"assert typeobj.writeonly.__doc__ == 'Write-only property field'"
);
}
}

Expand Down