Skip to content

Commit 388366f

Browse files
committed
extract out PyLong_AsLong __index__ deprecation
Signed-off-by: Michael Carlstrom <[email protected]>
1 parent fc815ec commit 388366f

File tree

3 files changed

+29
-42
lines changed

3 files changed

+29
-42
lines changed

tests/conftest.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
import textwrap
1818
import traceback
1919
import weakref
20-
from typing import Callable
20+
from typing import Callable, SupportsIndex, TypeVar
2121

2222
import pytest
2323

24+
import env
25+
2426
# Early diagnostic for failed imports
2527
try:
2628
import pybind11_tests
@@ -311,3 +313,19 @@ def backport(sanatized_string: SanitizedString) -> SanitizedString:
311313
return sanatized_string
312314

313315
return backport
316+
317+
_EXPECTED_T = TypeVar("_EXPECTED_T")
318+
319+
# The implicit conversion from np.float32 is undesirable but currently accepted.
320+
# TODO: Avoid DeprecationWarning in `PyLong_AsLong` (and similar)
321+
# TODO: PyPy 3.8 does not behave like CPython 3.8 here yet (7.3.7)
322+
# https://github.com/pybind/pybind11/issues/3408
323+
@pytest.fixture
324+
def avoid_PyLong_AsLong_deprecation() -> Callable[[Callable[[SupportsIndex], _EXPECTED_T], SupportsIndex, _EXPECTED_T], bool]:
325+
def check(convert: Callable[[SupportsIndex], _EXPECTED_T], value: SupportsIndex, expected: _EXPECTED_T) -> bool:
326+
if sys.version_info < (3, 10) and env.CPYTHON:
327+
with pytest.deprecated_call():
328+
return convert(value) == expected
329+
else:
330+
return convert(value) == expected
331+
return check

tests/env.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import sys
55
import sysconfig
66

7-
import pytest
8-
97
ANDROID = sys.platform.startswith("android")
108
LINUX = sys.platform.startswith("linux")
119
MACOS = sys.platform.startswith("darwin")
@@ -29,19 +27,3 @@
2927
or GRAALPY
3028
or (CPYTHON and PY_GIL_DISABLED and (3, 13) <= sys.version_info < (3, 14))
3129
)
32-
33-
34-
def deprecated_call():
35-
"""
36-
pytest.deprecated_call() seems broken in pytest<3.9.x; concretely, it
37-
doesn't work on CPython 3.8.0 with pytest==3.3.2 on Ubuntu 18.04 (#2922).
38-
39-
This is a narrowed reimplementation of the following PR :(
40-
https://github.com/pytest-dev/pytest/pull/4104
41-
"""
42-
# TODO: Remove this when testing requires pytest>=3.9.
43-
pieces = pytest.__version__.split(".")
44-
pytest_major_minor = (int(pieces[0]), int(pieces[1]))
45-
if pytest_major_minor < (3, 9):
46-
return pytest.warns((DeprecationWarning, PendingDeprecationWarning))
47-
return pytest.deprecated_call()

tests/test_builtin_casters.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def test_integer_casting():
247247
assert "incompatible function arguments" in str(excinfo.value)
248248

249249

250-
def test_int_convert(doc):
250+
def test_int_convert(doc, avoid_PyLong_AsLong_deprecation):
251251
class Int:
252252
def __int__(self):
253253
return 42
@@ -300,15 +300,9 @@ def cant_convert(v):
300300

301301
assert convert(7) == 7
302302
assert noconvert(7) == 7
303-
assert convert(3.14159) == 3
303+
assert avoid_PyLong_AsLong_deprecation(convert, 3.14159, 3)
304304
requires_conversion(3.14159)
305-
# TODO: Avoid DeprecationWarning in `PyLong_AsLong` (and similar)
306-
# TODO: PyPy 3.8 does not behave like CPython 3.8 here yet (7.3.7)
307-
if sys.version_info < (3, 10) and env.CPYTHON:
308-
with env.deprecated_call():
309-
assert convert(Int()) == 42
310-
else:
311-
assert convert(Int()) == 42
305+
assert avoid_PyLong_AsLong_deprecation(convert, Int(), 42)
312306
requires_conversion(Int())
313307
cant_convert(NotInt())
314308
cant_convert(Float())
@@ -326,7 +320,7 @@ def cant_convert(v):
326320
requires_conversion(RaisingValueErrorOnIndex())
327321

328322

329-
def test_float_convert(doc):
323+
def test_float_convert(doc, avoid_PyLong_AsLong_deprecation):
330324
class Int:
331325
def __int__(self):
332326
return -5
@@ -357,12 +351,12 @@ def cant_convert(v):
357351
assert pytest.approx(convert(Float())) == 41.45
358352
assert pytest.approx(convert(Index())) == -7.0
359353
assert isinstance(convert(Float()), float)
360-
assert pytest.approx(convert(3)) == 3.0
354+
assert avoid_PyLong_AsLong_deprecation(convert, 3, 3.0)
361355
assert pytest.approx(noconvert(3)) == 3.0
362356
cant_convert(Int())
363357

364358

365-
def test_numpy_int_convert():
359+
def test_numpy_int_convert(avoid_PyLong_AsLong_deprecation):
366360
np = pytest.importorskip("numpy")
367361

368362
convert, noconvert = m.int_passthrough, m.int_passthrough_noconvert
@@ -375,14 +369,7 @@ def require_implicit(v):
375369
assert noconvert(np.intc(42)) == 42
376370

377371
# The implicit conversion from np.float32 is undesirable but currently accepted.
378-
# TODO: Avoid DeprecationWarning in `PyLong_AsLong` (and similar)
379-
# TODO: PyPy 3.8 does not behave like CPython 3.8 here yet (7.3.7)
380-
# https://github.com/pybind/pybind11/issues/3408
381-
if (3, 8) <= sys.version_info < (3, 10) and env.CPYTHON:
382-
with env.deprecated_call():
383-
assert convert(np.float32(3.14159)) == 3
384-
else:
385-
assert convert(np.float32(3.14159)) == 3
372+
assert avoid_PyLong_AsLong_deprecation(convert, np.float32(3.14159), 3)
386373
require_implicit(np.float32(3.14159))
387374

388375

@@ -483,7 +470,7 @@ def test_reference_wrapper():
483470
assert m.refwrap_call_iiw(IncType(10), m.refwrap_iiw) == [10, 10, 10, 10]
484471

485472

486-
def test_complex_cast(doc):
473+
def test_complex_cast(doc, avoid_PyLong_AsLong_deprecation):
487474
"""std::complex casts"""
488475

489476
class Complex:
@@ -526,8 +513,8 @@ def cant_convert(v):
526513
)
527514
assert doc(noconvert) == "complex_noconvert(arg0: complex) -> complex"
528515

529-
assert convert(1) == 1.0
530-
assert convert(2.0) == 2.0
516+
assert avoid_PyLong_AsLong_deprecation(convert, 1, 1.0)
517+
assert avoid_PyLong_AsLong_deprecation(convert, 2.0, 2.0)
531518
assert convert(1 + 5j) == 1.0 + 5.0j
532519
assert convert(Complex()) == 5.0 + 4j
533520
assert convert(Float()) == 5.0

0 commit comments

Comments
 (0)