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 non_local_definitions lint triggers #3955

Merged
merged 1 commit into from
Mar 13, 2024
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
34 changes: 15 additions & 19 deletions pyo3-macros-backend/src/frompyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ impl<'a> Container<'a> {
}
});
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::Err(err) => ::std::result::Result::Err(err),
}
Expand All @@ -327,27 +327,29 @@ impl<'a> Container<'a> {
let field_name = ident.to_string();
let getter = match field.getter.as_ref().unwrap_or(&FieldGetter::GetAttr(None)) {
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) => {
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))) => {
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) => {
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 {
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 {
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)?)
}
};

Expand Down Expand Up @@ -609,17 +611,11 @@ pub fn build_derive_from_pyobject(tokens: &DeriveInput) -> Result<TokenStream> {

let ident = &tokens.ident;
Ok(quote!(
// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
use #pyo3_path::prelude::PyAnyMethods;

#[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
}
#[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
}
};
}
))
}
35 changes: 14 additions & 21 deletions pyo3-macros-backend/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ pub fn pymodule_function_impl(mut function: syn::ItemFn) -> Result<TokenStream>
if function.sig.inputs.len() == 2 {
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
.sig
Expand Down Expand Up @@ -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
// (and `super` doesn't always refer to the outer scope, e.g. if the `#[pymodule] is
// inside a function body)
// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
use #pyo3_path::impl_::pymodule as impl_;
use #pyo3_path::impl_::pymethods::BoundRef;

fn __pyo3_pymodule(module: &#pyo3_path::Bound<'_, #pyo3_path::types::PyModule>) -> #pyo3_path::PyResult<()> {
#ident(#(#module_args),*)
}
impl #ident::MakeDef {
const fn make_def() -> #pyo3_path::impl_::pymodule::ModuleDef {
fn __pyo3_pymodule(module: &#pyo3_path::Bound<'_, #pyo3_path::types::PyModule>) -> #pyo3_path::PyResult<()> {
#ident(#(#module_args),*)
}

impl #ident::MakeDef {
const fn make_def() -> impl_::ModuleDef {
const INITIALIZER: impl_::ModuleInitializer = impl_::ModuleInitializer(__pyo3_pymodule);
unsafe {
impl_::ModuleDef::new(
#ident::__PYO3_NAME,
#doc,
INITIALIZER
)
}
const INITIALIZER: #pyo3_path::impl_::pymodule::ModuleInitializer = #pyo3_path::impl_::pymodule::ModuleInitializer(__pyo3_pymodule);
unsafe {
#pyo3_path::impl_::pymodule::ModuleDef::new(
#ident::__PYO3_NAME,
#doc,
INITIALIZER
)
}
}
};
}
})
}

Expand Down
54 changes: 21 additions & 33 deletions pyo3-macros-backend/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,15 +366,11 @@ fn impl_class(
.impl_all(ctx)?;

Ok(quote! {
// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
impl #pyo3_path::types::DerefToPyAny for #cls {}
impl #pyo3_path::types::DerefToPyAny for #cls {}

#pytypeinfo_impl
#pytypeinfo_impl

#py_class_impl
};
#py_class_impl
})
}

Expand Down Expand Up @@ -794,21 +790,17 @@ fn impl_simple_enum(
.impl_all(ctx)?;

Ok(quote! {
// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
#pytypeinfo
#pytypeinfo

#pyclass_impls
#pyclass_impls

#[doc(hidden)]
#[allow(non_snake_case)]
impl #cls {
#default_repr
#default_int
#default_richcmp
}
};
#[doc(hidden)]
#[allow(non_snake_case)]
impl #cls {
#default_repr
#default_int
#default_richcmp
}
})
}

Expand Down Expand Up @@ -933,25 +925,21 @@ fn impl_complex_enum(
}

Ok(quote! {
// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
#pytypeinfo
#pytypeinfo

#pyclass_impls
#pyclass_impls

#[doc(hidden)]
#[allow(non_snake_case)]
impl #cls {}
#[doc(hidden)]
#[allow(non_snake_case)]
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)*
})
}

Expand Down
14 changes: 5 additions & 9 deletions pyo3-macros-backend/src/pyfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,16 +282,12 @@ pub fn impl_wrap_pyfunction(
// 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
// inside a function body)
// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
impl #name::MakeDef {
const DEF: #pyo3_path::impl_::pymethods::PyMethodDef = #methoddef;
}
impl #name::MakeDef {
const DEF: #pyo3_path::impl_::pymethods::PyMethodDef = #methoddef;
}

#[allow(non_snake_case)]
#wrapper
};
#[allow(non_snake_case)]
#wrapper
};
Ok(wrapped_pyfunction)
}
18 changes: 7 additions & 11 deletions pyo3-macros-backend/src/pyimpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,15 @@ pub fn impl_methods(
};

Ok(quote! {
// FIXME https://github.com/PyO3/pyo3/issues/3903
#[allow(unknown_lints, non_local_definitions)]
const _: () = {
#(#trait_impls)*
#(#trait_impls)*

#items
#items

#[doc(hidden)]
#[allow(non_snake_case)]
impl #ty {
#(#associated_methods)*
}
};
#[doc(hidden)]
#[allow(non_snake_case)]
impl #ty {
#(#associated_methods)*
}
})
}

Expand Down
1 change: 1 addition & 0 deletions src/tests/hygiene/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct Derive2(i32, i32); // tuple case
#[allow(dead_code)]
struct Derive3 {
f: i32,
#[pyo3(item(42))]
g: i32,
} // struct case

Expand Down
Loading