Skip to content

functools.reduce over sets becomes unacceptable when used in a larger expression #17694

Closed

Description

@inducer

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.

Activity

  1. added
    bug
    on Aug 20, 2024
  2. added
    topic-type-context
    on Dec 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtopic-type-context

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions