On Monday 15 March 2010 12:50:13 Colin Morris wrote:
> If it were possible to check a little more carefully what's being
> asserted, then we could give a very specific message. As I understand
> it, we're warning against the case where someone is trying to assert
> some boolean expression along with a message to pass to the
> AssertionError's constructor ("assert x, msg"), but instead muddles the
> syntax and asserts a tuple ("assert (x, msg)"). If we could check that
> what's being asserted is a size 2 tuple literal, then we could give a
> more specific message like: "Assertion will always be true. Did you
> mean 'assert x, msg'?".

here we just have to test len(node.elts) == 2

> Also, that could avoid some false positives 
> since, I imagine, there are legitimate situations where you might want
> to assert some variable that holds a tuple of undetermined size (though
> 'assert len(sometuple) is not 0' might be more clear). I don't know if
> this is possible though--can the ASTNG tell whether a tuple is a
> literal or not?

I think, here is (at least) a misunderstanding : we will trigger our 
message on a *astng* Tuple node, as in Fletcher Johnson's patch :

+        if isinstance(node.test, astng.Tuple):

and we always know all elements of a Tuple node;

not on a Name node that could possibly be inferred as be a *python* tuple.
But: 

>>> from logilab.astng import builder, nodes
>>> bb = builder.ASTNGBuilder()
>>> tree = bb.string_build("""
... a = tuple(b for b in iterbl)
... """)
>>> list(tree['a'].infer())
[<Instance of __builtin__.tuple at 0x24025104>]
>>> inst = list(tree['a'].infer())[0]
>>> isinstance(inst, nodes.Tuple)
False


> Without those checks, we can't really be sure that the assertion will
> always be true, so the warning message would have to be a bit more
> noncommittal. Maybe something like:
>
>  "Found assertion of tuple. Did you mean to do 'assert a, msg'?"
> or
> "Assertion of a non-empty tuple will always pass. Did you mean 'assert
> a, msg'?"

So, there should be two different messages depending on 
len(node.test.elts) == 2 or not

-- 

Emile Anclin <[email protected]>
http://www.logilab.fr/   http://www.logilab.org/ 
Informatique scientifique & et gestion de connaissances
_______________________________________________
Python-Projects mailing list
[email protected]
http://lists.logilab.org/mailman/listinfo/python-projects

Reply via email to