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): Potential for an unsafe autofix using Any #16491

Closed
Avasam opened this issue Mar 4, 2025 · 4 comments
Closed
Labels
fixes Related to suggested fixes for violations

Comments

@Avasam
Copy link
Contributor

Avasam commented Mar 4, 2025

Summary

Reviving #8820 in light of jaraco/skeleton#165

The original issue didn't mention that an autofix (or editor suggestion) would be possible by typing the attributes as Any. I would of course consider this unsafe as

  1. It doesn't solve the core issue of being untyped
  2. There are rules preventing explicit Any that this would conflict with
  3. Pyright looses on type safety since the type will no longer be Unknown

But, at least as an editor suggestion, it can help mechanically transform the code and get it most of the way there.

from collections import namedtuple

person = namedtuple("Person", ["name", "age"])
from typing import Any, NamedTuple

class person(NamedTuple):
    __name__ = "Person"
    name: Any  # TODO: Type me !
    age: Any  # TODO: Type me !
@MichaReiser
Copy link
Member

I don't know how an editor-only fix would help with the linked issue. It seems that they want an automated fix that they can apply to the entire code base.

Automatically typing everything as Any also defeats the purpose of the rule: It doesn't increase type safety, but it makes the code more verbose.

That's why I'm still leaning towards not offering a fix until we have type inference and can guess the correct types (and e.g. not offer a fix if any or all fields infer as Any)

@MichaReiser MichaReiser added rule Implementing or modifying a lint rule fixes Related to suggested fixes for violations and removed rule Implementing or modifying a lint rule labels Mar 4, 2025
@Avasam
Copy link
Contributor Author

Avasam commented Mar 4, 2025

Might be out of scope, but a language server could have a contextual option to refactor from collections.namedtuple to typing.NamedTuple. Maybe I should ask over at https://github.com/microsoft/pylance-release

Automatically typing everything as Any also defeats the purpose of the rule: It doesn't increase type safety, but it makes the code more verbose.

Correct. I'd also have my reservations.
As I just realized, maybe I'm just not approaching my request correctly. I think what I actually want is this:

Image

ESlint has a concept of "editor only fixes" (aka suggestions), that can never be triggered through cli. It also allows them to display multiple possible fixes for the same issue. But I don't think Ruff does (does it?)

@MichaReiser
Copy link
Member

Refactors is something I want but, but unfortunately, it is probably quiet far in the future :( I did a quick search and couldn't find an issue for refactor support. I'd prefer to close this issue in favor of an explicit ask for refactors (which PYI024 could make use of once implemented)

@Avasam
Copy link
Contributor Author

Avasam commented Mar 4, 2025

Makes sense to me. This can be used as an example of a useful refactor without providing a full autofix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixes Related to suggested fixes for violations
Projects
None yet
Development

No branches or pull requests

2 participants