[Import-SIG] Round 2 for "A ModuleSpec Type for the Import System"

Eric Snow ericsnowcurrently at gmail.com
Wed Aug 14 01:47:53 CEST 2013


On Tue, Aug 13, 2013 at 7:21 AM, Brett Cannon <brett at python.org> wrote:

> On Tue, Aug 13, 2013 at 12:17 AM, Eric Snow <ericsnowcurrently at gmail.com>wrote:
>
>> On Sun, Aug 11, 2013 at 2:08 PM, Brett Cannon <brett at python.org> wrote:
>>
>>>
>>>
> [SNIP]
>
>
>>
>>>
>>>>  ``module_repr()`` also conflicts with the same
>>>> method on loaders, but that workaround is not complicated since both are
>>>> methods.
>>>>
>>>> Unfortunately, the ability to proxy does not extend to ``id()``
>>>> comparisons and ``isinstance()`` tests.  In the case of the return value
>>>> of ``find_module()``, we accept that break in backward compatibility.
>>>> However, we will mitigate the problem with ``isinstance()`` somewhat by
>>>> registering ``ModuleSpec`` on the loaders in ``importlib.abc``.
>>>>
>>>
>>> Actually, ModuleSpec doesn't even need to register; __instancecheck__
>>> and __subclasscheck__ can just be defined and delegate by calling
>>> issubclass/isinstance on the loader as appropriate.
>>>
>>
>> Do you mean add custom versions of those methods to importlib.abc.Loader?
>>
>
> Nope, I meant ModuleSpec because every time I have a reason to override
> something it's on the object and not the class and so I forget the support
> is the other way around. Argh.
>

Yeah, that would make things a lot easier.


>  That should work as well as the register approach.  It won't work for all
>> loaders but should be good enough.  I was just planning on registering
>> ModuleSpec on the loader in the setter for a `loader` property on
>> ModuleSpec.
>>
>
> But the registration is at the class level so how would that work?
>

@property
def loader(self):
    return self._loader

@loader.setter
def loader(self, loader):
    try:
        register = loader.__class__.register
    except AttributeError:
        pass
    else:
        register(self.__class__)
    self._loader = loader

It's not pretty and it won't work on non-ABCs, but it's better than
nothing.  The likelihood of someone doing an isinstance check on a loader
seems pretty low though.  Of course, I'm planning on doing just that for
handling of namespace packages, but that's a little different.

-eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/import-sig/attachments/20130813/00ae2e2c/attachment.html>


More information about the Import-SIG mailing list