我试图在单元测试设置期间对由对象和(相同类型)对象列表组成的列表进行排序,但我得到的是TypeError: can't compare datetime.date to NoneType
。
我知道它非常简单,但我错过了什么!?
objs = [myobject, myobject]
more_objs = [[myobject, myobject], [myobject, myobject]]
def sort_all_objs(data):
"""
Sort mixed list by the dates.
"""
if isinstance(data, list):
sort_all_objs(data[0])
else:
date = getattr(data.calculation, 'duedate')
if date:
return date
raise AttributeError("List doesn't have objects with a duedate attribute: {0}".format(data.calculation))
all_objs = objs + more_objs
all_objs = sorted(self.all_objs, key=sort_all_objs)
答案 0 :(得分:1)
在sort_all_objs
函数中:
if isinstance(data, list):
sort_all_objs(data[0]) # recursively call the function
# no return here
else:
# else branch returns a date object
函数本身没有返回值,因此为子列表返回None
。
您可能希望:
return sort_all_objs(data[0])