mypy
mypy copied to clipboard
`functools.reduce` over sets becomes unacceptable when used in a larger expression
Reproducer:
from typing import Collection
from functools import reduce
def _pull_out_loop_nest(
loop_nests: list[frozenset[str]],
inames_to_pull_out: frozenset[str]
) -> None:
emptyset: frozenset[str] = frozenset()
# OK
reduce(frozenset.union, loop_nests[:-1], emptyset)
# Error
inames_to_pull_out - reduce(frozenset.union, loop_nests[:-1], emptyset)
Here is what mypy says as of 1.11.1:
mypybug.py:15: error: Argument 1 to "reduce" has incompatible type "Callable[[frozenset[_T_co], VarArg(Iterable[_S])], frozenset[_T_co | _S]]"; expected "Callable[[AbstractSet[str], frozenset[str]], AbstractSet[str]]" [arg-type]
This seems spurious: It's not clear to me why this should be acceptable as a stand-alone expression, but not within a larger expression. Pyright (1.1.376) also seems to like this code just fine.
See also #17693.