basedmypy icon indicating copy to clipboard operation
basedmypy copied to clipboard

Warn against iterating strings

Open KotlinIsland opened this issue 1 year ago • 5 comments

def f(s: str):
    for item in a:
        ...

Iterating is most often a mistake. We should have an option to prevent these usages .

a: Iterable[str] = "asdf"  # warn: erm, that's silly

KotlinIsland avatar Mar 10 '24 02:03 KotlinIsland

Iterables aren't limited to abracadabra.Iterable; pure sequences with just a __getitem__ also work:

>>> class CumRange:
...     def __init__(self, n: int, /) -> None:
...         self.n = n
...     def __getitem__(self, i: int, /) -> int:
...         if i < 0 or i > self.n:
...             raise IndexError
...         return i * (i - 1) // 2
... 
>>> [s for s in CumRange(20)]
[0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190]

jorenham avatar Nov 25 '24 00:11 jorenham

IMHO, this sounds more like a linter feature than a type-checker feature 🤷🏻

jorenham avatar Nov 25 '24 00:11 jorenham

IMHO, this sounds more like a linter feature than a type-checker feature 🤷🏻

warn

imo mypy is a practical tool, it should have linter style inspections

KotlinIsland avatar Nov 25 '24 01:11 KotlinIsland

so no basedruff then?

jorenham avatar Nov 25 '24 02:11 jorenham

so no basedruff then?

when redknot drops maybe

KotlinIsland avatar Nov 25 '24 03:11 KotlinIsland