Skip to content

Commit ce89f5d

Browse files
committed
Fix message passing in frozen errors
Follow common exception workflows instead of working around it. fixes #1514
1 parent eccd966 commit ce89f5d

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/attr/exceptions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from __future__ import annotations
44

5-
from typing import ClassVar
6-
75

86
class FrozenError(AttributeError):
97
"""
@@ -16,8 +14,10 @@ class FrozenError(AttributeError):
1614
.. versionadded:: 20.1.0
1715
"""
1816

19-
msg = "can't set attribute"
20-
args: ClassVar[tuple[str]] = [msg]
17+
def __init__(self):
18+
msg = "can't set attribute"
19+
super().__init__(msg)
20+
self.msg = msg
2121

2222

2323
class FrozenInstanceError(FrozenError):

tests/test_functional.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,20 @@ def test_frozen_instance(self, frozen_class):
264264
"""
265265
frozen = frozen_class(1)
266266

267-
with pytest.raises(FrozenInstanceError) as e:
267+
with pytest.raises(
268+
FrozenInstanceError, match="can't set attribute"
269+
) as e:
268270
frozen.x = 2
269271

270-
with pytest.raises(FrozenInstanceError) as e:
272+
assert e.value.msg == e.value.args[0] == "can't set attribute"
273+
assert 1 == frozen.x
274+
275+
with pytest.raises(
276+
FrozenInstanceError, match="can't set attribute"
277+
) as e:
271278
del frozen.x
272279

273-
assert e.value.args[0] == "can't set attribute"
280+
assert e.value.msg == e.value.args[0] == "can't set attribute"
274281
assert 1 == frozen.x
275282

276283
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)