Skip to content

typing.NamedTuple does not support mixins #80698

Closed

Description

@rectalogic
mannequin
BPO 36517
Nosy @rhettinger, @serhiy-storchaka, @ilevkivskyi, @bentheiii
PRs
  • bpo-36517: Raise error on multiple inheritance with NamedTuple #19363
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2019-04-03.13:07:30.022>
    labels = ['3.7', 'type-bug', 'library']
    title = 'typing.NamedTuple does not support mixins'
    updated_at = <Date 2021-02-09.09:57:06.552>
    user = 'https://bugs.python.org/rectalogic'

    bugs.python.org fields:

    activity = <Date 2021-02-09.09:57:06.552>
    actor = 'avrahami.ben'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2019-04-03.13:07:30.022>
    creator = 'rectalogic'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 36517
    keywords = ['patch']
    message_count = 4.0
    messages = ['339390', '339546', '365771', '386701']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'serhiy.storchaka', 'levkivskyi', 'rectalogic', 'avrahami.ben']
    pr_nums = ['19363']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue36517'
    versions = ['Python 3.7']

    Activity

    1. rectalogic commented on Apr 3, 2019

      rectalogicmannequin
      MannequinAuthor

      Subclassing typing.NamedTuple an inheriting from a mixin class does not work. It does work for collections.namedtuple, and can be worked around by modifying typing.NamedTupleMeta:

      >>> import collections
      >>> import typing
      >>>
      >>>
      >>> class Mixin:
      ...     def mixin(self):
      ...         return "mixin"
      ...
      >>>
      >>> class CollectionsNamedTuple(Mixin, collections.namedtuple('CollectionsNamedTuple', [
      ...     "a",
      ...     "b",
      ... ])):
      ...     pass
      ...
      >>>
      >>> class TypingNamedTuple(Mixin, typing.NamedTuple):
      ...     a: str
      ...     b: str
      ...
      >>>
      >>> class NamedTupleMeta(typing.NamedTupleMeta):
      ...     def __new__(cls, typename, bases, ns):
      ...         cls_obj = super().__new__(cls, typename + '_nm_base', bases, ns)
      ...         bases = bases + (cls_obj,)
      ...         return type(typename, bases, {})
      ...
      >>>
      >>> class FixedTypingNamedTuple(Mixin, metaclass=NamedTupleMeta):
      ...     a: str
      ...     b: str
      ...
      >>>
      >>> cnt = CollectionsNamedTuple("av", "bv")
      >>> tnt = TypingNamedTuple("av", "bv")
      >>> ftnt = FixedTypingNamedTuple("av", "bv")
      >>>
      >>> cnt.mixin()
      'mixin'
      >>> ftnt.mixin()
      'mixin'
      >>> tnt.mixin()
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      AttributeError: 'TypingNamedTuple' object has no attribute 'mixin'
    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Apr 3, 2019
    3. ilevkivskyi commented on Apr 6, 2019

      @ilevkivskyi
      Member

      Hm, it looks like we can support this. I however don't think the proposed "patch" is the right way to fix it, since this makes the implementation with and without a mixin quite different.

      Also I am not sure I will have time to work on this in the nearest month.

    4. serhiy-storchaka commented on Apr 4, 2020

      @serhiy-storchaka
      Member

      New changeset a94e627 by Serhiy Storchaka in branch 'master':
      bpo-36517: Raise error on multiple inheritance with NamedTuple (GH-19363)
      a94e627

    5. bentheiii commented on Feb 9, 2021

      bentheiiimannequin
      Mannequin

      The patch PR blocks out a useful idiom: generic Nametuple

      >>> class LLNode(NamedTuple, Generic[T]):
      ...     value :T
      ...     next: Optional[LLNode[T]]

      I put forward that, at the least, NamedTuple should accept do-nothing bases like Generic.

    6. transferred this issue fromon Apr 10, 2022
    7. AlexWaygood commented on Apr 10, 2023

      @AlexWaygood
      Member

      Generic NamedTuples are supported on 3.11+, and the decision has been taken to prohibit mixins in other situations. I don't think we'll be changing that now. Closing as completed.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions