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

Error for unnecessary "= ..." #98

Open
Akuli opened this issue Jan 17, 2022 · 5 comments
Open

Error for unnecessary "= ..." #98

Akuli opened this issue Jan 17, 2022 · 5 comments

Comments

@Akuli
Copy link
Collaborator

Akuli commented Jan 17, 2022

This was previously Y0092, and it was removed because it had problems: #88

@JelleZijlstra
Copy link
Collaborator

The trouble with this check is that flake8 produces undefined name errors if you use a name defined with just x: T elsewhere in the stub. That's correct in general but causes some false positives in typeshed.

Two possible solutions:

  • Track in flake8-pyi whether a name has been used elsewhere in the stub, and emit an error only when it's not used elsewhere. The machinery @AlexWaygood added for detecting unused typevars could be reused for this.
  • Somehow teach flake8 to not emit errors in this case. This allows us to always remove = ..., which is more consistent. One approach could be to disable the standard flake8 undefined name error and add our own.

@AlexWaygood
Copy link
Collaborator

Following #364, flake8-with-flake8-pyi-installed no longer emits errors for this code:

x: int
y = x

That means we can now revisit this issue.

@Avasam
Copy link
Contributor

Avasam commented Nov 22, 2023

I just saw astral-sh/ruff#8818 and I've been wondering, should this apply to enums? Given they can be dynamically generated. Maybe that's not a use-case we'd want to support.

@AlexWaygood
Copy link
Collaborator

I just saw astral-sh/ruff#8818 and I've been wondering, should this apply to enums? Given they can be dynamically generated. Maybe that's not a use-case we'd want to support.

I don't see any particular reason that this hypothetical check should exclude enums — the following two enums in a stub file should (I think) be interpreted the same way by a type checker, but the second one is more explicit:

from _typeshed import Incomplete

class Foo(Enum):
    X = ...
    Y = ...

class Bar(Enum):
    X: Incomplete
    Y: Incomplete

If code generators are currently generating Foo, instead of Bar, we should fix the code generators ;)

That said, I think the ruff maintainers are correct to say in astral-sh/ruff#8818 that PIE796 probably shouldn't emit an error on Foo in a stub file.

@Avasam
Copy link
Contributor

Avasam commented Nov 22, 2023

I thought it was completely breaking type-safety (at least in pyright), but I just forgot to write it in a .pyi file, not .py (since in a .py file the vlaue needs to be initialized X: Any = ...)

Not a problematic edge-case then.

Details

py
image

pyi
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants