Re: Round 2 for "A ModuleSpec Type for the Import System"

Nick Coghlan <[email protected]>
Newsgroups gmane.comp.python.import
Message-ID <CADiSq7cR7-C9CyGca52a6hZno_N48ieSzGk=7_JYGpW0FSq5AA@mail.gmail.com>
On 13 Aug 2013 18:48, "Eric Snow" <[email protected]> wrote:
>
> On Tue, Aug 13, 2013 at 7:21 AM, Brett Cannon <[email protected]> wrote:
>>
>> On Tue, Aug 13, 2013 at 12:17 AM, Eric Snow <[email protected]>
wrote:
>>>
>>> On Sun, Aug 11, 2013 at 2:08 PM, Brett Cannon <[email protected]> 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.

That ends up registering ModuleSpec as an example of every loader ABC, so
it doesn't work at all. Making the importlib ABC hooks ModuleSpec aware (so
they knew to check the loader, not the spec) would be pretty easy, though.

>
> -eric
>
> _______________________________________________
> Import-SIG mailing list
> [email protected]
> http://mail.python.org/mailman/listinfo/import-sig
>

_______________________________________________
Import-SIG mailing list
[email protected]
http://mail.python.org/mailman/listinfo/import-sig
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.