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

Remove redundant inheritances from Iterator in builtins #12851

Merged
merged 3 commits into from
Oct 21, 2024

Conversation

tungol
Copy link
Contributor

@tungol tungol commented Oct 18, 2024

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

previous: #12813 #12814 #12816 #12827

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

This comment has been minimized.

@tungol tungol marked this pull request as draft October 18, 2024 16:50
@tungol
Copy link
Contributor Author

tungol commented Oct 18, 2024

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 (#12816 and #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
Copy link
Contributor Author

tungol commented Oct 19, 2024

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.

@hauntsaninja
Copy link
Collaborator

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

@hauntsaninja
Copy link
Collaborator

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

This comment has been minimized.

Copy link
Contributor

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]

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

Successfully merging this pull request may close these issues.

4 participants