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

Refactor typing.NamedTuple #84366

Closed
serhiy-storchaka opened this issue Apr 4, 2020 · 5 comments
Closed

Refactor typing.NamedTuple #84366

serhiy-storchaka opened this issue Apr 4, 2020 · 5 comments
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@serhiy-storchaka
Copy link
Member

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

    @serhiy-storchaka
    Copy link
    Member Author

    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.

    @serhiy-storchaka serhiy-storchaka added 3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Apr 4, 2020
    @serhiy-storchaka
    Copy link
    Member Author

    See also bpo-40187.

    @serhiy-storchaka
    Copy link
    Member Author

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

    @tirkarthi
    Copy link
    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

    @serhiy-storchaka
    Copy link
    Member Author

    $ 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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants