wemake-python-styleguide icon indicating copy to clipboard operation
wemake-python-styleguide copied to clipboard

WPS356 (unnecessary iterable unpacking) incompatible with Variadic Generics

Open aaronmondal opened this issue 2 years ago • 3 comments

What's wrong

The following is not allowed by WPS356:

from typing import Generic, Self, Tuple, TypeVar, TypeVarTuple


DType = TypeVar('DType')
Shape = TypeVarTuple('Shape')


class Array(Generic[DType, *Shape]):
    ...
    
    def resize_to_shape(
        self: Self,
        shape: Tuple[*Shape],  # Error.
    ) -> Self

How it should be

According to https://peps.python.org/pep-0646/#type-variable-tuples-must-always-be-unpacked it is a hard requirement to unpack Shape in this case. Since wemake doesn't cover typing, it should ignore this case entirely.

Flake8 version and plugins

Python 3.11 Flake8 6.0.0 with all plugins from wemake-python-styleguide 0.18.0

pip information

pip 23.2.1 (python 3.11)

OS information

Gentoo

aaronmondal avatar Jul 28 '23 20:07 aaronmondal

Yes, we should ignore this case.

  1. We can ignore all cases in annotations
  2. What to do with type aliases on module and class levels?

sobolevn avatar Jul 29 '23 07:07 sobolevn

  1. What to do with type aliases on module and class levels?

This seems like a tricky issue in general. Just from playing around with things for a short tim I've encountered quite a few cases that confuse wemake. For instance, this is flagged (which would be fixed by 1):

from typing import Literal

Height: Literal[128] = 128  # Triggers WPS432 avoid magic number

But I assume the following doesn't count as "annotation":

from typing import Literal

SymbolicOne = Literal[1]  # Also triggers WPS432.

I'm just guessing here, maybe letting wemake know about Generic Aliases could be helpful? Giving wemake deeper knowledge about types just so that it can ignore them seems like a lot of effort though :sweat_smile:

Maybe a config flag for code that disables a certain set of warnings for generic code could be an option? Certainly not an optimal solution, but seems a lot easier to implement than knowledge about types.

aaronmondal avatar Jul 29 '23 20:07 aaronmondal

One way to allow *TVT is to allow it in SomeType[*TVT], but not in [*TVT] I will comment on Literal later, I am focusing on one task per commit right now :)

sobolevn avatar Mar 31 '24 12:03 sobolevn