hdl.rec: unbreak hasattr(rec, ...).
hasattr() requires that AttributeError be raised. Change __getitem__ to raise AttributeError, too, since it is fundamentally just sugar for getattr().
This commit is contained in:
parent
6fae06aea9
commit
a1940c5528
|
@ -109,8 +109,8 @@ class Record(Value):
|
|||
reference = "Unnamed record"
|
||||
else:
|
||||
reference = "Record '{}'".format(self.name)
|
||||
raise NameError("{} does not have a field '{}'. Did you mean one of: {}?"
|
||||
.format(reference, item, ", ".join(self.fields))) from None
|
||||
raise AttributeError("{} does not have a field '{}'. Did you mean one of: {}?"
|
||||
.format(reference, item, ", ".join(self.fields))) from None
|
||||
else:
|
||||
return super().__getitem__(item)
|
||||
|
||||
|
|
|
@ -71,6 +71,9 @@ class RecordTestCase(FHDLTestCase):
|
|||
self.assertEqual(r.stb.name, "r__stb")
|
||||
self.assertEqual(r["stb"].name, "r__stb")
|
||||
|
||||
self.assertTrue(hasattr(r, "stb"))
|
||||
self.assertFalse(hasattr(r, "xxx"))
|
||||
|
||||
def test_unnamed(self):
|
||||
r = [Record([
|
||||
("stb", 1)
|
||||
|
@ -93,7 +96,10 @@ class RecordTestCase(FHDLTestCase):
|
|||
("stb", 1),
|
||||
("ack", 1),
|
||||
])
|
||||
with self.assertRaises(NameError,
|
||||
with self.assertRaises(AttributeError,
|
||||
msg="Record 'r' does not have a field 'en'. Did you mean one of: stb, ack?"):
|
||||
r["en"]
|
||||
with self.assertRaises(AttributeError,
|
||||
msg="Record 'r' does not have a field 'en'. Did you mean one of: stb, ack?"):
|
||||
r.en
|
||||
|
||||
|
@ -102,7 +108,7 @@ class RecordTestCase(FHDLTestCase):
|
|||
("stb", 1),
|
||||
("ack", 1),
|
||||
])][0]
|
||||
with self.assertRaises(NameError,
|
||||
with self.assertRaises(AttributeError,
|
||||
msg="Unnamed record does not have a field 'en'. Did you mean one of: stb, ack?"):
|
||||
r.en
|
||||
|
||||
|
|
Loading…
Reference in a new issue