Suppress non_local_definitions lint as we often want the non-local effects in macro code (#4074)

* Resolve references to legacy numerical constants and use the associated constants instead

* Suppress non_local_definitions lint as we often want the non-local effects in macro code
This commit is contained in:
Adam Reichold 2024-04-13 14:59:44 +02:00 committed by David Hewitt
parent 3f6faf00ff
commit 4f7c7065c2
10 changed files with 25 additions and 24 deletions

View File

@ -11,8 +11,8 @@ pub type Py_ssize_t = ::libc::ssize_t;
pub type Py_hash_t = Py_ssize_t; pub type Py_hash_t = Py_ssize_t;
pub type Py_uhash_t = ::libc::size_t; pub type Py_uhash_t = ::libc::size_t;
pub const PY_SSIZE_T_MIN: Py_ssize_t = std::isize::MIN as Py_ssize_t; pub const PY_SSIZE_T_MIN: Py_ssize_t = isize::MIN as Py_ssize_t;
pub const PY_SSIZE_T_MAX: Py_ssize_t = std::isize::MAX as Py_ssize_t; pub const PY_SSIZE_T_MAX: Py_ssize_t = isize::MAX as Py_ssize_t;
#[cfg(target_endian = "big")] #[cfg(target_endian = "big")]
pub const PY_BIG_ENDIAN: usize = 1; pub const PY_BIG_ENDIAN: usize = 1;

View File

@ -249,6 +249,7 @@ pub fn pymodule_module_impl(mut module: syn::ItemMod) -> Result<TokenStream> {
#initialization #initialization
#[allow(unknown_lints, non_local_definitions)]
impl MakeDef { impl MakeDef {
const fn make_def() -> #pyo3_path::impl_::pymodule::ModuleDef { const fn make_def() -> #pyo3_path::impl_::pymodule::ModuleDef {
use #pyo3_path::impl_::pymodule as impl_; use #pyo3_path::impl_::pymodule as impl_;
@ -333,6 +334,7 @@ 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)
#[allow(unknown_lints, non_local_definitions)]
impl #ident::MakeDef { impl #ident::MakeDef {
const fn make_def() -> #pyo3_path::impl_::pymodule::ModuleDef { const fn make_def() -> #pyo3_path::impl_::pymodule::ModuleDef {
fn __pyo3_pymodule(module: &#pyo3_path::Bound<'_, #pyo3_path::types::PyModule>) -> #pyo3_path::PyResult<()> { fn __pyo3_pymodule(module: &#pyo3_path::Bound<'_, #pyo3_path::types::PyModule>) -> #pyo3_path::PyResult<()> {

View File

@ -273,6 +273,7 @@ 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)
#[allow(unknown_lints, non_local_definitions)]
impl #name::MakeDef { impl #name::MakeDef {
const _PYO3_DEF: #pyo3_path::impl_::pymethods::PyMethodDef = #methoddef; const _PYO3_DEF: #pyo3_path::impl_::pymethods::PyMethodDef = #methoddef;
} }

View File

@ -366,6 +366,7 @@ pub fn impl_py_method_def_new(
#deprecations #deprecations
use #pyo3_path::impl_::pyclass::*; use #pyo3_path::impl_::pyclass::*;
#[allow(unknown_lints, non_local_definitions)]
impl PyClassNewTextSignature<#cls> for PyClassImplCollector<#cls> { impl PyClassNewTextSignature<#cls> for PyClassImplCollector<#cls> {
#[inline] #[inline]
fn new_text_signature(self) -> ::std::option::Option<&'static str> { fn new_text_signature(self) -> ::std::option::Option<&'static str> {

View File

@ -4,7 +4,6 @@ use crate::err::{PyErr, PyResult};
use crate::exceptions::PyOverflowError; use crate::exceptions::PyOverflowError;
use crate::ffi::{self, Py_hash_t}; use crate::ffi::{self, Py_hash_t};
use crate::{IntoPy, PyObject, Python}; use crate::{IntoPy, PyObject, Python};
use std::isize;
use std::os::raw::c_int; use std::os::raw::c_int;
/// A type which can be the return type of a python C-API callback /// A type which can be the return type of a python C-API callback
@ -85,11 +84,7 @@ impl IntoPyCallbackOutput<()> for () {
impl IntoPyCallbackOutput<ffi::Py_ssize_t> for usize { impl IntoPyCallbackOutput<ffi::Py_ssize_t> for usize {
#[inline] #[inline]
fn convert(self, _py: Python<'_>) -> PyResult<ffi::Py_ssize_t> { fn convert(self, _py: Python<'_>) -> PyResult<ffi::Py_ssize_t> {
if self <= (isize::MAX as usize) { self.try_into().map_err(|_err| PyOverflowError::new_err(()))
Ok(self as isize)
} else {
Err(PyOverflowError::new_err(()))
}
} }
} }

View File

@ -445,7 +445,7 @@ mod test_128bit_integers {
#[test] #[test]
fn test_i128_max() { fn test_i128_max() {
Python::with_gil(|py| { Python::with_gil(|py| {
let v = std::i128::MAX; let v = i128::MAX;
let obj = v.to_object(py); let obj = v.to_object(py);
assert_eq!(v, obj.extract::<i128>(py).unwrap()); assert_eq!(v, obj.extract::<i128>(py).unwrap());
assert_eq!(v as u128, obj.extract::<u128>(py).unwrap()); assert_eq!(v as u128, obj.extract::<u128>(py).unwrap());
@ -456,7 +456,7 @@ mod test_128bit_integers {
#[test] #[test]
fn test_i128_min() { fn test_i128_min() {
Python::with_gil(|py| { Python::with_gil(|py| {
let v = std::i128::MIN; let v = i128::MIN;
let obj = v.to_object(py); let obj = v.to_object(py);
assert_eq!(v, obj.extract::<i128>(py).unwrap()); assert_eq!(v, obj.extract::<i128>(py).unwrap());
assert!(obj.extract::<i64>(py).is_err()); assert!(obj.extract::<i64>(py).is_err());
@ -467,7 +467,7 @@ mod test_128bit_integers {
#[test] #[test]
fn test_u128_max() { fn test_u128_max() {
Python::with_gil(|py| { Python::with_gil(|py| {
let v = std::u128::MAX; let v = u128::MAX;
let obj = v.to_object(py); let obj = v.to_object(py);
assert_eq!(v, obj.extract::<u128>(py).unwrap()); assert_eq!(v, obj.extract::<u128>(py).unwrap());
assert!(obj.extract::<i128>(py).is_err()); assert!(obj.extract::<i128>(py).is_err());
@ -495,7 +495,7 @@ mod test_128bit_integers {
#[test] #[test]
fn test_nonzero_i128_max() { fn test_nonzero_i128_max() {
Python::with_gil(|py| { Python::with_gil(|py| {
let v = NonZeroI128::new(std::i128::MAX).unwrap(); let v = NonZeroI128::new(i128::MAX).unwrap();
let obj = v.to_object(py); let obj = v.to_object(py);
assert_eq!(v, obj.extract::<NonZeroI128>(py).unwrap()); assert_eq!(v, obj.extract::<NonZeroI128>(py).unwrap());
assert_eq!( assert_eq!(
@ -509,7 +509,7 @@ mod test_128bit_integers {
#[test] #[test]
fn test_nonzero_i128_min() { fn test_nonzero_i128_min() {
Python::with_gil(|py| { Python::with_gil(|py| {
let v = NonZeroI128::new(std::i128::MIN).unwrap(); let v = NonZeroI128::new(i128::MIN).unwrap();
let obj = v.to_object(py); let obj = v.to_object(py);
assert_eq!(v, obj.extract::<NonZeroI128>(py).unwrap()); assert_eq!(v, obj.extract::<NonZeroI128>(py).unwrap());
assert!(obj.extract::<NonZeroI64>(py).is_err()); assert!(obj.extract::<NonZeroI64>(py).is_err());
@ -520,7 +520,7 @@ mod test_128bit_integers {
#[test] #[test]
fn test_nonzero_u128_max() { fn test_nonzero_u128_max() {
Python::with_gil(|py| { Python::with_gil(|py| {
let v = NonZeroU128::new(std::u128::MAX).unwrap(); let v = NonZeroU128::new(u128::MAX).unwrap();
let obj = v.to_object(py); let obj = v.to_object(py);
assert_eq!(v, obj.extract::<NonZeroU128>(py).unwrap()); assert_eq!(v, obj.extract::<NonZeroU128>(py).unwrap());
assert!(obj.extract::<NonZeroI128>(py).is_err()); assert!(obj.extract::<NonZeroI128>(py).is_err());
@ -573,7 +573,7 @@ mod tests {
#[test] #[test]
fn test_u32_max() { fn test_u32_max() {
Python::with_gil(|py| { Python::with_gil(|py| {
let v = std::u32::MAX; let v = u32::MAX;
let obj = v.to_object(py); let obj = v.to_object(py);
assert_eq!(v, obj.extract::<u32>(py).unwrap()); assert_eq!(v, obj.extract::<u32>(py).unwrap());
assert_eq!(u64::from(v), obj.extract::<u64>(py).unwrap()); assert_eq!(u64::from(v), obj.extract::<u64>(py).unwrap());
@ -584,7 +584,7 @@ mod tests {
#[test] #[test]
fn test_i64_max() { fn test_i64_max() {
Python::with_gil(|py| { Python::with_gil(|py| {
let v = std::i64::MAX; let v = i64::MAX;
let obj = v.to_object(py); let obj = v.to_object(py);
assert_eq!(v, obj.extract::<i64>(py).unwrap()); assert_eq!(v, obj.extract::<i64>(py).unwrap());
assert_eq!(v as u64, obj.extract::<u64>(py).unwrap()); assert_eq!(v as u64, obj.extract::<u64>(py).unwrap());
@ -595,7 +595,7 @@ mod tests {
#[test] #[test]
fn test_i64_min() { fn test_i64_min() {
Python::with_gil(|py| { Python::with_gil(|py| {
let v = std::i64::MIN; let v = i64::MIN;
let obj = v.to_object(py); let obj = v.to_object(py);
assert_eq!(v, obj.extract::<i64>(py).unwrap()); assert_eq!(v, obj.extract::<i64>(py).unwrap());
assert!(obj.extract::<i32>(py).is_err()); assert!(obj.extract::<i32>(py).is_err());
@ -606,7 +606,7 @@ mod tests {
#[test] #[test]
fn test_u64_max() { fn test_u64_max() {
Python::with_gil(|py| { Python::with_gil(|py| {
let v = std::u64::MAX; let v = u64::MAX;
let obj = v.to_object(py); let obj = v.to_object(py);
assert_eq!(v, obj.extract::<u64>(py).unwrap()); assert_eq!(v, obj.extract::<u64>(py).unwrap());
assert!(obj.extract::<i64>(py).is_err()); assert!(obj.extract::<i64>(py).is_err());
@ -664,7 +664,7 @@ mod tests {
#[test] #[test]
fn test_nonzero_u32_max() { fn test_nonzero_u32_max() {
Python::with_gil(|py| { Python::with_gil(|py| {
let v = NonZeroU32::new(std::u32::MAX).unwrap(); let v = NonZeroU32::new(u32::MAX).unwrap();
let obj = v.to_object(py); let obj = v.to_object(py);
assert_eq!(v, obj.extract::<NonZeroU32>(py).unwrap()); assert_eq!(v, obj.extract::<NonZeroU32>(py).unwrap());
assert_eq!(NonZeroU64::from(v), obj.extract::<NonZeroU64>(py).unwrap()); assert_eq!(NonZeroU64::from(v), obj.extract::<NonZeroU64>(py).unwrap());
@ -675,7 +675,7 @@ mod tests {
#[test] #[test]
fn test_nonzero_i64_max() { fn test_nonzero_i64_max() {
Python::with_gil(|py| { Python::with_gil(|py| {
let v = NonZeroI64::new(std::i64::MAX).unwrap(); let v = NonZeroI64::new(i64::MAX).unwrap();
let obj = v.to_object(py); let obj = v.to_object(py);
assert_eq!(v, obj.extract::<NonZeroI64>(py).unwrap()); assert_eq!(v, obj.extract::<NonZeroI64>(py).unwrap());
assert_eq!( assert_eq!(
@ -689,7 +689,7 @@ mod tests {
#[test] #[test]
fn test_nonzero_i64_min() { fn test_nonzero_i64_min() {
Python::with_gil(|py| { Python::with_gil(|py| {
let v = NonZeroI64::new(std::i64::MIN).unwrap(); let v = NonZeroI64::new(i64::MIN).unwrap();
let obj = v.to_object(py); let obj = v.to_object(py);
assert_eq!(v, obj.extract::<NonZeroI64>(py).unwrap()); assert_eq!(v, obj.extract::<NonZeroI64>(py).unwrap());
assert!(obj.extract::<NonZeroI32>(py).is_err()); assert!(obj.extract::<NonZeroI32>(py).is_err());
@ -700,7 +700,7 @@ mod tests {
#[test] #[test]
fn test_nonzero_u64_max() { fn test_nonzero_u64_max() {
Python::with_gil(|py| { Python::with_gil(|py| {
let v = NonZeroU64::new(std::u64::MAX).unwrap(); let v = NonZeroU64::new(u64::MAX).unwrap();
let obj = v.to_object(py); let obj = v.to_object(py);
assert_eq!(v, obj.extract::<NonZeroU64>(py).unwrap()); assert_eq!(v, obj.extract::<NonZeroU64>(py).unwrap());
assert!(obj.extract::<NonZeroI64>(py).is_err()); assert!(obj.extract::<NonZeroI64>(py).is_err());

View File

@ -852,6 +852,7 @@ slot_fragment_trait! {
#[macro_export] #[macro_export]
macro_rules! generate_pyclass_richcompare_slot { macro_rules! generate_pyclass_richcompare_slot {
($cls:ty) => {{ ($cls:ty) => {{
#[allow(unknown_lints, non_local_definitions)]
impl $cls { impl $cls {
#[allow(non_snake_case)] #[allow(non_snake_case)]
unsafe extern "C" fn __pymethod___richcmp____( unsafe extern "C" fn __pymethod___richcmp____(

View File

@ -55,7 +55,7 @@ struct BorrowFlag(usize);
impl BorrowFlag { impl BorrowFlag {
pub(crate) const UNUSED: BorrowFlag = BorrowFlag(0); pub(crate) const UNUSED: BorrowFlag = BorrowFlag(0);
const HAS_MUTABLE_BORROW: BorrowFlag = BorrowFlag(usize::max_value()); const HAS_MUTABLE_BORROW: BorrowFlag = BorrowFlag(usize::MAX);
const fn increment(self) -> Self { const fn increment(self) -> Self {
Self(self.0 + 1) Self(self.0 + 1)
} }

View File

@ -2491,6 +2491,7 @@ class SimpleClass:
#[cfg(feature = "macros")] #[cfg(feature = "macros")]
#[test] #[test]
#[allow(unknown_lints, non_local_definitions)]
fn test_hasattr_error() { fn test_hasattr_error() {
use crate::exceptions::PyValueError; use crate::exceptions::PyValueError;
use crate::prelude::*; use crate::prelude::*;

View File

@ -3,7 +3,7 @@
use pyo3::exceptions::{PyAttributeError, PyIndexError, PyValueError}; use pyo3::exceptions::{PyAttributeError, PyIndexError, PyValueError};
use pyo3::types::{PyDict, PyList, PyMapping, PySequence, PySlice, PyType}; use pyo3::types::{PyDict, PyList, PyMapping, PySequence, PySlice, PyType};
use pyo3::{prelude::*, py_run}; use pyo3::{prelude::*, py_run};
use std::{isize, iter}; use std::iter;
#[path = "../src/tests/common.rs"] #[path = "../src/tests/common.rs"]
mod common; mod common;