[Python-Dev] How far to go with user-friendliness

Florian Bruhin me at the-compiler.org
Tue Jul 14 15:51:42 CEST 2015


* Steven D'Aprano <steve at pearwood.info> [2015-07-14 23:41:56 +1000]:
> On Tue, Jul 14, 2015 at 02:06:14PM +0200, Dima Tisnek wrote:
> > https://bugs.python.org/issue21238 introduces detection of
> > missing/misspelt mock.assert_xxx() calls on getattr level in Python
> > 3.5
> > 
> > Michael and Kushal are of the opinion that "assret" is a common typo
> > of "assert" and should be supported in a sense that it also triggers
> > AttributeError and is not silently ignored like a mocked user
> > attribute.
> > 
> > I disagree
> 
> I must admit I don't use mock so don't quite understand what is going on 
> in this bug report.

Without using spec/autospec, a mock (by design) supports calling any
method on itself, which returns another mock:

    >>> m = mock.Mock()
    >>> m.eggs()
    <Mock name='mock.eggs()' id='140373912833384'>
    >>> m.bacon()
    <Mock name='mock.bacon()' id='140373910240616'>

However, it also has some special methods to see if it has been
called:

    >>> m.assert_called_with()
    [...]
    AssertionError: Expected call: mock()
    Not called

Now because of that, if you do a typo, you won't notice in a test:

    >>> m.assert_caled_with()
    <Mock name='mock.assert_caled_with()' id='140373910240672'>

With the patch, an AttributeError is raised if you call something
starting with assert or assret instead.

Florian

-- 
http://www.the-compiler.org | me at the-compiler.org (Mail/XMPP)
   GPG: 916E B0C8 FD55 A072 | http://the-compiler.org/pubkey.asc
         I love long mails! | http://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150714/13872b7a/attachment.sig>


More information about the Python-Dev mailing list