Skip to content

Crash on the numpy main branch: AssertionError in check_multi_assignment_from_tuple #21824

Description

@jorenham

Crash Report

On the current numpy main branch (https://github.com/numpy/numpy/tree/87310db7f2a791b4a4e8503d4111359d85052cd4), mypy crashes when a result of np.unique is assigned to a variable named _ (this name matters).

This currently causes mypy_primer to crash when running in optuna, first observed in numpy/numpy#32231 (comment).

Traceback

Traceback (most recent call last):
  File "mypy/checker.py", line 765, in accept
  File "mypy/nodes.py", line 1909, in accept
  File "mypy/checker.py", line 3367, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None)
  File "mypy/checker.py", line 3419, in check_assignment
    self.check_assignment_to_multiple_lvalues(
  File "mypy/checker.py", line 4182, in check_assignment_to_multiple_lvalues
    self.check_multi_assignment(lvalues, rvalue, context, infer_lvalue_type)
  File "mypy/checker.py", line 4260, in check_multi_assignment
    self.check_multi_assignment_from_tuple(
  File "mypy/checker.py", line 4411, in check_multi_assignment_from_tuple
    assert isinstance(reinferred_rvalue_type, TupleType)
AssertionError

Repro

Run mypy --no-incremental --show-traceback t.py on

# t.py
import numpy as np


def f(a: np.ndarray) -> None:
    _, ranks = np.unique(a, return_inverse=True)

with the latest numpy@main installed (e.g. https://github.com/numpy/numpy/tree/87310db7f2a791b4a4e8503d4111359d85052cd4)

Note that with --allow-redefinition this doesn't crash. It also doesn't crash when renaming _ to something else.

This isn't an issue with the latest 2.5.2 NumPy release, where np.unique counts 32 overloads instead of the current 74 overloads (each of which serve a purpose and are handwritten by me, in case you were wondering).

Your Environment

  • Mypy version used: 2.3.0 (compiled: yes), also 2.4.0+dev.5bb72b7
  • Mypy command-line flags: see above
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12, 3.14 (doesn't matter)
  • Operating system and version: linux, but I don't think this matters

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions