This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Cant sort ModuleInfo instances
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, berker.peksag, eric.smith, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2019-05-19 03:18 by BTaskaya, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 13414 closed BTaskaya, 2019-05-19 03:33
Messages (9)
msg342830 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2019-05-19 03:18
I can't sort the result of iter_modules;
>>> import random, pkgutil
>>> modules = list(pkgutil.iter_modules(None))
>>> random.shuffle(modules)
>>> sorted(modules)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'FileFinder' and 'FileFinder'
msg342832 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2019-05-19 03:30
I think dataclasses can be used to do it with order, frozen true parameters. Also a __getitem__ is required for doesnt break anything.
msg342841 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-05-19 11:37
Unfortunately your change isn't backward compatible with the existing (namedtuple) version.

I expect this to fail in the dataclass version:
>>> finder, name, ispkg = list(pkgutil.iter_modules(None))[0]

And since this is an enhancement, it can only go in to 3.8. And the window is closing for that, so it's more likely to be 3.9, if we decide that backward compatibility isn't important here.
msg342843 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2019-05-19 12:25
You're right. I am thinking implementing 4 sequence methods
(contains/len/iter/getitem) and set a depraction warning for them. We can
remove this methods in next relase

On Sun, May 19, 2019, 2:37 PM Eric V. Smith <report@bugs.python.org> wrote:

>
> Eric V. Smith <eric@trueblade.com> added the comment:
>
> Unfortunately your change isn't backward compatible with the existing
> (namedtuple) version.
>
> I expect this to fail in the dataclass version:
> >>> finder, name, ispkg = list(pkgutil.iter_modules(None))[0]
>
> And since this is an enhancement, it can only go in to 3.8. And the window
> is closing for that, so it's more likely to be 3.9, if we decide that
> backward compatibility isn't important here.
>
> ----------
> nosy: +eric.smith
> type:  -> enhancement
> versions:  -Python 3.6, Python 3.7
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue36962>
> _______________________________________
>
msg342844 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2019-05-19 12:47
Can you review the PR, i implemented it there.
msg342846 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-05-19 13:20
I'm not sure all of this churn is worth it for an unusual use case that can be satisfied by:

>>> sorted(pkgutil.iter_modules(None), key=lambda x: (x[1], x[2])

or even:

>>> sorted(pkgutil.iter_modules(None), key=operator.itemgetter(1))

(assuming that ispkg doesn't really need to be part of the key)

Before reviewing the patch, I suggest you raise the issue on python-dev and get some additional input.
msg342849 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2019-05-19 13:37
I agree with Eric that this use case can be easily covered by using sorted(..., key=...).

I suggest closing this as 'rejected'.
msg342852 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-05-19 13:50
Dataclasses are even more heavyweight than namedtuples. I am not sure that they should be used in the stdlib now. Especially in this case. I think the common use of iter_modules() is immediate unpacking of the yielded tuple:

    for importer, modname, ispkg in pkgutil.iter_modules():
        ...

Your change breaks it.

I agree with Eric and Berker that this issue should be closed.
msg342867 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2019-05-19 18:23
I agree. Sorry, BTaskaya, but I just don't think the benefit of this change is worth the disruption.
History
Date User Action Args
2022-04-11 14:59:15adminsetgithub: 81143
2019-05-19 18:23:35eric.smithsetstatus: open -> closed
resolution: wont fix
messages: + msg342867

stage: patch review -> resolved
2019-05-19 13:50:07serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg342852
2019-05-19 13:37:57berker.peksagsetnosy: + berker.peksag
messages: + msg342849
2019-05-19 13:20:46eric.smithsetmessages: + msg342846
2019-05-19 12:47:57BTaskayasetmessages: + msg342844
2019-05-19 12:25:25BTaskayasetmessages: + msg342843
2019-05-19 11:37:56eric.smithsetversions: - Python 3.6, Python 3.7
nosy: + eric.smith

messages: + msg342841

type: enhancement
2019-05-19 03:33:00BTaskayasetkeywords: + patch
stage: patch review
pull_requests: + pull_request13325
2019-05-19 03:30:36BTaskayasetmessages: + msg342832
2019-05-19 03:18:37BTaskayacreate