AttributeError: Can't set attribute - How can I fix it?
AttributeError: Can't set attribute - How can I fix it?
我正在使用 pytest 开发测试,但我遇到了 AttributeError: can't set attribute.
这是型号:
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)
这就是我进行测试的地方,前两个工作正常。最后一个测试每月post税收收入得到有关设置属性的错误。
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
@property
@db_session
def post_tax_income(self):
return round(self.pretax_income - (self.pretax_income * self.tax_rate), 2)
这只定义了一个getter。它允许您获取值,但您不能这样做:
user.post_tax_income = 36000
这还需要定义一个 setter:
@post_tax_income.setter
def set_post_tax_income(self):
...
我正在使用 pytest 开发测试,但我遇到了 AttributeError: can't set attribute.
这是型号:
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)
这就是我进行测试的地方,前两个工作正常。最后一个测试每月post税收收入得到有关设置属性的错误。
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
@property
@db_session
def post_tax_income(self):
return round(self.pretax_income - (self.pretax_income * self.tax_rate), 2)
这只定义了一个getter。它允许您获取值,但您不能这样做:
user.post_tax_income = 36000
这还需要定义一个 setter:
@post_tax_income.setter
def set_post_tax_income(self):
...