On Sat, 09 Feb 2013 13:57:41 -0500, Terry Reedy <[email protected]> wrote:
> On 2/9/2013 8:31 AM, R. David Murray wrote:
> Changing AttributeError to TypeError only requires try-except, which is
> cheap if there is no error, not an early type check.
>
> > The reason we avoid such type checks is that we prefer to operate via
> > "duck typing", which means that if an object behaves like the expected
> > input, it is accepted.
>
> And catching the existing AttributeError allows work-alikes. I agree
> that an isinstance check is bad as well as unnecessary.
>
> As it is, the core code for path, given above, is *already* wrapped in
> try: except TypeError, (to give a more friendly error message!, as
> TypeError is most like from a unicode-bytes mixing). So there would be
> no extra cost for correct calls and all that is needed is another except
> clause.
>
> except AttributeError as e:
> bad = e.args[0].split()[0]
> msg = "all components must be strings; one is a {}".format(bad)
> raise TypeError("all components must be strings")
>
> Thomas, unless David or someone else shoots down this idea here, I say
> go ahead and open an enhancement issue and add terry.reedy as nosy.
Especially given that there is already a try/except there, this seems
fine to me. I think perhaps the error text would be better if it
referred only to the type that is invalid, not to str. So something
like:
TypeError("object of type {} is not valid as a path
"component".format(type(bad)))
--David
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com