From 1f639e2c22ca92f85521db7dd05f743e946ff338 Mon Sep 17 00:00:00 2001 From: Victor Maryama Date: Sat, 12 Oct 2019 14:33:43 +0200 Subject: [PATCH 1/4] Casting fixture parameter to list at the beginning of parameter parsing. --- setup.py | 1 + src/_pytest/fixtures.py | 5 +++-- testing/python/fixtures.py | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index dcf63f6fd..be38857b0 100644 --- a/setup.py +++ b/setup.py @@ -28,6 +28,7 @@ def main(): "mock", "nose", "requests", + "numpy", "xmlschema", ] }, diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index b4b10fcd2..ed7e715de 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1113,6 +1113,9 @@ def fixture( ``fixture_`` and then use ``@pytest.fixture(name='')``. """ + if params is not None and not isinstance(params, (list, tuple)): + params = list(params) + fixture_function, arguments = _parse_fixture_args( callable_or_scope, *args, @@ -1134,8 +1137,6 @@ def fixture( fixture_function ) - if params is not None and not isinstance(params, (list, tuple)): - params = list(params) return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name) diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index f4dbfdf09..e0b464c96 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -4187,3 +4187,21 @@ def test_indirect_fixture_does_not_break_scope(testdir): ) result = testdir.runpytest() result.assert_outcomes(passed=7) + + +def test_fixture_parametrization_nparray(testdir): + testdir.makepyfile( + """ + from numpy import linspace + from pytest import fixture + + @fixture(params=linspace(1, 10, 10)) + def value(request): + return request.param + + def test_bug(value): + assert value == value + """ + ) + result = testdir.runpytest() + result.assert_outcomes(passed=10) From 122748a6cf12e6853c8b2ee2576f91f2b722706d Mon Sep 17 00:00:00 2001 From: Victor Maryama Date: Sat, 12 Oct 2019 14:38:58 +0200 Subject: [PATCH 2/4] Added changelog file. --- changelog/5946.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/5946.bugfix.rst diff --git a/changelog/5946.bugfix.rst b/changelog/5946.bugfix.rst new file mode 100644 index 000000000..07bf23e44 --- /dev/null +++ b/changelog/5946.bugfix.rst @@ -0,0 +1 @@ +Fixed issue when parametrizing fixtures with numpy arrays (and possibly other sequence-like types). \ No newline at end of file From 63e3d89647499b7c28bde50391fa9313edb21c5d Mon Sep 17 00:00:00 2001 From: Victor Maryama Date: Sat, 12 Oct 2019 15:08:47 +0200 Subject: [PATCH 3/4] Fixed linting. --- changelog/5946.bugfix.rst | 2 +- testing/python/fixtures.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/changelog/5946.bugfix.rst b/changelog/5946.bugfix.rst index 07bf23e44..62f012ccd 100644 --- a/changelog/5946.bugfix.rst +++ b/changelog/5946.bugfix.rst @@ -1 +1 @@ -Fixed issue when parametrizing fixtures with numpy arrays (and possibly other sequence-like types). \ No newline at end of file +Fixed issue when parametrizing fixtures with numpy arrays (and possibly other sequence-like types). diff --git a/testing/python/fixtures.py b/testing/python/fixtures.py index e0b464c96..4fcab0245 100644 --- a/testing/python/fixtures.py +++ b/testing/python/fixtures.py @@ -4191,7 +4191,7 @@ def test_indirect_fixture_does_not_break_scope(testdir): def test_fixture_parametrization_nparray(testdir): testdir.makepyfile( - """ + """ from numpy import linspace from pytest import fixture From 122cf60b2789487efec2b725f429eaa59baf3ea2 Mon Sep 17 00:00:00 2001 From: Victor Maryama Date: Sat, 12 Oct 2019 15:46:28 +0200 Subject: [PATCH 4/4] Always creating list for consistency. Co-Authored-By: Bruno Oliveira --- src/_pytest/fixtures.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index ed7e715de..7dc919235 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -1113,7 +1113,7 @@ def fixture( ``fixture_`` and then use ``@pytest.fixture(name='')``. """ - if params is not None and not isinstance(params, (list, tuple)): + if params is not None: params = list(params) fixture_function, arguments = _parse_fixture_args(