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

pytest.approx doesn't support comparison between arrays and scalars #3312

Closed
tadeu opened this issue Mar 14, 2018 · 3 comments
Closed

pytest.approx doesn't support comparison between arrays and scalars #3312

tadeu opened this issue Mar 14, 2018 · 3 comments
Labels
status: help wanted developers would like help from experts on this topic type: enhancement new feature or API change, should be merged into features branch type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@tadeu
Copy link
Member

tadeu commented Mar 14, 2018

Sometimes we want to assert that all elements from an array are approximately equal to a single value, so this code comes naturally to mind:

assert some_array == approx(1.0)

the problem is that pytest.approx doesn't support this (the workaround is to create a second array with the same shape and filled with the scalar).

Here is a test case:

from pytest import approx
import numpy as np

def test_approx_array_scalar():
    assert np.array([1.0, 1.0]) == approx(1.0)

def test_approx_scalar_array():
    assert 1.0 == approx(np.array([1.0, 1.0]))

here are the outputs:

______________________________________________ test_approx_array_scalar _______________________________________________
                                                                                                                       
    def test_approx_array_scalar():                                                                                    
>       assert np.array([1.0, 1.0]) == approx(1.0)                                                                     
E       ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()        
                                                                                                                       
test_approx.py:5: ValueError                                                                                           
______________________________________________ test_approx_scalar_array _______________________________________________
                                                                                                                       
    def test_approx_scalar_array():                                                                                    
>       assert 1.0 == approx(np.array([1.0, 1.0]))                                                                     
E       assert 1.0 == approx([1.0 ± 1.0e-06, 1.0 ± 1.0e-06])                                                           
E        +  where approx([1.0 ± 1.0e-06, 1.0 ± 1.0e-06]) = approx(array([ 1.,  1.]))                                   
E        +    where array([ 1.,  1.]) = <built-in function array>([1.0, 1.0])                                          
E        +      where <built-in function array> = np.array                                                             
                                                                                                                       
test_approx.py:8: AssertionError                                                                                       

This is pip list, including pytest version:

certifi (2016.2.28)
colorama (0.3.9)   
numpy (1.13.1)     
pip (9.0.1)        
py (1.4.34)        
pytest (3.2.1)     
setuptools (36.4.0)
wheel (0.29.0)     
wincertstore (0.2) 
@pytestbot pytestbot added status: help wanted developers would like help from experts on this topic type: refactoring internal improvements to the code labels Mar 14, 2018
@pytestbot
Copy link
Contributor

GitMate.io thinks possibly related issues are #1994 (Supporting arrays for approx), #1123 (does pytest support to this way to collect test cases?), #2043 (Pytest does not support indirect parameters for overriden fixtures), #2796 (Possible bug in pytest --doctest comparison), and #2196 (Pytest on cygwin doesn't find files).

@pytestbot pytestbot added the type: bug problem that needs to be addressed label Mar 14, 2018
@nicoddemus nicoddemus added type: enhancement new feature or API change, should be merged into features branch type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature and removed type: bug problem that needs to be addressed type: refactoring internal improvements to the code labels Mar 14, 2018
@nicoddemus
Copy link
Member

Thanks @tadeu!

@benjaminp
Copy link
Contributor

closing since the pull request was merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted developers would like help from experts on this topic type: enhancement new feature or API change, should be merged into features branch type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

4 participants