diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 907b51e7e..443996b0e 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -135,8 +135,8 @@ class ApproxBase: def _recursive_sequence_map(f, x): """Recursively map a function over a sequence of arbitary depth""" - if isinstance(x, Sequence): - seq_type = type(x) + seq_type = type(x) + if seq_type in (list, tuple): return seq_type(_recursive_sequence_map(f, xi) for xi in x) else: return f(x) diff --git a/testing/python/approx.py b/testing/python/approx.py index 6cb3f6cf8..6acb466ff 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -147,20 +147,6 @@ class TestApprox: ], ) - assert_approx_raises_regex( - range(0, 3), - range(1, 4), - [ - r" comparison failed. Mismatched elements: 3 / 3:", - rf" Max absolute difference: {SOME_FLOAT}", - rf" Max relative difference: {SOME_FLOAT}", - r" Index \| Obtained\s+\| Expected ", - rf" 0 \| {SOME_FLOAT} \| {SOME_FLOAT} ± {SOME_FLOAT}", - rf" 1 \| {SOME_FLOAT} \| {SOME_FLOAT} ± {SOME_FLOAT}", - rf" 2 \| {SOME_FLOAT} \| {SOME_FLOAT} ± {SOME_FLOAT}", - ], - ) - # Specific test for comparison with 0.0 (relative diff will be 'inf') assert_approx_raises_regex( [0.0], @@ -912,11 +898,25 @@ class TestRecursiveSequenceMap: def test_map_over_scalar(self): assert _recursive_sequence_map(sqrt, 16) == 4 + def test_map_over_empty_list(self): + assert _recursive_sequence_map(sqrt, []) == [] + def test_map_over_list(self): assert _recursive_sequence_map(sqrt, [4, 16, 25, 676]) == [2, 4, 5, 26] def test_map_over_tuple(self): assert _recursive_sequence_map(sqrt, (4, 16, 25, 676)) == (2, 4, 5, 26) - def test_map_over_range(self): - assert _recursive_sequence_map(lambda x: 2 * x, range(0, 5)) == range(0, 9, 2) + def test_map_over_nested_lists(self): + assert _recursive_sequence_map(sqrt, [4, [25, 64], [[49]]]) == [ + 2, + [5, 8], + [[7]], + ] + + def test_map_over_mixed_sequence(self): + assert _recursive_sequence_map(sqrt, [4, (25, 64), [(49)]]) == [ + 2, + (5, 8), + [(7)], + ]