Skip to content

set operations and unpacking #14259

Closed

Description

@Molkree

Bug Report

Error on a valid code.

To Reproduce

sets = map(set[str], ["aaa", "abb", "acc"])
intersection = set[str].intersection(*sets)

mypy Playground

Actual Behavior

Argument 1 to "intersection" of "set" has incompatible type "*map[Set[str]]"; expected "Set[_T]" [arg-type]

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags: --strict (it's present without strict checking as well)
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11

This issue is present for a bunch of other set methods like union and difference (I guess all of them that accept *s: Iterable[T]).
Possibly related #9706.

Activity

  1. Kein commented on Jan 19, 2023

    @Kein

    Same:

    @dataclass
    class StatsEntry:
        val0: int
        val1: int
        val2: int
    
        def __post_init__(self) -> None:
            for field in dcfields(self):
                value = getattr(self, field.name)
                setattr(self, field.name, field.type(value))
    
    
    entry = line.rstrip().split(',')
    stat = StatsEntry(*entry) #type: ignore
    # Argument 1 to "StatsEntry" has incompatible type "*List[str]"; expected "int"  [arg-type]mypy(error)
  2. randolf-scholz commented on Aug 3, 2023

    @randolf-scholz
    Contributor

    Another example (mypy-playground):

    from typing import Mapping, Any
    
    def joint_keys(*dicts: Mapping[str, Any]) -> set[str]:
        """Find joint keys in collection of dictionaries."""
        return set.intersection(*map(set, dicts))    # ✘ [arg-type]
    
    def joint_keys2(*dicts: Mapping[str, Any]) -> set[str]:
        """Find joint keys in collection of dictionaries."""
        dicts_keys: map[set[str]] = map(set, dicts)  # ✔
        return set.intersection(*dicts_keys)         # ✔

    pyright raises no errors, so likely an issue with mypy (and not with stubs).

    EDIT: It is fixed using the --new-type-inference flag on master branch

    https://mypy-play.net/?mypy=master&python=3.11&gist=95765a01a9543509a1c740f4f9448397&flags=new-type-inference

  3. Molkree commented on Dec 22, 2024

    @Molkree
    Author

    @randolf-scholz your example raises no errors since mypy 1.7 (released Nov 10, 2023) where new type inference was enabled by default.

    My original repro raises no errors since mypy 1.14 (released Dec 19, 2024).

  4. Molkree commented on Jan 5, 2025

    @Molkree
    Author

    Original repro raises an error once again in mypy 1.14.1

    Reason is, mypy stopped raising in 1.14.0 due to a bug (inferring Any instead of a more precise type).

    So this issue wasn't really fixed, reopening.

  5. reopened this on Jan 5, 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

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions