Fix non_local_definitions lint triggers (#3955)

This commit is contained in:
Bruno Kolenbrander 2024-03-12 23:59:33 +01:00 committed by GitHub
parent 7cde95bba4
commit 989d2c53ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 63 additions and 93 deletions

View File

@ -310,7 +310,7 @@ impl<'a> Container<'a> {
} }
}); });
quote!( quote!(
match obj.extract() { match #pyo3_path::types::PyAnyMethods::extract(obj) {
::std::result::Result::Ok((#(#field_idents),*)) => ::std::result::Result::Ok(#self_ty(#(#fields),*)), ::std::result::Result::Ok((#(#field_idents),*)) => ::std::result::Result::Ok(#self_ty(#(#fields),*)),
::std::result::Result::Err(err) => ::std::result::Result::Err(err), ::std::result::Result::Err(err) => ::std::result::Result::Err(err),
} }
@ -327,27 +327,29 @@ impl<'a> Container<'a> {
let field_name = ident.to_string(); let field_name = ident.to_string();
let getter = match field.getter.as_ref().unwrap_or(&FieldGetter::GetAttr(None)) { let getter = match field.getter.as_ref().unwrap_or(&FieldGetter::GetAttr(None)) {
FieldGetter::GetAttr(Some(name)) => { FieldGetter::GetAttr(Some(name)) => {
quote!(getattr(#pyo3_path::intern!(obj.py(), #name))) quote!(#pyo3_path::types::PyAnyMethods::getattr(obj, #pyo3_path::intern!(obj.py(), #name)))
} }
FieldGetter::GetAttr(None) => { FieldGetter::GetAttr(None) => {
quote!(getattr(#pyo3_path::intern!(obj.py(), #field_name))) quote!(#pyo3_path::types::PyAnyMethods::getattr(obj, #pyo3_path::intern!(obj.py(), #field_name)))
} }
FieldGetter::GetItem(Some(syn::Lit::Str(key))) => { FieldGetter::GetItem(Some(syn::Lit::Str(key))) => {
quote!(get_item(#pyo3_path::intern!(obj.py(), #key))) quote!(#pyo3_path::types::PyAnyMethods::get_item(obj, #pyo3_path::intern!(obj.py(), #key)))
}
FieldGetter::GetItem(Some(key)) => {
quote!(#pyo3_path::types::PyAnyMethods::get_item(obj, #key))
} }
FieldGetter::GetItem(Some(key)) => quote!(get_item(#key)),
FieldGetter::GetItem(None) => { FieldGetter::GetItem(None) => {
quote!(get_item(#pyo3_path::intern!(obj.py(), #field_name))) quote!(#pyo3_path::types::PyAnyMethods::get_item(obj, #pyo3_path::intern!(obj.py(), #field_name)))
} }
}; };
let extractor = match &field.from_py_with { let extractor = match &field.from_py_with {
None => { None => {
quote!(#pyo3_path::impl_::frompyobject::extract_struct_field(&obj.#getter?, #struct_name, #field_name)?) quote!(#pyo3_path::impl_::frompyobject::extract_struct_field(&#getter?, #struct_name, #field_name)?)
} }
Some(FromPyWithAttribute { Some(FromPyWithAttribute {
value: expr_path, .. value: expr_path, ..
}) => { }) => {
quote! (#pyo3_path::impl_::frompyobject::extract_struct_field_with(#expr_path as fn(_) -> _, &obj.#getter?, #struct_name, #field_name)?) quote! (#pyo3_path::impl_::frompyobject::extract_struct_field_with(#expr_path as fn(_) -> _, &#getter?, #struct_name, #field_name)?)
} }
}; };
@ -609,17 +611,11 @@ pub fn build_derive_from_pyobject(tokens: &DeriveInput) -> Result<TokenStream> {
let ident = &tokens.ident; let ident = &tokens.ident;
Ok(quote!( Ok(quote!(
// FIXME https://github.com/PyO3/pyo3/issues/3903 #[automatically_derived]
#[allow(unknown_lints, non_local_definitions)] impl #trait_generics #pyo3_path::FromPyObject<#lt_param> for #ident #generics #where_clause {
const _: () = { fn extract_bound(obj: &#pyo3_path::Bound<#lt_param, #pyo3_path::PyAny>) -> #pyo3_path::PyResult<Self> {
use #pyo3_path::prelude::PyAnyMethods; #derives
#[automatically_derived]
impl #trait_generics #pyo3_path::FromPyObject<#lt_param> for #ident #generics #where_clause {
fn extract_bound(obj: &#pyo3_path::Bound<#lt_param, #pyo3_path::PyAny>) -> #pyo3_path::PyResult<Self> {
#derives
}
} }
}; }
)) ))
} }

View File

@ -295,7 +295,7 @@ pub fn pymodule_function_impl(mut function: syn::ItemFn) -> Result<TokenStream>
if function.sig.inputs.len() == 2 { if function.sig.inputs.len() == 2 {
module_args.push(quote!(module.py())); module_args.push(quote!(module.py()));
} }
module_args.push(quote!(::std::convert::Into::into(BoundRef(module)))); module_args.push(quote!(::std::convert::Into::into(#pyo3_path::methods::BoundRef(module))));
let extractors = function let extractors = function
.sig .sig
@ -330,29 +330,22 @@ pub fn pymodule_function_impl(mut function: syn::ItemFn) -> Result<TokenStream>
// this avoids complications around the fact that the generated module has a different scope // this avoids complications around the fact that the generated module has a different scope
// (and `super` doesn't always refer to the outer scope, e.g. if the `#[pymodule] is // (and `super` doesn't always refer to the outer scope, e.g. if the `#[pymodule] is
// inside a function body) // inside a function body)
// FIXME https://github.com/PyO3/pyo3/issues/3903 impl #ident::MakeDef {
#[allow(unknown_lints, non_local_definitions)] const fn make_def() -> #pyo3_path::impl_::pymodule::ModuleDef {
const _: () = { fn __pyo3_pymodule(module: &#pyo3_path::Bound<'_, #pyo3_path::types::PyModule>) -> #pyo3_path::PyResult<()> {
use #pyo3_path::impl_::pymodule as impl_; #ident(#(#module_args),*)
use #pyo3_path::impl_::pymethods::BoundRef; }
fn __pyo3_pymodule(module: &#pyo3_path::Bound<'_, #pyo3_path::types::PyModule>) -> #pyo3_path::PyResult<()> { const INITIALIZER: #pyo3_path::impl_::pymodule::ModuleInitializer = #pyo3_path::impl_::pymodule::ModuleInitializer(__pyo3_pymodule);
#ident(#(#module_args),*) unsafe {
} #pyo3_path::impl_::pymodule::ModuleDef::new(
#ident::__PYO3_NAME,
impl #ident::MakeDef { #doc,
const fn make_def() -> impl_::ModuleDef { INITIALIZER
const INITIALIZER: impl_::ModuleInitializer = impl_::ModuleInitializer(__pyo3_pymodule); )
unsafe {
impl_::ModuleDef::new(
#ident::__PYO3_NAME,
#doc,
INITIALIZER
)
}
} }
} }
}; }
}) })
} }

View File

@ -366,15 +366,11 @@ fn impl_class(
.impl_all(ctx)?; .impl_all(ctx)?;
Ok(quote! { Ok(quote! {
// FIXME https://github.com/PyO3/pyo3/issues/3903 impl #pyo3_path::types::DerefToPyAny for #cls {}
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
impl #pyo3_path::types::DerefToPyAny for #cls {}
#pytypeinfo_impl #pytypeinfo_impl
#py_class_impl #py_class_impl
};
}) })
} }
@ -794,21 +790,17 @@ fn impl_simple_enum(
.impl_all(ctx)?; .impl_all(ctx)?;
Ok(quote! { Ok(quote! {
// FIXME https://github.com/PyO3/pyo3/issues/3903 #pytypeinfo
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
#pytypeinfo
#pyclass_impls #pyclass_impls
#[doc(hidden)] #[doc(hidden)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
impl #cls { impl #cls {
#default_repr #default_repr
#default_int #default_int
#default_richcmp #default_richcmp
} }
};
}) })
} }
@ -933,25 +925,21 @@ fn impl_complex_enum(
} }
Ok(quote! { Ok(quote! {
// FIXME https://github.com/PyO3/pyo3/issues/3903 #pytypeinfo
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
#pytypeinfo
#pyclass_impls #pyclass_impls
#[doc(hidden)] #[doc(hidden)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
impl #cls {} impl #cls {}
#(#variant_cls_zsts)* #(#variant_cls_zsts)*
#(#variant_cls_pytypeinfos)* #(#variant_cls_pytypeinfos)*
#(#variant_cls_pyclass_impls)* #(#variant_cls_pyclass_impls)*
#(#variant_cls_impls)* #(#variant_cls_impls)*
};
}) })
} }

View File

@ -282,16 +282,12 @@ pub fn impl_wrap_pyfunction(
// this avoids complications around the fact that the generated module has a different scope // this avoids complications around the fact that the generated module has a different scope
// (and `super` doesn't always refer to the outer scope, e.g. if the `#[pyfunction] is // (and `super` doesn't always refer to the outer scope, e.g. if the `#[pyfunction] is
// inside a function body) // inside a function body)
// FIXME https://github.com/PyO3/pyo3/issues/3903 impl #name::MakeDef {
#[allow(unknown_lints, non_local_definitions)] const DEF: #pyo3_path::impl_::pymethods::PyMethodDef = #methoddef;
const _: () = { }
impl #name::MakeDef {
const DEF: #pyo3_path::impl_::pymethods::PyMethodDef = #methoddef;
}
#[allow(non_snake_case)] #[allow(non_snake_case)]
#wrapper #wrapper
};
}; };
Ok(wrapped_pyfunction) Ok(wrapped_pyfunction)
} }

View File

@ -170,19 +170,15 @@ pub fn impl_methods(
}; };
Ok(quote! { Ok(quote! {
// FIXME https://github.com/PyO3/pyo3/issues/3903 #(#trait_impls)*
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
#(#trait_impls)*
#items #items
#[doc(hidden)] #[doc(hidden)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
impl #ty { impl #ty {
#(#associated_methods)* #(#associated_methods)*
} }
};
}) })
} }

View File

@ -14,6 +14,7 @@ struct Derive2(i32, i32); // tuple case
#[allow(dead_code)] #[allow(dead_code)]
struct Derive3 { struct Derive3 {
f: i32, f: i32,
#[pyo3(item(42))]
g: i32, g: i32,
} // struct case } // struct case