Skip to content

Commit 065e62b

Browse files
committed
Refactor deprecating For/If attributes previously inherited from Keyword
1 parent a755b9e commit 065e62b

2 files changed

Lines changed: 45 additions & 36 deletions

File tree

‎src/robot/model/control.py‎

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,25 @@
2222
from .tags import Tags
2323

2424

25+
def deprecated(method):
26+
def wrapper(self, *args, **kws):
27+
"""Deprecated."""
28+
if self.deprecate_keyword_attributes:
29+
warnings.warn(
30+
"'%s.%s' is deprecated." % (type(self).__name__, method.__name__)
31+
)
32+
return method(self, *args, **kws)
33+
return wrapper
34+
35+
2536
@py3to2
2637
@Body.register
2738
class For(BodyItem):
2839
type = BodyItem.FOR_TYPE
2940
body_class = Body
3041
repr_args = ('variables', 'flavor', 'values')
3142
__slots__ = ['variables', 'flavor', 'values']
43+
deprecate_keyword_attributes = True
3244

3345
def __init__(self, variables=(), flavor='IN', values=(), parent=None):
3446
self.variables = variables
@@ -65,42 +77,34 @@ def __str__(self):
6577
# TODO: Remove deprecated Keyword related properties in RF 4.1/5.0.
6678

6779
@property
80+
@deprecated
6881
def name(self):
69-
"""Deprecated since Robot Framework 4.0."""
70-
warnings.warn("'For.name' is deprecated since Robot Framework 4.0. "
71-
"Access 'variables', 'flavor' or 'values' directly or "
72-
"use 'str()' to get a string representation.", UserWarning)
7382
return '%s %s [ %s ]' % (' | '.join(self.variables), self.flavor,
7483
' | '.join(self.values))
7584

7685
@property
86+
@deprecated
7787
def doc(self):
78-
"""Deprecated since Robot Framework 4.0."""
79-
warnings.warn("'For.doc' is deprecated since Robot Framework 4.0.", UserWarning)
8088
return ''
8189

8290
@property
91+
@deprecated
8392
def args(self):
84-
"""Deprecated since Robot Framework 4.0."""
85-
warnings.warn("'For.args' is deprecated since Robot Framework 4.0.", UserWarning)
8693
return ()
8794

8895
@property
96+
@deprecated
8997
def assign(self):
90-
"""Deprecated since Robot Framework 4.0."""
91-
warnings.warn("'For.assign' is deprecated since Robot Framework 4.0.", UserWarning)
9298
return ()
9399

94100
@property
101+
@deprecated
95102
def tags(self):
96-
"""Deprecated since Robot Framework 4.0."""
97-
warnings.warn("'For.tags' is deprecated since Robot Framework 4.0.", UserWarning)
98103
return Tags()
99104

100105
@property
106+
@deprecated
101107
def timeout(self):
102-
"""Deprecated since Robot Framework 4.0."""
103-
warnings.warn("'For.timeout' is deprecated since Robot Framework 4.0.", UserWarning)
104108
return None
105109

106110

@@ -111,6 +115,7 @@ class If(BodyItem):
111115
inactive = object()
112116
repr_args = ('condition',)
113117
__slots__ = ['condition', '_orelse']
118+
deprecate_keyword_attributes = True
114119

115120
def __init__(self, condition=None, parent=None):
116121
self.condition = condition
@@ -175,39 +180,31 @@ def __bool__(self):
175180
# TODO: Remove deprecated Keyword related properties in RF 4.1/5.0.
176181

177182
@property
183+
@deprecated
178184
def name(self):
179-
"""Deprecated since Robot Framework 4.0."""
180-
warnings.warn("'If.name' is deprecated since Robot Framework 4.0. "
181-
"Access 'condition' directly or use 'str()' to get "
182-
"a string representation.", UserWarning)
183185
return self.condition
184186

185187
@property
188+
@deprecated
186189
def doc(self):
187-
"""Deprecated since Robot Framework 4.0."""
188-
warnings.warn("'If.doc' is deprecated since Robot Framework 4.0.", UserWarning)
189190
return ''
190191

191192
@property
193+
@deprecated
192194
def args(self):
193-
"""Deprecated since Robot Framework 4.0."""
194-
warnings.warn("'If.args' is deprecated since Robot Framework 4.0.", UserWarning)
195195
return ()
196196

197197
@property
198+
@deprecated
198199
def assign(self):
199-
"""Deprecated since Robot Framework 4.0."""
200-
warnings.warn("'If.assign' is deprecated since Robot Framework 4.0.", UserWarning)
201200
return ()
202201

203202
@property
203+
@deprecated
204204
def tags(self):
205-
"""Deprecated since Robot Framework 4.0."""
206-
warnings.warn("'If.tags' is deprecated since Robot Framework 4.0.", UserWarning)
207205
return Tags()
208206

209207
@property
208+
@deprecated
210209
def timeout(self):
211-
"""Deprecated since Robot Framework 4.0."""
212-
warnings.warn("'If.timeout' is deprecated since Robot Framework 4.0.", UserWarning)
213210
return None

‎utest/model/test_control.py‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,23 @@ def test_string_reprs(self):
2929
assert_equal(str(for_), unicode(for_).encode('UTF-8'))
3030

3131
def test_deprecated_keyword_specific_properties(self):
32-
for_ = For(['${x}', '${y}'], 'IN', ['a', 'b', 'c', 'd'])
33-
tmpl = "'For.%s' is deprecated since Robot Framework 4.0."
32+
class NotDeprecated(For):
33+
deprecate_keyword_attributes = False
34+
35+
deprecated = For(['${x}', '${y}'], 'IN', ['a', 'b', 'c', 'd'])
36+
not_deprecated = NotDeprecated(['${x}', '${y}'], 'IN', ['a', 'b', 'c', 'd'])
37+
3438
for name, expected in [('name', '${x} | ${y} IN [ a | b | c | d ]'),
3539
('doc', ''),
3640
('args', ()),
3741
('assign', ()),
3842
('tags', Tags()),
3943
('timeout', None)]:
4044
with warnings.catch_warnings(record=True) as w:
41-
assert_equal(getattr(for_, name), expected)
42-
assert_true(str(w[0].message).startswith(tmpl % name))
45+
assert_equal(getattr(not_deprecated, name), expected)
46+
assert_equal(len(w), 0)
47+
assert_equal(getattr(deprecated, name), expected)
48+
assert_equal(str(w[0].message), "'For.%s' is deprecated." % name)
4349
assert_equal(w[0].category, UserWarning)
4450

4551

@@ -72,17 +78,23 @@ def test_string_reprs(self):
7278
assert_equal(str(if_), unicode(if_).encode('UTF-8'))
7379

7480
def test_deprecated_keyword_specific_properties(self):
75-
if_ = If('$x > 0')
76-
tmpl = "'If.%s' is deprecated since Robot Framework 4.0."
81+
class NotDeprecated(If):
82+
deprecate_keyword_attributes = False
83+
84+
deprecated = If('$x > 0')
85+
not_deprecated = NotDeprecated('$x > 0')
86+
7787
for name, expected in [('name', '$x > 0'),
7888
('doc', ''),
7989
('args', ()),
8090
('assign', ()),
8191
('tags', Tags()),
8292
('timeout', None)]:
8393
with warnings.catch_warnings(record=True) as w:
84-
assert_equal(getattr(if_, name), expected)
85-
assert_true(str(w[0].message).startswith(tmpl % name))
94+
assert_equal(getattr(not_deprecated, name), expected)
95+
assert_equal(len(w), 0)
96+
assert_equal(getattr(deprecated, name), expected)
97+
assert_equal(str(w[0].message), "'If.%s' is deprecated." % name)
8698
assert_equal(w[0].category, UserWarning)
8799

88100

0 commit comments

Comments
 (0)