classification
Title: namedtuple integration for importlib.abc.Loader
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: brett.cannon, captain-kark, eric.smith, ethan.furman, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2019-07-18 19:07 by captain-kark, last changed 2019-08-05 06:20 by captain-kark. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 14848 closed python-dev, 2019-07-18 20:13
Messages (7)
msg348124 - (view) Author: Andrew Yurisich (captain-kark) * Date: 2019-07-18 19:07
I wanted to return a namedtuple from a concrete implementation of an importlib.abc.Loader base class, and wasn't able to provide a __spec__ property on the underlying class behind the namedtuple. All return values from importlib.abc.Loader#create_module need to have a __spec__ property set.

Similar to the namedtuple optional argument 'module', I'd like to be able to pass in a 'spec', and add this value to result.__spec__ before returning the final result.
msg348146 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-07-19 07:04
Brett, do you have any thoughts on this?  My initial take is that __spec__ is primarily about import logic and that it likely shouldn't creep into exec'd code like dataclasses and named tuples.  Also, I'm reluctant to expand the API for something that looks like a one time use, especially when other alternatives are possible.
msg348160 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-07-19 09:49
I think using a dataclass here would be easier, since you can control class variables. Is there some reason that your loader must be a namedtuple?

Something like:

from typing import ClassVar
from dataclasses import dataclass

@dataclass
class MyLoader:
    __spec__: ClassVar["Any"] = None
    name: str

l = MyLoader('test')

I'm not sure of the actual type of __spec__, I'm just using "Any" as a convenient placeholder. I'm also not sure if your intention is to inherit from importlib.abc.Loader, but that's easy enough.
msg348194 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-07-19 19:08
It is not hard to set __spec__ (as well as any other attributes) after creating a namedtuple class.

A = namedtuple(...)
A.__spec__ = ...

or

class A(namedtuple(...)):
    __spec__ = ...

__spec__ do not have anything to namedtuple. It is not like __module__ or __doc__ setting which would benefit almost every public namedtuple class. It is not even special for types.
msg348197 - (view) Author: Andrew Yurisich (captain-kark) * Date: 2019-07-19 20:25
You're right, I was invoking the namedtuple on the same line that I was
defining it, freezing it in the process.

I split it to into two statements, and snuck the __spec__ attribute between
the definition and the instantiation.

I'll update the examples on my GitHub issue in the morning, and probably
close the issue out unless I find something else that is blocking me.

Thanks for the input 👍

On Fri, Jul 19, 2019, 22:08 Serhiy Storchaka <report@bugs.python.org> wrote:

>
> Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment:
>
> It is not hard to set __spec__ (as well as any other attributes) after
> creating a namedtuple class.
>
> A = namedtuple(...)
> A.__spec__ = ...
>
> or
>
> class A(namedtuple(...)):
>     __spec__ = ...
>
> __spec__ do not have anything to namedtuple. It is not like __module__ or
> __doc__ setting which would benefit almost every public namedtuple class.
> It is not even special for types.
>
> ----------
> nosy: +serhiy.storchaka
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue37623>
> _______________________________________
>
msg348208 - (view) Author: Andrew Yurisich (captain-kark) * Date: 2019-07-20 05:54
This issue was raised due to a misunderstanding of the namedtuple creation process. After creating the fields, but before assigning them, __spec__ is trivially added to namedtuple class' definition as a property.

Thanks again @serhiy.storchaka
msg349028 - (view) Author: Andrew Yurisich (captain-kark) * Date: 2019-08-05 06:20
If anyone is interested in the progress I was able to make as a result of this discussion, feel free to check out https://github.com/captain-kark/python-module-resources/blob/d85453ff4f5022127874a5842449d95bb5eda234/module_resources/module_resources.py and leave you feedback or comments.
History
Date User Action Args
2019-08-05 06:20:28captain-karksetmessages: + msg349028
2019-07-20 05:54:30captain-karksetstatus: open -> closed
resolution: works for me
messages: + msg348208

stage: patch review -> resolved
2019-07-19 20:25:37captain-karksetmessages: + msg348197
2019-07-19 19:08:45serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg348194
2019-07-19 09:49:37eric.smithsetmessages: + msg348160
2019-07-19 09:33:29rhettingersetnosy: + ethan.furman
2019-07-19 07:04:47rhettingersetnosy: + brett.cannon

messages: + msg348146
versions: - Python 3.5, Python 3.6, Python 3.7, Python 3.8
2019-07-18 21:31:26eric.smithsetassignee: rhettinger

nosy: + eric.smith, rhettinger
2019-07-18 20:13:12python-devsetkeywords: + patch
stage: patch review
pull_requests: + pull_request14639
2019-07-18 19:07:23captain-karkcreate