Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typing.NamedTuple does not support mixins #80698

Closed
rectalogic mannequin opened this issue Apr 3, 2019 · 5 comments
Closed

typing.NamedTuple does not support mixins #80698

rectalogic mannequin opened this issue Apr 3, 2019 · 5 comments
Labels
stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error

Comments

@rectalogic
Copy link
Mannequin

rectalogic mannequin commented Apr 3, 2019

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']

    @rectalogic
    Copy link
    Mannequin Author

    rectalogic mannequin commented Apr 3, 2019

    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'

    @rectalogic rectalogic mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Apr 3, 2019
    @ilevkivskyi
    Copy link
    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.

    @serhiy-storchaka
    Copy link
    Member

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

    @bentheiii
    Copy link
    Mannequin

    bentheiii mannequin commented Feb 9, 2021

    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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @AlexWaygood AlexWaygood added topic-typing and removed 3.7 (EOL) end of life labels Apr 13, 2022
    @AlexWaygood
    Copy link
    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
    Labels
    stdlib Python modules in the Lib dir topic-typing type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants