Skip to content

Using kwargs with typing.NamedTuple not recognized #1259

Open
@rkoeninger

Description

@rkoeninger

This is being reported in response to an issue with pylint: pylint-dev/pylint#4431

This issue may also be related to:
See #109
See #421

Steps to reproduce

  1. Declare a named tuple using kwargs syntax: Point = NamedTuple("Point", x=float, y=float)
  2. Try to create an instance of this type: p = Point(1, 2)

The kwargs behavior is noted since 3.5 in the source code for CPython here:

https://github.com/python/cpython/blob/3.5/Lib/typing.py#L2176-L2213

Current behavior

astroid will not identify Point as callable.

This causes pylint to report a not-callable error for the application of Point.

Expected behavior

The Point type from the Steps to reproduce should be identified as a callable object.

python -c "from astroid import __pkginfo__; print(__pkginfo__.version)" output

2.8.5

Visible in current version:

https://github.com/PyCQA/astroid/blob/b07eb62466ae54bb0209d9253b7018a5d4c764e2/astroid/brain/brain_namedtuple_enum.py#L521-L522

The number of expected node.args is exactly 2 - one for the name of the named tuple type and one for the list of members. This line will probably have to be replaced with something like len(node.args) >= 2 or further examine ast for list of tuples vs kwargs syntax.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions