Add explicit short-circuit in get_all_by_consumer_id

PyCharm complained of a potentially-unset local variable in
get_all_by_consumer_id, even though that wasn't actually possible given
the list comp would be iterating over zero values in that case. However,
adding an explicit short-circuit for this case makes the code clearer to
read, and gets rid of the IDE warning, so \o/.

An unrelated typo in a comment is fixed at the same time.

Change-Id: Ic7b5a223a8eab8f7dd651521b8723d89abf2ded1
This commit is contained in:
Eric Fried 2019-03-01 17:46:50 -06:00
parent 849c89d0e5
commit aa2c1442af
2 changed files with 17 additions and 15 deletions

View File

@ -522,20 +522,22 @@ def get_all_by_consumer_id(context, consumer_id):
_create_incomplete_consumer(context, consumer_id)
db_allocs = _get_allocations_by_consumer_uuid(context, consumer_id)
if db_allocs:
# Build up the Consumer object (it's the same for all allocations
# since we looked up by consumer ID)
db_first = db_allocs[0]
consumer = consumer_obj.Consumer(
context, id=db_first['consumer_id'],
uuid=db_first['consumer_uuid'],
generation=db_first['consumer_generation'],
project=project_obj.Project(
context, id=db_first['project_id'],
external_id=db_first['project_external_id']),
user=user_obj.User(
context, id=db_first['user_id'],
external_id=db_first['user_external_id']))
if not db_allocs:
return []
# Build up the Consumer object (it's the same for all allocations
# since we looked up by consumer ID)
db_first = db_allocs[0]
consumer = consumer_obj.Consumer(
context, id=db_first['consumer_id'],
uuid=db_first['consumer_uuid'],
generation=db_first['consumer_generation'],
project=project_obj.Project(
context, id=db_first['project_id'],
external_id=db_first['project_external_id']),
user=user_obj.User(
context, id=db_first['user_id'],
external_id=db_first['user_external_id']))
# Build up a list of Allocation objects, setting the Allocation object
# fields to the same-named database record field we got from

View File

@ -161,7 +161,7 @@ class TestAllocationListCreateDelete(tb.PlacementDbBaseTestCase):
# redo one allocation
# TODO(cdent): This does not currently behave as expected
# because a new allocataion is created, adding to the total
# because a new allocation is created, adding to the total
# used, not replacing.
rp1_used += 1
self.allocate_from_provider(