Skip to content

Commit 8f72e7a

Browse files
committed
backup properties instead of potentially empty private attributes
1 parent 3f4709a commit 8f72e7a

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/compas/tolerance.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -398,14 +398,14 @@ def temporary(
398398
"""
399399
# Save current state
400400
saved = {
401-
"unit": self._unit,
402-
"absolute": self._absolute,
403-
"relative": self._relative,
404-
"angular": self._angular,
405-
"approximation": self._approximation,
406-
"precision": self._precision,
407-
"lineardeflection": self._lineardeflection,
408-
"angulardeflection": self._angulardeflection,
401+
"unit": self.unit,
402+
"absolute": self.absolute,
403+
"relative": self.relative,
404+
"angular": self.angular,
405+
"approximation": self.approximation,
406+
"precision": self.precision,
407+
"lineardeflection": self.lineardeflection,
408+
"angulardeflection": self.angulardeflection,
409409
}
410410
try:
411411
# Apply temporary changes

tests/compas/test_tolerance.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ def test_tolerance_temporary_restores_on_exception():
6262
assert TOL.absolute == original
6363

6464

65+
def test_tolerance_temporary_restores_unit():
66+
"""Test that temporary() restores values even if an exception occurs."""
67+
original = TOL.unit
68+
69+
with TOL.temporary(unit="MM"):
70+
assert TOL.unit == "MM"
71+
72+
assert TOL.unit == original
73+
74+
6575
def test_tolerance_format_number():
6676
assert TOL.format_number(0, precision=3) == "0.000"
6777
assert TOL.format_number(0.5, precision=3) == "0.500"

0 commit comments

Comments
 (0)