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

Fixture parametrization breaks if parameter is a numpy array #5946

Closed
3 of 4 tasks
Sup3rGeo opened this issue Oct 11, 2019 · 2 comments · Fixed by #5950
Closed
3 of 4 tasks

Fixture parametrization breaks if parameter is a numpy array #5946

Sup3rGeo opened this issue Oct 11, 2019 · 2 comments · Fixed by #5950
Labels
topic: fixtures anything involving fixtures directly or indirectly topic: parametrize related to @pytest.mark.parametrize type: bug problem that needs to be addressed

Comments

@Sup3rGeo
Copy link
Member

Sup3rGeo commented Oct 11, 2019

Description

On latest release and also on master branch, windows 10

The following test:

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

breaks with:

Traceback (most recent call last):
  File "D:/WORK/pytest-nparray-params-bug/test_bug.py", line 4, in <module>
    @fixture(params=linspace(1, 10, 10))
  File "D:\WORK\pytest-nparray-params-bug\venv\lib\site-packages\_pytest\fixtures.py", line 1123, in fixture
    name=name
  File "D:\WORK\pytest-nparray-params-bug\venv\lib\site-packages\_pytest\fixtures.py", line 1032, in _parse_fixture_args
    key: value for key, value in kwargs.items() if arguments.get(key) != value
  File "D:\WORK\pytest-nparray-params-bug\venv\lib\site-packages\_pytest\fixtures.py", line 1032, in <dictcomp>
    key: value for key, value in kwargs.items() if arguments.get(key) != value
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Cause

It seems that this line:

pytest/src/_pytest/fixtures.py

Lines 1031 to 1033 in b162ab6

kwargs = {
key: value for key, value in kwargs.items() if arguments.get(key) != value
}

Tries to get a boolean result from comparing None != <array> which is ambiguous.

Possible solution

This might be solved by moving the cast to list (line 1137) at the very beginning (1116) of this function:

pytest/src/_pytest/fixtures.py

Lines 1116 to 1139 in b162ab6

fixture_function, arguments = _parse_fixture_args(
callable_or_scope,
*args,
scope=scope,
params=params,
autouse=autouse,
ids=ids,
name=name
)
scope = arguments.get("scope")
params = arguments.get("params")
autouse = arguments.get("autouse")
ids = arguments.get("ids")
name = arguments.get("name")
if fixture_function and params is None and autouse is False:
# direct decoration
return FixtureFunctionMarker(scope, params, autouse, name=name)(
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)

I am actually running tests with this change but not 100% certain about side-effects, although it seems to be that casting to list that soon should not cause problems (equivalent of casting to list in user test code).

If you think this is okay I can create the PR.

  • a detailed description of the bug or suggestion
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible
@nicoddemus
Copy link
Member

Hi @Sup3rGeo,

Thanks a lot for the report and detailed proposed solution.

I don't think this will have any side-effects, please open a PR! Thanks!

@Zac-HD Zac-HD added topic: fixtures anything involving fixtures directly or indirectly topic: parametrize related to @pytest.mark.parametrize type: bug problem that needs to be addressed labels Oct 12, 2019
@Zac-HD
Copy link
Member

Zac-HD commented Oct 12, 2019

FYI @Sup3rGeo you can get syntax highlighting for code blocks on GitHub, relevantly for python and python-traceback (and pycon for a REPL session) 😄

Thanks for reporting this, and for suggesting a fix!

bors bot added a commit to duckinator/bork that referenced this issue Oct 25, 2019
68: Update pytest to 5.2.2 r=duckinator a=pyup-bot


This PR updates [pytest](https://pypi.org/project/pytest) from **5.2.1** to **5.2.2**.



<details>
  <summary>Changelog</summary>
  
  
   ### 5.2.2
   ```
   =========================

Bug Fixes
---------

- `5206 &lt;https://github.com/pytest-dev/pytest/issues/5206&gt;`_: Fix ``--nf`` to not forget about known nodeids with partial test selection.


- `5906 &lt;https://github.com/pytest-dev/pytest/issues/5906&gt;`_: Fix crash with ``KeyboardInterrupt`` during ``--setup-show``.


- `5946 &lt;https://github.com/pytest-dev/pytest/issues/5946&gt;`_: Fixed issue when parametrizing fixtures with numpy arrays (and possibly other sequence-like types).


- `6044 &lt;https://github.com/pytest-dev/pytest/issues/6044&gt;`_: Properly ignore ``FileNotFoundError`` exceptions when trying to remove old temporary directories,
  for instance when multiple processes try to remove the same directory (common with ``pytest-xdist``
  for example).
   ```
   
  
</details>


 

<details>
  <summary>Links</summary>
  
  - PyPI: https://pypi.org/project/pytest
  - Changelog: https://pyup.io/changelogs/pytest/
  - Homepage: https://docs.pytest.org/en/latest/
</details>



Co-authored-by: pyup-bot <github-bot@pyup.io>
bors bot added a commit to rehandalal/therapist that referenced this issue Oct 29, 2019
98: Update pytest to 5.2.2 r=rehandalal a=pyup-bot


This PR updates [pytest](https://pypi.org/project/pytest) from **5.2.1** to **5.2.2**.



<details>
  <summary>Changelog</summary>
  
  
   ### 5.2.2
   ```
   =========================

Bug Fixes
---------

- `5206 &lt;https://github.com/pytest-dev/pytest/issues/5206&gt;`_: Fix ``--nf`` to not forget about known nodeids with partial test selection.


- `5906 &lt;https://github.com/pytest-dev/pytest/issues/5906&gt;`_: Fix crash with ``KeyboardInterrupt`` during ``--setup-show``.


- `5946 &lt;https://github.com/pytest-dev/pytest/issues/5946&gt;`_: Fixed issue when parametrizing fixtures with numpy arrays (and possibly other sequence-like types).


- `6044 &lt;https://github.com/pytest-dev/pytest/issues/6044&gt;`_: Properly ignore ``FileNotFoundError`` exceptions when trying to remove old temporary directories,
  for instance when multiple processes try to remove the same directory (common with ``pytest-xdist``
  for example).
   ```
   
  
</details>


 

<details>
  <summary>Links</summary>
  
  - PyPI: https://pypi.org/project/pytest
  - Changelog: https://pyup.io/changelogs/pytest/
  - Homepage: https://docs.pytest.org/en/latest/
</details>



Co-authored-by: pyup-bot <github-bot@pyup.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: fixtures anything involving fixtures directly or indirectly topic: parametrize related to @pytest.mark.parametrize type: bug problem that needs to be addressed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants