Python 属性错误:Can';t设置属性-如何修复它?
我正在使用pytest开发测试,但是我遇到了AttributeError:无法设置属性Python 属性错误:Can';t设置属性-如何修复它?,python,ponyorm,Python,Ponyorm,我正在使用pytest开发测试,但是我遇到了AttributeError:无法设置属性 class TestCalculator: @db_session def test_calc_monthly_income(self): user = User.get(email='test@test.com') user.pretax_income = 12000 assert user.monthly_income == Decimal('1000') user.pr
class TestCalculator:
@db_session
def test_calc_monthly_income(self):
user = User.get(email='test@test.com')
user.pretax_income = 12000
assert user.monthly_income == Decimal('1000')
user.pretax_income = 48000
assert user.monthly_income== Decimal('4000')
@db_session
def test_calc_post_tax_income(self):
user = User.get(email='test@test.com')
user.pretax_income = 120000
user.tax_rate = 0.1
assert user.post_tax_income == 108000
user.pretax_income = 90000
user.tax_rate = 0.2
assert user.post_tax_income == 72000
@db_session
def test_calc_post_tax_income_monthly(self):
user = User.get(email='test@test.com')
user.post_tax_income = 36000
assert user.post_tax_income_monthly == Decimal('3000')
user.post_tax_income = 60000
assert user.post_tax_income_monthly == Decimal('5000')
这就是模型:
class User(UserMixin, db.Entity):
_table_ = 'users'
email = Required(str, unique=True)
username = Required(str, unique=True)
password_hash = Optional(str)
pretax_income = Optional(Decimal, default=110000)
tax_rate = Optional(Decimal, default=0.1)
@property
@db_session
def monthly_income(self):
return round(self.pretax_income/12, 2)
@property
@db_session
def post_tax_income(self):
return round(self.pretax_income - (self.pretax_income * self.tax_rate), 2)
@property
@db_session
def post_tax_income_monthly(self):
return round(self.post_tax_income / 12, 2)
这就是我做测试的地方,前两个测试很好。最后一个测试每月税后收入的测试将获得有关设置属性的错误
class TestCalculator:
@db_session
def test_calc_monthly_income(self):
user = User.get(email='test@test.com')
user.pretax_income = 12000
assert user.monthly_income == Decimal('1000')
user.pretax_income = 48000
assert user.monthly_income== Decimal('4000')
@db_session
def test_calc_post_tax_income(self):
user = User.get(email='test@test.com')
user.pretax_income = 120000
user.tax_rate = 0.1
assert user.post_tax_income == 108000
user.pretax_income = 90000
user.tax_rate = 0.2
assert user.post_tax_income == 72000
@db_session
def test_calc_post_tax_income_monthly(self):
user = User.get(email='test@test.com')
user.post_tax_income = 36000
assert user.post_tax_income_monthly == Decimal('3000')
user.post_tax_income = 60000
assert user.post_tax_income_monthly == Decimal('5000')
更新:我试图以这种方式添加setter。显然,我做错了
@post_tax_income.setter
@db_session
def set_post_tax_income(self):
self.post_tax_income = post_tax_income
这只定义了一个getter。它允许您获取值,但您不能这样做:
user.post_tax_income = 36000
这需要定义一个setter:
@post_tax_income.setter
def set_post_tax_income(self):
...
您已将
税后收入定义为一种有获取者而无设置者的财产。所以你只能得到它,不能设定它。谢谢你,我明白你的意思。我添加了以下内容,但仍然不确定问题出在哪里。这是我第一次使用setter。“@post_tax_income.setter@db_session def set_post_tax_income(self):self.post_tax_income=post_tax_income这取决于您的计划。设置post\u tax\u income
甚至意味着什么?既然post\u tax\u income
似乎是一个计算属性,设置它有意义吗?也许你需要重新评估测试是如何编写的,它是一个计算属性。那么你为什么要在测试中设置它呢?这应该达到什么目的?如果目的是测试post\u tax\u income\u monthly
方法,那么为什么不在测试post\u tax\u income
的同一测试中进行测试?i、 e如果验证了税后收入,那么您也可以每月验证税后收入
方法。那么你就不需要事先知道税后收入了