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

collections-named-tuple (PYI024): Documented fix is not equivalent #16492

Open
Avasam opened this issue Mar 4, 2025 · 3 comments
Open

collections-named-tuple (PYI024): Documented fix is not equivalent #16492

Avasam opened this issue Mar 4, 2025 · 3 comments
Labels
documentation Improvements or additions to documentation help wanted Contributions especially welcome

Comments

@Avasam
Copy link
Contributor

Avasam commented Mar 4, 2025

Summary

From the doc:

Example

from collections import namedtuple

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

Use instead:

from typing import NamedTuple

class Person(NamedTuple):
    name: str
    age: int

But that's not equivalent! Since the variable name person differs from the class name Person.

For this to be equivalent:

from collections import namedtuple

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

This would be used:

from typing import NamedTuple

class person(NamedTuple):
    __name__ = "Person"
    name: str
    age: int

class Rect(NamedTuple):
    x: int
    y: int

or

from typing import NamedTuple

class Person(NamedTuple):
    name: str
    age: int
person = Person  # old name

class Rect(NamedTuple):
    x: int
    y: int

I wouldn't recommend that usage. But backwards compatibility is important in libraries, where NamedTuples matter more.

@MichaReiser
Copy link
Member

It took me a while to understand what the difference is because I thought it was some subtlety with named tuples when all it is is just the naming of the person variable.

I suggest that we simply change the example in the docs to:

from collections import namedtuple

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

The rule doesn't have an auto fix and library authors should be aware that changing the casing of an exported variable (if it is exported) is a breaking change.

@MichaReiser MichaReiser added documentation Improvements or additions to documentation help wanted Contributions especially welcome labels Mar 4, 2025
@Avasam
Copy link
Contributor Author

Avasam commented Mar 4, 2025

(I'm going to bed but doc changes are easy, no need for rust knowledge and I can do it tomorrow if no one completed it already, I just needed a decision)

@MichaReiser
Copy link
Member

Thank you and thanks for reporting the issue. We could consider adding a snapshot test for this with a comment. Just in case we ever add a fix for this (as you requested in the other issue). This seems easy to overlook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation help wanted Contributions especially welcome
Projects
None yet
Development

No branches or pull requests

2 participants