Skip to content

Commit 71323b0

Browse files
committed
Only raise NotImplementedError in abstract classes
Raising NotImplementedError states that a method is abstract and should be implemented in subclasses. This causes pylint errors when subclassing KeyedList: Method 'not_implemented' is abstract in class 'KeyedList' but is not overridden [abstract-method] Raise TypeError for unsupported operations in KeyedList and for unsupported schema/data node types. Signed-off-by: Robin Jarry <robin.jarry@6wind.com>
1 parent 302fbbb commit 71323b0

File tree

4 files changed

+32
-34
lines changed

4 files changed

+32
-34
lines changed

libyang/data.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,7 @@ def new(cls, context: "libyang.Context", cdata) -> "DNode":
595595
cdata = ffi.cast("struct lyd_node *", cdata)
596596
nodecls = cls.NODETYPE_CLASS.get(cdata.schema.nodetype, None)
597597
if nodecls is None:
598-
raise NotImplementedError(
599-
"node type %s not implemented" % cdata.schema.nodetype
600-
)
598+
raise TypeError("node type %s not implemented" % cdata.schema.nodetype)
601599
return nodecls(context, cdata)
602600

603601

libyang/keyed_list.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def extend(self, iterable: Iterable) -> None:
6969

7070
def pop(self, key: ListKeyVal = None, default: Any = None) -> Any:
7171
if key is None or isinstance(key, (int, slice)):
72-
raise NotImplementedError("non-ordered lists cannot be accessed by index")
72+
raise TypeError("non-ordered lists cannot be accessed by index")
7373
return self._map.pop(key, default)
7474

7575
def remove(self, element: Any) -> None:
@@ -78,12 +78,12 @@ def remove(self, element: Any) -> None:
7878

7979
def __getitem__(self, key: ListKeyVal) -> Any:
8080
if isinstance(key, (int, slice)):
81-
raise NotImplementedError("non-ordered lists cannot be accessed by index")
81+
raise TypeError("non-ordered lists cannot be accessed by index")
8282
return self._map[key]
8383

8484
def __delitem__(self, key: ListKeyVal) -> None:
8585
if isinstance(key, (int, slice)):
86-
raise NotImplementedError("non-ordered lists cannot be accessed by index")
86+
raise TypeError("non-ordered lists cannot be accessed by index")
8787
del self._map[key]
8888

8989
def __eq__(self, other: Any) -> bool:
@@ -140,24 +140,24 @@ def __deepcopy__(self, memo) -> "KeyedList":
140140
return k
141141

142142
# unsupported list API methods
143-
def not_implemented(self, *args, **kwargs):
144-
raise NotImplementedError()
145-
146-
index = not_implemented
147-
insert = not_implemented
148-
reverse = not_implemented
149-
sort = not_implemented
150-
__add__ = not_implemented
151-
__ge__ = not_implemented
152-
__gt__ = not_implemented
153-
__iadd__ = not_implemented
154-
__imul__ = not_implemented
155-
__le__ = not_implemented
156-
__lt__ = not_implemented
157-
__mul__ = not_implemented
158-
__reversed__ = not_implemented
159-
__rmul__ = not_implemented
160-
__setitem__ = not_implemented
143+
def __unsupported(self, *args, **kwargs):
144+
raise TypeError("unsupported operation for non-ordered lists")
145+
146+
index = __unsupported
147+
insert = __unsupported
148+
reverse = __unsupported
149+
sort = __unsupported
150+
__add__ = __unsupported
151+
__ge__ = __unsupported
152+
__gt__ = __unsupported
153+
__iadd__ = __unsupported
154+
__imul__ = __unsupported
155+
__le__ = __unsupported
156+
__lt__ = __unsupported
157+
__mul__ = __unsupported
158+
__reversed__ = __unsupported
159+
__rmul__ = __unsupported
160+
__setitem__ = __unsupported
161161

162162

163163
# -------------------------------------------------------------------------------------

libyang/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ def new(context: "libyang.Context", cdata) -> "SNode":
862862
cdata = ffi.cast("struct lys_node *", cdata)
863863
nodecls = SNode.NODETYPE_CLASS.get(cdata.nodetype, None)
864864
if nodecls is None:
865-
raise NotImplementedError("node type %s not implemented" % cdata.nodetype)
865+
raise TypeError("node type %s not implemented" % cdata.nodetype)
866866
return nodecls(context, cdata)
867867

868868

tests/test_keyedlist.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@
1010
class KeyedListTest(unittest.TestCase):
1111
def test_keylist_base(self):
1212
l = ly.KeyedList(["x"])
13-
with self.assertRaises(NotImplementedError):
13+
with self.assertRaises(TypeError):
1414
l.index("x")
15-
with self.assertRaises(NotImplementedError):
15+
with self.assertRaises(TypeError):
1616
l.insert(0, "x")
17-
with self.assertRaises(NotImplementedError):
17+
with self.assertRaises(TypeError):
1818
l.reverse()
19-
with self.assertRaises(NotImplementedError):
19+
with self.assertRaises(TypeError):
2020
l.sort()
21-
with self.assertRaises(NotImplementedError):
21+
with self.assertRaises(TypeError):
2222
l["x"] = 2
23-
with self.assertRaises(NotImplementedError):
23+
with self.assertRaises(TypeError):
2424
l[0] # pylint: disable=pointless-statement
25-
with self.assertRaises(NotImplementedError):
25+
with self.assertRaises(TypeError):
2626
del l[0]
27-
with self.assertRaises(NotImplementedError):
27+
with self.assertRaises(TypeError):
2828
l.pop()
29-
with self.assertRaises(NotImplementedError):
29+
with self.assertRaises(TypeError):
3030
l.pop(0)
3131

3232
def test_keylist_leaflist(self):

0 commit comments

Comments
 (0)