Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Feature Request: Refactor collections.namedtuple to typing.NamedTuple #7009

Closed
Avasam opened this issue Mar 4, 2025 · 1 comment
Closed
Assignees
Labels
needs repro Issue has not been reproduced yet

Comments

@Avasam
Copy link

Avasam commented Mar 4, 2025

Pyright (by extension, pylance), will raise reportUntypedNamedTuple for function-based namedtuples.

I'm requesting an editor action to easily refactor it into a class-based NamedTuple

Image
Image
Image

This:

from collections import namedtuple

person = namedtuple("Person", ["name", "age"])

Would refactor into that:

from typing import Any, NamedTuple

class person(NamedTuple):
    __name__ = "Person"
    name: Any  # TODO: Type me !
    age: Any  # TODO: Type me !

(note the use of __name__ when the class name and variable name don't match)

This change being tedious (jaraco/skeleton#165) and not autofixable by linters (astral-sh/ruff#16491) is why I'm requesting this as a feature.

This can probably be exampled to other function-to-class transforms as well (enums come to mind)

@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Mar 4, 2025
@rchiodo
Copy link
Contributor

rchiodo commented Mar 5, 2025

Transferring to discussions for up votes

@microsoft microsoft locked and limited conversation to collaborators Mar 5, 2025
@rchiodo rchiodo converted this issue into discussion #7014 Mar 5, 2025

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
needs repro Issue has not been reproduced yet
Projects
None yet
Development

No branches or pull requests

3 participants