Skip to content

Commit

Permalink
Merge pull request #755 from davidhewitt/property-doc
Browse files Browse the repository at this point in the history
Fix docstrings generated by `[pyo3(get, set)]`
  • Loading branch information
kngwyu authored Feb 3, 2020
2 parents 8fea23a + dfb7d7c commit 14980d7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
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 @@ -464,8 +464,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 @@ -22,7 +22,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 @@ -35,6 +47,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

0 comments on commit 14980d7

Please sign in to comment.