Skip to content

Raise TypeError for mapping-shaped cell_data instead of KeyError - #1575

Open
barlowa124 wants to merge 1 commit into
nschloe:mainfrom
barlowa124:cell-data-dict-shape-1520
Open

barlowa124 wants to merge 1 commit into
nschloe:mainfrom
barlowa124:cell-data-dict-shape-1520

Conversation

@barlowa124

Copy link
Copy Markdown

Fixes #1520. meshio.Mesh(points, cells, cell_data=...) crashed with KeyError: 0 when a cell_data value was a nested dict ({cell_type: {name: data}}) instead of the documented {name: [per-block arrays]} shape.

The validation loop checks len(data) != len(cells) and then indexes data[k]. A dict passes the length check whenever its key count happens to equal the block count, so the first subscript raises KeyError with no hint about the contract. The issue reporter hit exactly this with {"triangle": {"value": arr}} on a single triangle.

This adds a Mapping check at the top of the loop. That input now raises

TypeError: cell_data['triangle'] is a dict; cell_data must map a field
name to a list of arrays, one array per cell block.

TypeError because the signature already declares dict[str, list[ArrayLike]], so a mapping value violates the declared contract. The neighboring shape mismatches remain ValueError, matching the file's convention for value-level problems.

Regression test in tests/test_mesh.py covers the reported shape. List and ndarray values are unaffected. pytest tests/test_mesh.py reports 6 passed, 1 skipped.

Mesh.__init__ indexes each cell_data value by position, so a nested
{cell type: {name: data}} dict crashed with KeyError: 0 when its
length happened to match the cell-block count. Reject mappings up
front with a message describing the expected
{name: [per-block arrays]} shape.

Fixes nschloe#1520.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]KeyError: 0 in meshio._mesh.__init__ when using cell_data (meshio 4.4.6, 5.2.0, 5.3.5 on Python 3.10/Windows with Numpy 1.25.x)

1 participant