Skip to content

Revert namedtuple member rules #10461

@WSH032

Description

@WSH032

If you have a question about a behavior that you’re seeing in pyright, consider posting to the Pyright discussion forum.

Is your feature request related to a problem? Please describe.

#10351 disallows namedtuple members whose names start with an underscore (_), but consider the following case:

from collections import namedtuple
from typing import TYPE_CHECKING, NamedTuple

if not TYPE_CHECKING:
    # Import from extension module
    Foo = namedtuple("Foo", ["_0", "_1", "_2"], rename=True)

else:
    # Provide type annotations (i.e., `.pyi` files)
    class Foo(NamedTuple):
        _0: int
        _1: int
        _2: int


# work normally
foo = Foo(_0=0, _1=1, _2=2)
print(foo[0])
print(foo._0)

I know that for typing.NamedTuple, member names starting with an underscore are not allowed anyway. However, collections.namedtuple does allow this (with rename=True).

In my use case, I do have a namedtuple with members prefixed by _ (for example, imported from an extension module), and I want to provide type annotations for it (as you would in a .pyi file). This used to work fine, but after #10351, pyright rejects this pattern.

I know I can use pyright: ignore to suppress these errors, but #10351 also affects other IDE features:

# works, but pyright complains: No parameter named "_0"
foo = Foo(_0=0, _1=1, _2=2)

# `x, y, z` should be `int`, but pyright says they are `Never`
match foo:
    case (x, y, z):
        pass

Describe the solution you’d like

Considering that python/typing#1979 is still just a draft, and that members starting with an underscore are valid at runtime, I request that the changes in #10351 be reverted.

Alternatively, please downgrade this to a warning so that it does not affect other IDE features.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions