<div dir="ltr"><div>We could do that, but we would have to do it in many places. Wouldn't it be better to centralize the Exception raising in one place?<br><br></div>I'm looking for a case where cast() is called, yet it is correct for cast() to transform the type sometimes but not others. If that situation exists then what I'm proposing isn't right. Does this situation exist?<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jun 5, 2017 at 2:08 PM, Austin Macdonald <span dir="ltr"><<a href="mailto:amacdona@redhat.com" target="_blank">amacdona@redhat.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><br></div>I think there is still an improvement to be made with cast() when it fails to cast. The cast method itself can't know if you're dealing with the master or most detailed object, but the code author calling cast() always knows and if they don't need it cast() they shouldn't call cast(). For example the publish code here [0]. The publish code calls cast() because it needs the type to be the detail version of models.Publisher. Anytime the plugin writer (or core) looks up a master instance and calls cast() and it returns the exact same object that was requested to be cast() that should cause an error instead of failing silently. What do you all think about this?<br></div></div></blockquote><div><br></div></span><div>Rather than adding an argument to cast we could just catch the `AttributeError`. </div><div><br></div><div>From the publish example [0]:</div><div><br></div><div>try:</div><div>    publisher.publish() # publish is defined on the detail model, never the master model.</div><div>except AttributeError as e:</div><div>    raise SomePulpCodedException</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><br><br>[0]: <a href="https://github.com/pulp/pulp/commit/bf7433236c5cf5203808341c74715ad257d69718#diff-5e2d175476716cc65d5d2d61b7eb5efdR35" target="_blank">https://github.com/pulp/pulp/c<wbr>ommit/bf7433236c5cf5203808341c<wbr>74715ad257d69718#diff-5e2d1754<wbr>76716cc65d5d2d61b7eb5efdR35</a><br><br></div><br></div><div class="gmail_extra"><br></div></blockquote></div></div></div>
</blockquote></div><br></div>