[Python-ideas] inspect.getclassdistance

Alexis Lee alexisl at hp.com
Mon Jan 5 15:05:20 CET 2015


Andrew Barnert said on Mon, Jan 05, 2015 at 01:09:37PM +0100:
> On Jan 5, 2015, at 12:20, Alexis Lee <alexisl at hp.com> wrote:
> You're trying to find out if dst is an ancestor of src (the option of
> taking a list of pairs, despite being documented, seems to be there
> only for internal use by recursive calls) and, if so, how long the
> path is between them.

All correct.

(moved for clarity)
> When would it ever be interesting?

The usecase is here (comments at line 861, also see 866-894):
    https://review.openstack.org/#/c/142835/5/nova/api/openstack/compute/servers.py

IE I receive a NeutronException of some specialised type; based on that
there are multiple Nova exceptions I might want to throw. I would use
getclassdistance (or rather, your walker) to sort those and throw the
first, most-specific exception. EG I might be considering throwing
either HTTPConflict or HTTPBadRequest, but the former is-a latter, hence
it's more specific and the one which should be thrown.

>  * Only actual inheritance counts; abc.register or other subclass hooks are ignored.

Never used abc; happy to enhance if the usecase is useful.

>  * In case of diamond inheritance where the sides are of unequal
>  length, the shorter one counts (even if that shorter path would never
>  be followed by the c3 rule and therefore isn't even relevant at
>  runtime).

Yes. Although thinking about it, I actually want the longest path to
count. Curses. This behaviour should be a toggle parameter.

>  * The actual classes on the path are irrelevant to you, you just want
>  its length.

Yes.

>  * More generally, you don't care about the c3 MRO path, or the actual
>  path taken for any particular method's lookup, just the inheritance
>  path itself.

Yes. MRO distance is trivial to obtain from __mro__.

> Also, it seems like it would be simpler to just create a general
> inheritance-BFS-walker function that yields each base as it goes,
> which would allow you to write this function as a one-liner.

Yes, that's fair. I'll wait for some more feedback on whether such a
thing would be useful in the stdlib at all before writing that.

> I also don't get why you're using recursion rather than a loop here,

Just how I think.


Alexis
-- 
Nova Engineer, HP Cloud.  AKA lealexis, lxsli.


More information about the Python-ideas mailing list