typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

Remove redundant inheritances from Iterator in builtins

Open tungol opened this issue 1 year ago • 6 comments

This is the last batch of them; I saved the builtins for last.

previous: https://github.com/python/typeshed/pull/12813 https://github.com/python/typeshed/pull/12814 https://github.com/python/typeshed/pull/12816 https://github.com/python/typeshed/pull/12827

The unrelated extra newline in this MR was black's idea, not mine, to be clear.

tungol avatar Oct 18 '24 16:10 tungol

Diff from mypy_primer, showing the effect of this PR on open source code:

psycopg (https://github.com/psycopg/psycopg)
+ tests/types/test_datetime.py:785: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/types/test_datetime.py:807: error: Unused "type: ignore" comment  [unused-ignore]

ibis (https://github.com/ibis-project/ibis)
- ibis/expr/types/numeric.py:1223: error: Argument 1 to "cases" of "Value" has incompatible type "*enumerate[str]"; expected "tuple[Value, Value]"  [arg-type]

spark (https://github.com/apache/spark)
+ python/pyspark/pandas/frame.py:7648: error: "object" object is not iterable  [misc]
+ python/pyspark/pandas/frame.py:7653: error: "None" has no attribute "__iter__" (not iterable)  [attr-defined]

spack (https://github.com/spack/spack)
+ lib/spack/spack/util/spack_yaml.py:490: error: "object" has no attribute "__iter__"; maybe "__dir__" or "__str__"? (not iterable)  [attr-defined]

optuna (https://github.com/optuna/optuna)
+ optuna/study/_dataframe.py:64: error: "object" has no attribute "__iter__"; maybe "__dir__" or "__str__"? (not iterable)  [attr-defined]

github-actions[bot] avatar Oct 18 '24 16:10 github-actions[bot]

Three of the hits look like the ternary join issue again:

Here's Spack:

def anchorify(data: dict | list) -> None:
    for key, value in data.items() if isinstance(data, dict) else enumerate(data):
        pass

Optuna:

def foo(values: list[str], names: list[str] | None):
    iterator = enumerate(values) if names is None else zip(names, values)
    for item in iterator:
        pass

PySpark:

from typing import Any

def drop(labels: Any):
    if labels is not None:
        return
    foo = []
    cols, labels = zip(*foo) if len(foo) > 0 else ([], [])
    [label for label in labels]

Ibis and psycopg show errors going away, and I think represent mypy inferring Any where it didn't before.

The error from Ibis going away looks like this:

from collections.abc import Iterable

class Value:
    def cases(self, *branches: tuple[Value, Value]) -> Value: ...

    def label(self, labels: Iterable[str]) -> Value:
        return self.cases(*enumerate(labels))

Take away the type: ignore from psycopg and the diff is:

< tests/types/test_datetime.py:785: error: Argument 1 to "time" has incompatible type "*map[int]"; expected "tzinfo | None"  [arg-type]
< tests/types/test_datetime.py:807: error: Argument 1 to "datetime" has incompatible type "*map[int]"; expected "tzinfo | None"  [arg-type]

Which comes from something like this:

import datetime

def as_time(s):
    datetime.time(*map(int, s.split(",")))

def as_naive_dt(s):
    datetime.datetime(*map(int, s.split(",")))

Following on from the revert and discussion of the related itertools MR (https://github.com/python/typeshed/pull/12816 and https://github.com/python/typeshed/pull/12853 ), I believe that all of these are blocking. I'll close out this MR in a day or two if nobody else has anything to say about it.

tungol avatar Oct 18 '24 19:10 tungol

Moving this to open since the revert of #12816 wasn't necessary. I'm pretty sure the three new messages in mypy-primer will go away for mypy 1.12.

tungol avatar Oct 19 '24 00:10 tungol

Let's update typeshed to latest mypy to confirm before merging this!

hauntsaninja avatar Oct 19 '24 00:10 hauntsaninja

Also this class of change probably makes type checkers slower, since structural checks are more expensive than nominal checks

hauntsaninja avatar Oct 19 '24 00:10 hauntsaninja

Diff from mypy_primer, showing the effect of this PR on open source code:

psycopg (https://github.com/psycopg/psycopg)
+ tests/types/test_datetime.py:785: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/types/test_datetime.py:807: error: Unused "type: ignore" comment  [unused-ignore]

ibis (https://github.com/ibis-project/ibis)
- ibis/expr/types/numeric.py:1223: error: Argument 1 to "cases" of "Value" has incompatible type "*enumerate[str]"; expected "tuple[Value, Value]"  [arg-type]

github-actions[bot] avatar Oct 19 '24 15:10 github-actions[bot]

Diff from mypy_primer, showing the effect of this PR on open source code:

psycopg (https://github.com/psycopg/psycopg)
+ tests/types/test_datetime.py:785: error: Unused "type: ignore" comment  [unused-ignore]
+ tests/types/test_datetime.py:807: error: Unused "type: ignore" comment  [unused-ignore]

ibis (https://github.com/ibis-project/ibis)
- ibis/expr/types/numeric.py:1223: error: Argument 1 to "cases" of "Value" has incompatible type "*enumerate[str]"; expected "tuple[Value, Value]"  [arg-type]

github-actions[bot] avatar Oct 21 '24 12:10 github-actions[bot]