Merge "Data structs declared with constructor replaced with literal"

This commit is contained in:
Jenkins 2015-04-15 02:35:30 +00:00 committed by Gerrit Code Review
commit 066470f74a
19 changed files with 71 additions and 35 deletions

View File

@ -93,7 +93,7 @@ class SaharaCluster(base.Context):
@rutils.log_task_wrapper(LOG.info, _("Enter context: `Sahara Cluster`"))
def setup(self):
wait_dict = dict()
wait_dict = {}
for user, tenant_id in rutils.iterate_per_tenants(
self.context["users"]):
@ -136,7 +136,7 @@ class SaharaCluster(base.Context):
check_interval=CONF.benchmark.cluster_check_interval)
def update_clusters_dict(self, dct):
new_dct = dict()
new_dct = {}
for cluster, client in dct.items():
new_cl = client.clusters.get(cluster.id)
new_dct[new_cl] = client

View File

@ -96,7 +96,7 @@ class UserGenerator(base.Context):
def __init__(self, context):
super(UserGenerator, self).__init__(context)
self.context["users"] = []
self.context["tenants"] = dict()
self.context["tenants"] = {}
self.endpoint = self.context["admin"]["endpoint"]
# NOTE(boris-42): I think this is the best place for adding logic when
# we are using pre created users or temporary. So we
@ -150,7 +150,7 @@ class UserGenerator(base.Context):
# NOTE(msdubov): consume() will fill the tenants list in the closure.
broker.run(publish, consume, threads)
tenants_dict = dict()
tenants_dict = {}
for t in tenants:
tenants_dict[t["id"]] = t
@ -212,7 +212,7 @@ class UserGenerator(base.Context):
cache["client"].delete_project(tenant_id)
broker.run(publish, consume, threads)
self.context["tenants"] = dict()
self.context["tenants"] = {}
def _delete_users(self):
threads = self.config["resource_management_workers"]

View File

@ -520,8 +520,8 @@ class TaskCommands(object):
tasks = isinstance(tasks, list) and tasks or [tasks]
results = list()
processed_names = dict()
results = []
processed_names = {}
for task_file_or_uuid in tasks:
if os.path.exists(os.path.expanduser(task_file_or_uuid)):
with open(os.path.expanduser(task_file_or_uuid),

View File

@ -143,7 +143,7 @@ class TaskTestCase(unittest.TestCase):
rally = utils.Rally()
cfg = self._get_sample_task_config()
config = utils.TaskConfig(cfg)
task_uuids = list()
task_uuids = []
for i in range(3):
res = rally("task start --task %s" % config.filename)
for line in res.splitlines():
@ -158,7 +158,7 @@ class TaskTestCase(unittest.TestCase):
rally = utils.Rally()
cfg = self._get_sample_task_config()
config = utils.TaskConfig(cfg)
files = list()
files = []
for i in range(3):
rally("task start --task %s" % config.filename)
path = "/tmp/task_%d.json" % i
@ -241,7 +241,7 @@ class TaskTestCase(unittest.TestCase):
# Validate against a single task
res = rally("task start --task %s" % config.filename)
task_uuids = list()
task_uuids = []
for line in res.splitlines():
if "finished" in line:
task_uuids.append(line.split(" ")[1][:-1])

View File

@ -97,7 +97,7 @@ class Rally(object):
self.reports_root = os.environ.get("REPORTS_ROOT",
"rally-cli-output-files")
self._created_files = list()
self._created_files = []
self("deployment create --file %s --name MAIN" % DEPLOYMENT_FILE,
write_report=False)

View File

@ -24,3 +24,4 @@ Rally Specific Commandments
* [N340] - Ensure that we are importing always ``from rally import objects``
* [N341] - Ensure that we are importing oslo_xyz packages instead of deprecated oslo.xyz ones
* [N350] - Ensure that single quotes are not used
* [N351] - Ensure that data structs (i.e Lists and Dicts) are declared literally rather than using constructors

View File

@ -20,7 +20,7 @@ Guidelines for writing new hacking checks
- Keep the test method code in the source file ordered based
on the N3xx value.
- List the new rule in the top level HACKING.rst file
- Add test cases for each new rule to tests/test_hacking.py
- Add test cases for each new rule to tests/unit/test_hacking.py
"""
@ -46,6 +46,10 @@ re_assert_equal_in_end_with_true_or_false = re.compile(
r"assertEqual\((\w|[][.'\"])+( not)? in (\w|[][.'\", ])+, (True|False)\)")
re_assert_equal_in_start_with_true_or_false = re.compile(
r"assertEqual\((True|False), (\w|[][.'\"])+( not)? in (\w|[][.'\", ])+\)")
re_no_construct_dict = re.compile(
r"=\sdict\(\)")
re_no_construct_list = re.compile(
r"=\slist\(\)")
def skip_ignored_lines(func):
@ -333,6 +337,21 @@ def check_quotes(logical_line, filename):
yield (i, "N350 Remove Single quotes")
@skip_ignored_lines
def check_no_constructor_data_struct(logical_line, filename):
"""Check that data structs (lists, dicts) are declared using literals
N351
"""
match = re_no_construct_dict.search(logical_line)
if match:
yield (0, "N351 Remove dict() construct and use literal {}")
match = re_no_construct_list.search(logical_line)
if match:
yield (0, "N351 Remove list() construct and use literal []")
def factory(register):
register(check_assert_methods_from_mock)
register(check_import_of_logging)
@ -346,3 +365,4 @@ def factory(register):
register(check_no_direct_rally_objects_import)
register(check_no_oslo_deprecated_import)
register(check_quotes)
register(check_no_constructor_data_struct)

View File

@ -35,8 +35,8 @@ class SaharaClusterTestCase(test.TestCase):
self.users = self.tenants_num * self.users_per_tenant
self.task = mock.MagicMock()
self.tenants = dict()
self.users_key = list()
self.tenants = {}
self.users_key = []
for i in range(self.tenants_num):
self.tenants[str(i)] = {"id": str(i), "name": str(i),

View File

@ -31,8 +31,8 @@ class SaharaEDPTestCase(test.TestCase):
self.users = self.tenants_num * self.users_per_tenant
self.task = mock.MagicMock()
self.tenants = dict()
self.users_key = list()
self.tenants = {}
self.users_key = []
for i in range(self.tenants_num):
self.tenants[str(i)] = {"id": str(i), "name": str(i),

View File

@ -32,8 +32,8 @@ class SaharaImageTestCase(test.TestCase):
self.users = self.tenants_num * self.users_per_tenant
self.task = mock.MagicMock()
self.tenants = dict()
self.users_key = list()
self.tenants = {}
self.users_key = []
for i in range(self.tenants_num):
self.tenants[str(i)] = {"id": str(i), "name": str(i)}

View File

@ -29,7 +29,7 @@ SCN = "rally.benchmark.scenarios"
class ImageGeneratorTestCase(test.TestCase):
def _gen_tenants(self, count):
tenants = dict()
tenants = {}
for id_ in range(count):
tenants[str(id_)] = dict(name=str(id_))
return tenants
@ -78,7 +78,7 @@ class ImageGeneratorTestCase(test.TestCase):
mock_osclients.Clients.return_value = fc
tenants = self._gen_tenants(tenants_count)
users = list()
users = []
for id in tenants:
for i in range(users_per_tenant):
users.append({"id": i, "tenant_id": id,
@ -128,7 +128,7 @@ class ImageGeneratorTestCase(test.TestCase):
images_per_tenant = 5
tenants = self._gen_tenants(tenants_count)
users = list()
users = []
for id_ in tenants:
for i in range(users_per_tenant):
users.append({"id": i, "tenant_id": id_,

View File

@ -29,7 +29,7 @@ TYP = "rally.benchmark.types"
class ServerGeneratorTestCase(test.TestCase):
def _gen_tenants(self, count):
tenants = dict()
tenants = {}
for id in range(count):
tenants[str(id)] = dict(name=str(id))
return tenants
@ -70,7 +70,7 @@ class ServerGeneratorTestCase(test.TestCase):
servers_per_tenant = 5
tenants = self._gen_tenants(tenants_count)
users = list()
users = []
for id in tenants.keys():
for i in range(users_per_tenant):
users.append({"id": i, "tenant_id": id,
@ -120,7 +120,7 @@ class ServerGeneratorTestCase(test.TestCase):
servers_per_tenant = 5
tenants = self._gen_tenants(tenants_count)
users = list()
users = []
for id in tenants.keys():
for i in range(users_per_tenant):
users.append({"id": i, "tenant_id": id,

View File

@ -26,7 +26,7 @@ SCN = "rally.benchmark.scenarios"
class TestStackGenerator(test.TestCase):
def _gen_tenants(self, count):
tenants = dict()
tenants = {}
for id in range(count):
tenants[str(id)] = dict(name=str(id))
return tenants
@ -57,7 +57,7 @@ class TestStackGenerator(test.TestCase):
mock_osclients.Clients.return_value = fc
tenants = self._gen_tenants(tenants_count)
users = list()
users = []
for ten_id in tenants:
for i in range(users_per_tenant):
users.append({"id": i, "tenant_id": ten_id,
@ -98,4 +98,4 @@ class TestStackGenerator(test.TestCase):
stack_ctx = stacks.StackGenerator(context)
stack_ctx.cleanup()
mock_cleanup.assert_called_once_with(names=["heat.stacks"],
users=context["users"])
users=context["users"])

View File

@ -28,7 +28,7 @@ SCN = "rally.benchmark.scenarios"
class VolumeGeneratorTestCase(test.TestCase):
def _gen_tenants(self, count):
tenants = dict()
tenants = {}
for id in range(count):
tenants[str(id)] = dict(name=str(id))
return tenants
@ -58,7 +58,7 @@ class VolumeGeneratorTestCase(test.TestCase):
volumes_per_tenant = 5
tenants = self._gen_tenants(tenants_count)
users = list()
users = []
for id in tenants.keys():
for i in range(users_per_tenant):
users.append({"id": i, "tenant_id": id,
@ -103,7 +103,7 @@ class VolumeGeneratorTestCase(test.TestCase):
volumes_per_tenant = 5
tenants = self._gen_tenants(tenants_count)
users = list()
users = []
for id in tenants.keys():
for i in range(users_per_tenant):
users.append({"id": i, "tenant_id": id,

View File

@ -51,8 +51,8 @@ class ScenarioHelpersTestCase(test.TestCase):
@mock.patch(BASE + "random.choice", side_effect=lambda x: x[1])
def test_get_scenario_context(self, mock_random):
users = list()
tenants = dict()
users = []
tenants = {}
for i in range(2):
tenants[str(i)] = dict(name=str(i))

View File

@ -387,7 +387,7 @@ class TaskCommandsTestCase(test.TestCase):
"load_duration": 2.1,
"full_duration": 2.2}}]
results = list()
results = []
for task_uuid in tasks:
results.extend(
map(lambda x: {"key": x["key"],

View File

@ -338,7 +338,7 @@ class EditDistanceTestCase(test.TestCase):
class TenantIteratorTestCase(test.TestCase):
def test_iterate_per_tenant(self):
users = list()
users = []
tenants_count = 2
users_per_tenant = 5
for tenant_id in range(tenants_count):

View File

@ -245,3 +245,18 @@ class HackingTestCase(test.TestCase):
"a = '' # noqa "
]
self._assert_good_samples(checks.check_quotes, good_lines)
def test_check_no_constructor_data_struct(self):
bad_struct = [
"= dict()",
"= list()"
]
self._assert_bad_samples(checks.check_no_constructor_data_struct,
bad_struct)
good_struct = [
"= []",
"= {}",
]
self._assert_good_samples(checks.check_no_constructor_data_struct,
good_struct)

View File

@ -52,7 +52,7 @@ class LogTestCase(test.TestCase):
name = "fake"
vers = "fake"
mock_oslogger._loggers = dict()
mock_oslogger._loggers = {}
returned_logger = log.getLogger(name, vers)