Skip to content

[Code scan] DataType crashes for tuple dtypes despite documenting them #989

Description

@njzjz

This issue is part of a Codex global repository code scan.

DataType.__init__() documents dtype as "type or tuple[type]", and check() uses isinstance(data, self.dtype), which supports tuple dtypes. However, error and repr formatting use self.dtype.__name__, which raises AttributeError when dtype is a tuple.

Affected code:

dtype : type or tuple[type]
data type, e.g. np.ndarray
shape : tuple[int], optional

dpdata/dpdata/data_type.py

Lines 98 to 101 in a7a50bf

return (
f"DataType(name='{self.name}', dtype={self.dtype.__name__}, "
f"shape={self.shape}, required={self.required}, "
f"deepmd_name='{self.deepmd_name}')"

dpdata/dpdata/data_type.py

Lines 146 to 149 in a7a50bf

elif not isinstance(data, self.dtype):
raise DataError(
f"Type of {self.name} is {type(data).__name__}, but expected {self.dtype.__name__}"
)

Minimal reproducer:

from dpdata.data_type import DataType

print(repr(DataType("x", (list, tuple))))

Current behavior:

AttributeError: 'tuple' object has no attribute '__name__'

The same issue occurs in check() when reporting a type mismatch. Either tuple dtypes should be supported in formatting, or the documented accepted type should be narrowed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions