Skip to content

Commit ca69f11

Browse files
xmlunwrap: Fix pytype warning and cover changed function
Signed-off-by: Bernhard Kaindl <bernhard.kaindl@cloud.com>
1 parent cadfb48 commit ca69f11

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

tests/test_xmlunwrap.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
import xml.dom.minidom
33

44
from xcp.xmlunwrap import (getElementsByTagName, getText, getMapAttribute,
5-
getStrAttribute, XmlUnwrapError)
5+
getStrAttribute, getIntAttribute, XmlUnwrapError)
66

77
class TestXmlUnwrap(unittest.TestCase):
88
def setUp(self):
9-
a_text = """<installation mode='test'>
9+
a_text = """<installation mode='test' integer='1'>
1010
<fred>text1</fred>
1111
<fred>text2</fred>
1212
</installation>"""
@@ -20,17 +20,25 @@ def test(self):
2020
for el in getElementsByTagName(self.top_el, ["fred"])],
2121
["text1", "text2"])
2222

23+
# Test xcp.xmlunwrap.getIntAttribute()
24+
self.assertEqual(getIntAttribute(self.top_el, ["integer"], 5), 1)
25+
self.assertEqual(getIntAttribute(self.top_el, ["noexist"], 5), 5)
26+
with self.assertRaises(XmlUnwrapError):
27+
getIntAttribute(self.top_el, ["nonexisting-attribute"])
28+
29+
# Test xcp.xmlunwrap.getMapAttribute()
2330
x = getMapAttribute(self.top_el, ["mode"], [('test', 42), ('stuff', 77)])
2431
self.assertEqual(x, 42)
2532
x = getMapAttribute(self.top_el, ["made"], [('test', 42), ('stuff', 77)],
2633
default='stuff')
2734
self.assertEqual(x, 77)
2835

36+
# Test xcp.xmlunwrap.getIntAttribute()
2937
x = getStrAttribute(self.top_el, ["mode"])
3038
self.assertEqual(x, "test")
3139
x = getStrAttribute(self.top_el, ["made"])
3240
self.assertEqual(x, "")
33-
x = getStrAttribute(self.top_el, ["made"], None)
41+
x = getStrAttribute(self.top_el, ["made"], None) # pyright: ignore
3442
self.assertEqual(x, None)
3543

3644
with self.assertRaises(XmlUnwrapError):

xcp/xmlunwrap.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ def getIntAttribute(el, attrs, default = None):
7373
if val == '':
7474
return default
7575
try:
76-
int_val = int(val, 0)
76+
return int(val, 0)
7777
except Exception as e:
7878
six.raise_from(XmlUnwrapError("Invalid integer value for %s" % attrs[0]), e)
79-
return int_val
8079

8180
def getMapAttribute(el, attrs, mapping, default = None):
8281
mandatory = (default == None)

0 commit comments

Comments
 (0)