keystone.tests.unit.ksfixtures.hacking.
HackingCode
[source]¶Bases: fixtures.fixture.Fixture
A fixture to house the various code examples for the keystone hacking style checks.
asserting_none_equality
= {‘expected_errors’: [(5, 8, ‘K003’), (6, 8, ‘K003’), (7, 8, ‘K004’), (8, 8, ‘K004’)], ‘code’: “\n class Test(object):\n\n def test(self):\n self.assertEqual(”, ”)\n self.assertEqual(”, None)\n self.assertEqual(None, ”)\n self.assertNotEqual(”, None)\n self.assertNotEqual(None, ”)\n self.assertNotEqual(”, None) # noqa\n self.assertNotEqual(None, ”) # noqa\n “}¶comments_begin_with_space
= {‘expected_errors’: [(3, 0, ‘K002’)], ‘code’: “\n # This is a good comment\n\n #This is a bad one\n\n # This is alright and can\n # be continued with extra indentation\n # if that’s what the developer wants.\n “}¶dict_constructor
= {‘expected_errors’: [(3, 0, ‘K008’), (4, 0, ‘K008’), (5, 0, ‘K008’)], ‘code’: “\n lower_res = {k.lower(): v for k, v in res[1].items()}\n fool = dict(a=’a’, b=’b’)\n lower_res = dict((k.lower(), v) for k, v in res[1].items())\n attrs = dict([(k, _from_json(v))])\n dict([[i,i] for i in range(3)])\n dict(({1:2}))\n “}¶mutable_default_args
= {‘expected_errors’: [(7, 10, ‘K001’), (10, 15, ‘K001’), (10, 29, ‘K001’), (13, 15, ‘K001’), (16, 15, ‘K001’), (16, 31, ‘K001’), (22, 14, ‘K001’), (22, 31, ‘K001’), (22, 53, ‘K001’), (25, 14, ‘K001’), (25, 36, ‘K001’), (28, 10, ‘K001’), (28, 27, ‘K001’), (29, 21, ‘K001’), (32, 11, ‘K001’)], ‘code’: ‘\n def f():\n pass\n\n def f(a, b='', c=None):\n pass\n\n def f(bad=[]):\n pass\n\n def f(foo, bad=[], more_bad=[x for x in range(3)]):\n pass\n\n def f(foo, bad={}):\n pass\n\n def f(foo, bad={}, another_bad=[], fine=None):\n pass\n\n def f(bad=[]): # noqa\n pass\n\n def funcs(bad=dict(), more_bad=list(), even_more_bad=set()):\n “creating mutables through builtins”\n\n def funcs(bad=something(), more_bad=some_object.something()):\n “defaults from any functions”\n\n def f(bad=set(), more_bad={x for x in range(3)},\n even_more_bad={1, 2, 3}):\n “set and set comprehession”\n\n def f(bad={x: x for x in range(3)}):\n “dict comprehension”\n ‘}¶keystone.tests.unit.ksfixtures.hacking.
HackingTranslations
[source]¶Bases: fixtures.fixture.Fixture
Fixtures for checking translation rules.
examples
= [{‘expected_errors’: [(3, 9, ‘K005’), (6, 20, ‘K005’)], ‘code’: “\n # stdlib logging\n LOG = logging.getLogger()\n LOG.info(_(‘text’))\n class C:\n def __init__(self):\n LOG.warning(oslo_i18n(‘text’, {}))\n “}, {‘expected_errors’: [(7, 12, ‘K005’)], ‘code’: “\n # stdlib logging w/ alias and specifying a logger\n class C:\n def __init__(self):\n self.L = logging.getLogger(__name__)\n def m(self):\n self.L.warning(\n _(‘text’), {}\n )\n “}, {‘expected_errors’: [(3, 8, ‘K005’)], ‘code’: “\n # oslo logging and specifying a logger\n L = log.getLogger(__name__)\n L.error(oslo_i18n(‘text’))\n “}, {‘expected_errors’: [(5, 26, ‘K005’)], ‘code’: “\n # oslo logging w/ alias\n class C:\n def __init__(self):\n self.LOG = oslo_logging.getLogger()\n self.LOG.critical(_(‘text’))\n “}, {‘expected_errors’: [(4, 14, ‘K005’)], ‘code’: “\n LOG = log.getLogger(__name__)\n # translation on a separate line\n msg = _(‘text’)\n LOG.exception(msg)\n “}, {‘expected_errors’: [], ‘code’: “\n # this should not be an error\n L = log.getLogger(__name__)\n msg = _(‘text’)\n L.warning(msg)\n raise Exception(msg)\n “}, {‘expected_errors’: [], ‘code’: “\n L = log.getLogger(__name__)\n def f():\n msg = _(‘text’)\n L2.warning(msg)\n something = True # add an extra statement here\n raise Exception(msg)\n “}, {‘expected_errors’: [(4, 16, ‘K005’)], ‘code’: “\n LOG = log.getLogger(__name__)\n def func():\n msg = _(‘text’)\n LOG.warning(msg)\n raise Exception(‘some other message’)\n “}, {‘expected_errors’: [], ‘code’: “\n LOG = log.getLogger(__name__)\n if True:\n msg = _(‘text’)\n else:\n msg = _(‘text’)\n LOG.warning(msg)\n raise Exception(msg)\n “}, {‘expected_errors’: [(6, 12, ‘K005’)], ‘code’: “\n LOG = log.getLogger(__name__)\n if True:\n msg = _(‘text’)\n else:\n msg = _(‘text’)\n LOG.warning(msg)\n “}, {‘expected_errors’: [], ‘code’: “\n LOG = log.getLogger(__name__)\n msg = _LW(‘text’)\n LOG.warning(msg)\n msg = _(‘something else’)\n raise Exception(msg)\n “}, {‘expected_errors’: [(3, 12, ‘K005’)], ‘code’: “\n LOG = log.getLogger(__name__)\n msg = _(‘hello %s’) % ‘world’\n LOG.warning(msg)\n “}, {‘expected_errors’: [], ‘code’: ‘\n # this should not be an error\n LOG = log.getLogger(__name__)\n try:\n something = True\n except AssertionError as e:\n LOG.warning(six.text_type(e))\n raise exception.Unauthorized(e)\n ‘}, {‘expected_errors’: [], ‘code’: “\n # this should not be an error\n LOG = log.getLogger(__name__)\n try:\n pass\n except AssertionError as e:\n msg = _(‘some message’)\n LOG.warning(msg)\n raise exception.Unauthorized(message=msg)\n “}]¶Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.