Skip to content

Commit ac88ca3

Browse files
authored
Merge pull request #948 from effigies/mnt/deprecate_setattr_on_read
MNT: Officially deprecate onetime.setattr_on_read
2 parents 516434c + 575b0b0 commit ac88ca3

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

nibabel/nicom/dicomwrappers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from . import csareader as csar
2121
from .dwiparams import B2q, nearest_pos_semi_def, q2bg
2222
from ..openers import ImageOpener
23-
from ..onetime import setattr_on_read as one_time
23+
from ..onetime import auto_attr as one_time
2424
from ..pydicom_compat import tag_for_keyword, Sequence
2525
from ..deprecated import deprecate_with_version
2626

nibabel/onetime.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
References
1616
----------
1717
[1] How-To Guide for Descriptors, Raymond
18-
Hettinger. http://users.rcn.com/python/download/Descriptor.htm
18+
Hettinger. https://docs.python.org/howto/descriptor.html
1919
2020
[2] Python data model, https://docs.python.org/reference/datamodel.html
2121
"""
2222

23+
from nibabel.deprecated import deprecate_with_version
24+
2325
# -----------------------------------------------------------------------------
2426
# Classes and Functions
2527
# -----------------------------------------------------------------------------
@@ -176,4 +178,6 @@ def auto_attr(func):
176178
# -----------------------------------------------------------------------------
177179

178180
# For backwards compatibility
179-
setattr_on_read = auto_attr
181+
setattr_on_read = deprecate_with_version(
182+
message="setattr_on_read has been renamed to auto_attr. Please use nibabel.onetime.auto_attr",
183+
since="3.2", until="5.0")(auto_attr)

nibabel/tests/test_onetime.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
from nibabel.onetime import auto_attr, setattr_on_read
3+
4+
5+
def test_setattr_on_read():
6+
with pytest.deprecated_call():
7+
class MagicProp:
8+
@setattr_on_read
9+
def a(self):
10+
return object()
11+
12+
x = MagicProp()
13+
assert 'a' not in x.__dict__
14+
obj = x.a
15+
assert 'a' in x.__dict__
16+
# Each call to object() produces a unique object. Verify we get the same one every time.
17+
assert x.a is obj

nibabel/tests/test_removalschedule.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
]
1313

1414
OBJECT_SCHEDULE = [
15-
("5.0.0", [("nibabel.pydicom_compat", "dicom_test")]),
15+
("5.0.0", [("nibabel.pydicom_compat", "dicom_test"),
16+
("nibabel.onetime", "setattr_on_read")]),
1617
("3.0.0", [("nibabel.testing", "catch_warn_reset")]),
1718
# Verify that the test will be quiet if the schedule outlives the modules
1819
("1.0.0", [("nibabel.nosuchmod", "anyobj"), ("nibabel.nifti1", "nosuchobj")]),

0 commit comments

Comments
 (0)