Use Rust mod document first
See https://github.com/RustPython/RustPython/pull/3145\#discussion_r716219438
@@ -20,8 +20,8 @@ pub fn impl_pymodule(
attr: AttributeArgs,
module_item: Item,
) -> std::result::Result<TokenStream, Diagnostic> {
- let mut module_item = match module_item {
- Item::Mod(m) => m,
+ let (doc, mut module_item) = match module_item {
+ Item::Mod(m) => (m.attrs.doc(), m),
other => bail_span!(other, "#[pymodule] can only be on a full module"),
};
let fake_ident = Ident::new("pymodule", module_item.span());
@@ -58,7 +58,12 @@ pub fn impl_pymodule(
// append additional items
let module_name = context.name.as_str();
let module_extend_items = context.module_extend_items;
- let doc = crate::doc::try_read(module_name).ok().flatten();
+ let doc = doc.or_else(|| {
+ crate::doc::try_read(module_name)
+ .ok()
+ .flatten()
+ .map(str::to_owned)
+ });
let doc = if let Some(doc) = doc {
quote!(Some(#doc))
} else {
