Skip to content

Refactor typing.NamedTuple #84366

Closed
Closed

Description

@serhiy-storchaka
BPO 40185
Nosy @gvanrossum, @serhiy-storchaka, @ilevkivskyi, @tirkarthi, @juliusgeo
PRs
  • bpo-40185: Refactor typing.NamedTuple #19371
  • bpo-43923: Revert "bpo-40185: Refactor typing.NamedTuple (GH-19371)" #31679
  • bpo-43923: Allow NamedTuple multiple inheritance #31779
  • 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 = <Date 2020-04-08.08:05:31.438>
    created_at = <Date 2020-04-04.21:08:45.136>
    labels = ['type-feature', 'library', '3.9']
    title = 'Refactor typing.NamedTuple'
    updated_at = <Date 2022-03-09.04:16:10.586>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2022-03-09.04:16:10.586>
    actor = 'juliusgeo'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-04-08.08:05:31.438>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2020-04-04.21:08:45.136>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40185
    keywords = ['patch']
    message_count = 5.0
    messages = ['365783', '365788', '365972', '375101', '375105']
    nosy_count = 6.0
    nosy_names = ['gvanrossum', 'python-dev', 'serhiy.storchaka', 'levkivskyi', 'xtreak', 'juliusgeo']
    pr_nums = ['19371', '31679', '31779']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue40185'
    versions = ['Python 3.9']

    Activity

    1. serhiy-storchaka commented on Apr 4, 2020

      @serhiy-storchaka
      MemberAuthor

      typing.NamedTuple is used in two ways.

      1. It is a callable which produces a new namedtuple type.
      2. It can also be used as a base in the class statement for creating a new namedtuple type.

      In both cases it is not a real class. You cannot create an instance of NamedTuple or a subclass of NamedTuple. But it is implemented as a class, and help() shows methods and data descriptors for it, which are useless.

      The proposed PR implements NamedTuple like a function. Implementation of the __mro_entries__ method allows to use it as a base in the class statement.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Apr 4, 2020
    3. serhiy-storchaka commented on Apr 4, 2020

      @serhiy-storchaka
      MemberAuthor

      See also bpo-40187.

    4. serhiy-storchaka commented on Apr 8, 2020

      @serhiy-storchaka
      MemberAuthor

      New changeset a2ec069 by Serhiy Storchaka in branch 'master':
      bpo-40185: Refactor typing.NamedTuple (GH-19371)
      a2ec069

    5. tirkarthi commented on Aug 10, 2020

      @tirkarthi
      Member

      This seems to have caused a test failure in astroid project. I am not sure if the fix needs to be done from their end probably relying on some implementation detail or from the patch itself. Feel free to ignore if it's a third-party only error.

      Reproducer script : /tmp/namedtuple_astroid.py

      from astroid import parse
      
      mod = parse('''from typing import NamedTuple; NamedTuple("A")''')
      print(str(next(mod.body[-1].value.infer())))
      assert str(next(mod.body[-1].value.infer())) != 'Uninferable'

      Before this commit :

      python /tmp/namedtuple_astroid.py
      Instance of typing.NamedTuple

      After this commit :

      python /tmp/namedtuple_astroid.py
      Uninferable
      Traceback (most recent call last):
        File "/tmp/namedtuple_astroid.py", line 4, in <module>
          assert str(next(mod.body[-1].value.infer())) != 'Uninferable'
      AssertionError
    6. serhiy-storchaka commented on Aug 10, 2020

      @serhiy-storchaka
      MemberAuthor
      $ echo 'from typing import NamedTuple; NamedTuple("A")' | ./python -m ast
      Module(
         body=[
            ImportFrom(
               module='typing',
               names=[
                  alias(name='NamedTuple')],
               level=0),
            Expr(
               value=Call(
                  func=Name(id='NamedTuple', ctx=Load()),
                  args=[
                     Constant(value='A')],
                  keywords=[]))],
         type_ignores=[])

      So mod.body[-1].value is a call:

      Call(func=Name(id='NamedTuple', ctx=Load()), args=[Constant(value='A')], keywords=[])
      

      I do not know what Astroid does with this node, but seem the problem is in its infer() method.

    7. transferred this issue fromon Apr 10, 2022
    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

      3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions