Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What misspellings / typos are supported in Python?

What misspellings / typos are supported in Python?

Not alternate spellings such as is_dir vs isdir, nor color vs colour but actual wrongly spelt aliases, such as proprety for property (which isn't supported).

like image 246
Ethan Furman Avatar asked Jul 21 '15 18:07

Ethan Furman


1 Answers

As of Python 3.5 beta 3 the unittest.mock object now supports assret standing in for assert -- note that this is not the keyword assert, but any attribute of a mock object that matches the regular expression assert.* or assret.*.

Some explanation:

When a mock object is created the default for any attribute access is to return a new Mock, except in one case: if the attribute is one of assert_called_with, assert_called_once_with, assert_any_call, assert_has_calls, and assert_not_called, in which case some code is actually run.

The problem is if one forgets the exact name and uses, for example, assert_called, then instead of code running to check that the mock was called, a new mock is returned instead and the test one wrote passes instead of actually doing the test and possibly failing.

To combat this problem Mock now raises an AttributeError if any access is made to an attribute that starts with assert.

Besides assert, Mock will also raise an AttributeError if any access is made to an attribute that starts with assret.

If one does not want the extra protection (for assert and assret) one can use unsafe=True when creating the Mock.

like image 86
Ethan Furman Avatar answered Nov 14 '22 23:11

Ethan Furman