Skip to content

[mypyc] dataclass and NamedTuple fail when typed with np.ndarray #20365

@lgeiger

Description

@lgeiger

Bug Report

Starting with mypyc 1.16.0 typing dataclass or NamedTuple with np.ndarray fails with a KeyError when mypyc compiled. This worked correctly with mypyc 1.15.0 but started failing with 1.16.0 and is still an issue with 1.20.0+dev.4eb6b509d2bdecad2bb638050e052174d1f6b3f8. I haven't checked whether this is only reproducible with np.ndarray. But given that it worked with older versions of mypyc, I expect this mmight be a mypyc issue.

To Reproduce

from dataclasses import dataclass
import numpy

@dataclass
class Foo:
    arr: numpy.ndarray

print(Foo(np.zeros((1, 1, 3), dtype=np.uint8)))

or

from typing import NamedTuple
import numpy as np

class Foo(NamedTuple):
    arr: np.ndarray

print(Foo(np.zeros((1, 1, 3), dtype=np.uint8)))

compiled with

mypyc test.py
python -c "import test"

Expected Behavior

Foo(arr=array([[[0, 0, 0]]], dtype=uint8))

Actual Behavior

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "test.py", line 5, in <module>
    arr: np.ndarray
KeyError: 'ndarray'

Your Environment

  • Mypy version used: mypy-1.20.0+dev.4eb6b509d2bdecad2bb638050e052174d1f6b3f8
  • Mypy command-line flags: mypyc test.py
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12.12

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