From 031a9e26160748bc426e6d2a0b687603e1345bff Mon Sep 17 00:00:00 2001 From: whitequark Date: Tue, 1 Jan 2019 03:35:34 +0000 Subject: [PATCH] hdl.rec: use a helpful error on unknown field reference. --- nmigen/hdl/rec.py | 8 ++++++-- nmigen/test/test_hdl_rec.py | 9 +++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/nmigen/hdl/rec.py b/nmigen/hdl/rec.py index 32028af..f291087 100644 --- a/nmigen/hdl/rec.py +++ b/nmigen/hdl/rec.py @@ -87,10 +87,14 @@ class Record(Value): self.fields[field_name] = Signal(field_shape, name=concat(name, field_name)) def __getattr__(self, name): - return self.fields[name] + return self[name] def __getitem__(self, name): - return self.fields[name] + try: + return self.fields[name] + except KeyError: + raise NameError("Record does not have a field '{}'. Did you mean one of: {}?" + .format(name, ", ".join(self.fields))) from None def shape(self): return sum(len(f) for f in self.fields.values()), False diff --git a/nmigen/test/test_hdl_rec.py b/nmigen/test/test_hdl_rec.py index 501fda9..dd5cb7b 100644 --- a/nmigen/test/test_hdl_rec.py +++ b/nmigen/test/test_hdl_rec.py @@ -78,3 +78,12 @@ class RecordTestCase(FHDLTestCase): self.assertEqual(repr(r), "(rec stb)") self.assertEqual(r.stb.name, "stb") + + def test_wrong_field(self): + r = Record([ + ("stb", 1), + ("ack", 1), + ]) + with self.assertRaises(NameError, + msg="Record does not have a field 'en'. Did you mean one of: stb, ack?"): + r.en -- 2.30.2