flake8-pyi icon indicating copy to clipboard operation
flake8-pyi copied to clipboard

Error for unnecessary "= ..."

Open Akuli opened this issue 3 years ago • 5 comments

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

Akuli avatar Jan 17 '22 17:01 Akuli

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.

JelleZijlstra avatar Jan 17 '22 17:01 JelleZijlstra

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.

AlexWaygood avatar May 01 '23 22:05 AlexWaygood

I just saw https://github.com/astral-sh/ruff/issues/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.

Avasam avatar Nov 22 '23 17:11 Avasam

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 https://github.com/astral-sh/ruff/issues/8818 that PIE796 probably shouldn't emit an error on Foo in a stub file.

AlexWaygood avatar Nov 22 '23 18:11 AlexWaygood

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

Avasam avatar Nov 22 '23 18:11 Avasam