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

assert has strange behavior with numpy arrays #10

Closed
pytestbot opened this issue Dec 5, 2010 · 1 comment
Closed

assert has strange behavior with numpy arrays #10

pytestbot opened this issue Dec 5, 2010 · 1 comment
Labels
type: bug problem that needs to be addressed

Comments

@pytestbot
Copy link
Contributor

Originally reported by: Anonymous


import numpy as np

def test_array():
assert not(any(np.array([0, -1]) < 0))

I have this file as test3.py
If I run this with
py.test test3.py

I get the following output
$ py.test test3.py
============================= test session starts ==============================
platform linux2 -- Python 2.6.6 -- pytest-2.0.0
collected 1 items

test3.py F

=================================== FAILURES ===================================
__________________________________ test_array __________________________________

self = AssertionError()

def __init__(self, *args):
    BuiltinAssertionError.__init__(self, *args)
    if args:
        try:
            self.msg = str(args[0])
        except py.builtin._sysex:
            raise
        except:
            self.msg = "<[broken __repr__] %s at %0xd>" %(
                args[0].__class__, id(args[0]))
    else:
        f = py.code.Frame(sys._getframe(1))
        try:
            source = f.code.fullsource
            if source is not None:
                try:
                    source = source.getstatement(f.lineno, assertion=True)
                except IndexError:
                    source = None
                else:
                    source = str(source.deindent()).strip()
        except py.error.ENOENT:
            source = None
            # this can also occur during reinterpretation, when the
            # co_filename is set to "<run>".
        if source:
          self.msg = reinterpret(source, f, should_fail=True)

/usr/local/lib/python2.6/dist-packages/py/_code/assertion.py:79:


source = 'assert not(any(np.array([0, -1]) < 0))'
frame = <py._code.code.Frame object at 0x96208cc>, should_fail = True

def interpret(source, frame, should_fail=False):
    mod = ast.parse(source)
    visitor = DebugInterpreter(frame)
    try:
      visitor.visit(mod)

/usr/local/lib/python2.6/dist-packages/py/_code/_assertionnew.py:48:


self = <py._code._assertionnew.DebugInterpreter object at 0x9678a4c>
node = <_ast.Module object at 0x96788ac>

def visit(self, node):
    """Visit a node."""
    method = 'visit_' + node.__class__.__name__
    visitor = getattr(self, method, self.generic_visit)
  return visitor(node)

/usr/lib/python2.6/ast.py:231:


self = <py._code._assertionnew.DebugInterpreter object at 0x9678a4c>
mod = <_ast.Module object at 0x96788ac>

def visit_Module(self, mod):
    for stmt in mod.body:
      self.visit(stmt)

/usr/local/lib/python2.6/dist-packages/py/_code/_assertionnew.py:146:


self = <py._code._assertionnew.DebugInterpreter object at 0x9678a4c>
node = <_ast.Assert object at 0x967872c>

def visit(self, node):
    """Visit a node."""
    method = 'visit_' + node.__class__.__name__
    visitor = getattr(self, method, self.generic_visit)
  return visitor(node)

/usr/lib/python2.6/ast.py:231:


self = <py._code._assertionnew.DebugInterpreter object at 0x9678a4c>
assrt = <_ast.Assert object at 0x967872c>

def visit_Assert(self, assrt):
  test_explanation, test_result = self.visit(assrt.test)

/usr/local/lib/python2.6/dist-packages/py/_code/_assertionnew.py:319:


self = <py._code._assertionnew.DebugInterpreter object at 0x9678a4c>
node = <_ast.UnaryOp object at 0x96785ac>

def visit(self, node):
    """Visit a node."""
    method = 'visit_' + node.__class__.__name__
    visitor = getattr(self, method, self.generic_visit)
  return visitor(node)

/usr/lib/python2.6/ast.py:231:


self = <py._code._assertionnew.DebugInterpreter object at 0x9678a4c>
unary = <_ast.UnaryOp object at 0x96785ac>

def visit_UnaryOp(self, unary):
    pattern = unary_map[unary.op.__class__]
  operand_explanation, operand_result = self.visit(unary.operand)

/usr/local/lib/python2.6/dist-packages/py/_code/_assertionnew.py:202:


self = <py._code._assertionnew.DebugInterpreter object at 0x9678a4c>
node = <_ast.Call object at 0x96789cc>

def visit(self, node):
    """Visit a node."""
    method = 'visit_' + node.__class__.__name__
    visitor = getattr(self, method, self.generic_visit)
  return visitor(node)

/usr/lib/python2.6/ast.py:231:


self = <py._code._assertionnew.DebugInterpreter object at 0x9678a4c>
call = <_ast.Call object at 0x96789cc>

def visit_Call(self, call):
    func_explanation, func = self.visit(call.func)
    arg_explanations = []
    ns = {"__exprinfo_func" : func}
    arguments = []
    for arg in call.args:
      arg_explanation, arg_result = self.visit(arg)

/usr/local/lib/python2.6/dist-packages/py/_code/_assertionnew.py:232:


self = <py._code._assertionnew.DebugInterpreter object at 0x9678a4c>
node = <_ast.Compare object at 0x9678a0c>

def visit(self, node):
    """Visit a node."""
    method = 'visit_' + node.__class__.__name__
    visitor = getattr(self, method, self.generic_visit)
  return visitor(node)

/usr/lib/python2.6/ast.py:231:


self = <py._code._assertionnew.DebugInterpreter object at 0x9678a4c>
comp = <_ast.Compare object at 0x9678a0c>

def visit_Compare(self, comp):
    left = comp.left
    left_explanation, left_result = self.visit(left)
    for op, next_op in zip(comp.ops, comp.comparators):
        next_explanation, next_result = self.visit(next_op)
        op_symbol = operator_map[op.__class__]
        explanation = "%s %s %s" % (left_explanation, op_symbol,
                                    next_explanation)
        source = "__exprinfo_left %s __exprinfo_right" % (op_symbol,)
        co = self._compile(source)
        try:
            result = self.frame.eval(co, __exprinfo_left=left_result,
                                     __exprinfo_right=next_result)
        except Exception:
            raise Failure(explanation)
      if not result:

E ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

/usr/local/lib/python2.6/dist-packages/py/_code/_assertionnew.py:177: ValueError
=========================== 1 failed in 0.23 seconds ===========================
$


@pytestbot
Copy link
Contributor Author

Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


fix issue10 - numpy arrays should now work better in assertion expressions
(or any other objects which have an exception-raising nonzero method ...)

@pytestbot pytestbot added the type: bug problem that needs to be addressed label Jun 15, 2015
mgorny pushed a commit to mgorny/pytest that referenced this issue May 27, 2023
I've bumped the version of distribute from 0.6.13 to 0.6.35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

1 participant