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.

Author pablogsal
Recipients Mark.Shannon, brandtbucher, gvanrossum, pablogsal, quentel
Date 2021-07-26.19:01:02
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1627326062.77.0.697417745358.issue44741@roundup.psfhosted.org>
In-reply-to
Content
I don't think there is anything technically wrong here. The Seq class is relying on the legacy version of the iterator protocol, which specifies that you need to raise IndexError there to stop the iteration.

For instance, this version works:

import collections.abc

class Seq(collections.abc.Sequence):
    def __getitem__(self, i):
        if i >= len(self):
            raise IndexError
        return i
    def __len__(self):
        return 42

match Seq():
    case [x, *w, y]:
        z = 0
        print(z)


Also, one could argue that Seq has exactly the same problem all over the language. For instance

>> list(Seq())

will hang

>> [x,*y] = Seq()

will hang

and so on and so forth. So, if I am a user and I am trying to **predict** how this would behave based on these two experiments my conclusion would be:

"Anything that tries to iterate on this thing will hang"

I think that whatever we do, we should make it *predictable* and consistency across the language, and IMHO only fixing it in pattern matching doesn't make it predictable. Being said that, I don't think these use cases justifies a major overhaul of how this behaves everywhere.
History
Date User Action Args
2021-07-26 19:01:02pablogsalsetrecipients: + pablogsal, gvanrossum, Mark.Shannon, quentel, brandtbucher
2021-07-26 19:01:02pablogsalsetmessageid: <1627326062.77.0.697417745358.issue44741@roundup.psfhosted.org>
2021-07-26 19:01:02pablogsallinkissue44741 messages
2021-07-26 19:01:02pablogsalcreate