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 eric.snow
Recipients amaury.forgeotdarc, daniel.urban, eric.araujo, eric.snow, pitrou, pwaller, python-dev, rhettinger, santoso.wijaya, thead, zuo
Date 2013-06-25.17:31:09
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1372181470.03.0.616740640058.issue7796@psf.upfronthosting.co.za>
In-reply-to
Content
A NamedTuple ABC doesn't have to define any API (so that part could wait?)[1].  I see it as most useful for isinstance checks.  Here's a solution along those lines:


class NamedTuple(Sequence):
    @classmethod
    def __subclasshook__(cls, C):
        if cls is NamedTuple:
            if any("_fields" in B.__dict__ for B in C.__mro__):
                return True
        return NotImplemented

def namedtuple(...):
    ...
    NamedTuple.register(result)
    return result

(or include NamedTuple in the bases in the template)

For structseq support:

class NamedTuple(Sequence):
    @classmethod
    def __subclasshook__(cls, C):
        if cls is NamedTuple:
            if any("_fields" in B.__dict__ or  # for namedtuple
                   "n_fields" in B.__dict__  # for structseq
                   for B in C.__mro__):
                return True
        return NotImplemented


[1] I agree there is still a problem if we define an ABC here with no API and then add some later.  Then classes that inherit from NamedTuple would break later when we add an API (just `_fields`?).  Not sure how much that is a concern though.  Then again we could just close out issue1820 and actually establish the API.
History
Date User Action Args
2013-06-25 17:31:10eric.snowsetrecipients: + eric.snow, rhettinger, amaury.forgeotdarc, pitrou, thead, eric.araujo, zuo, pwaller, daniel.urban, santoso.wijaya, python-dev
2013-06-25 17:31:10eric.snowsetmessageid: <1372181470.03.0.616740640058.issue7796@psf.upfronthosting.co.za>
2013-06-25 17:31:10eric.snowlinkissue7796 messages
2013-06-25 17:31:09eric.snowcreate