On 2020-04-10 11:16 p.m., Greg Ewing wrote:
On 11/04/20 6:34 am, Soni L. wrote:
def _extract(self, obj): try: yield (self.key, obj[self.key]) except (TypeError, IndexError, KeyError): if not self.skippable: raise exceptions.ValidationError
You can separate out the TypeError like this:
try: get = obj.__getitem__ except TypeError: ... try: yield (self.key, get(self.key)) except (IndexError, KeyError): ...
I also don't have a good way of changing this to wrap stuff in RuntimeError
Your proposed solution also requires everyone to update their __getitem__ methods before it will work. What's more, in the transition period (which you can expect to be *very* long) when not everyone has done so, your code would fail much of the time, because you would only be catching exceptions that were raised "in" the appropriate object, and would miss anything raised by old methods that did not use "in".
So your solution kind of has a chicken-and-egg problem. It wouldn't work unless everyone started using it everywhere at the same time, which is never going to happen.
They used to say that about Rust.