{"record":{"id":"fe70fc2282eaa6b1","repo":"pola-rs/polars","slug":"invalid-input-for-shape-shape-r","errorCode":null,"errorMessage":"invalid input for shape: {shape!r}","messagePattern":"invalid input for shape: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/datatypes/classes.py","lineNumber":1267,"sourceCode":"        inner_parsed = polars.datatypes.parse_into_dtype(inner)\n        inner_shape = inner_parsed.shape if isinstance(inner_parsed, Array) else ()\n\n        if isinstance(shape, int):\n            self.inner = inner_parsed\n            self.size = shape\n            self.shape = (shape,) + inner_shape\n\n        elif isinstance(shape, tuple) and isinstance(shape[0], int):  # type: ignore[redundant-expr]\n            if len(shape) > 1:\n                inner_parsed = Array(inner_parsed, shape[1:])\n\n            self.inner = inner_parsed\n            self.size = shape[0]\n            self.shape = shape + inner_shape\n\n        else:\n            msg = f\"invalid input for shape: {shape!r}\"\n            raise TypeError(msg)\n\n    def __eq__(self, other: PolarsDataType) -> bool:  # type: ignore[override]\n        # This equality check allows comparison of type classes and type instances.\n        # If a parent type is not specific about its inner type, we infer it as equal:\n        # > array[i64] == array[i64] -> True\n        # > array[i64] == array[f32] -> False\n        # > array[i64] == array      -> True\n\n        # allow comparing object instances to class\n        if type(other) is DataTypeClass and issubclass(other, Array):\n            return True\n        elif isinstance(other, Array):\n            if self.shape != other.shape:\n                return False\n            else:\n                return self.inner == other.inner\n        else:\n            return False","sourceCodeStart":1249,"sourceCodeEnd":1285,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/datatypes/classes.py#L1249-L1285","documentation":"Raised by a Polars datatype class (e.g. Array/List) when the `shape` argument passed to its constructor is not a valid shape tuple. Valid shapes are a single int or a tuple of ints; anything else (str, float, nested lists, empty tuple, None) is rejected. This is an eager constructor-level input validation.","triggerScenarios":"Calling polars.Array(dtype, shape) or a similar nested-type constructor with shape given as a string ('3'), a float (3.0), a list ([3]), None, a tuple of non-ints, or an empty tuple. Also triggered when shorthands like Array(Int64, (2, 'x')) are used.","commonSituations":"Hand-written schemas where shape came from a config string like '4' instead of int 4; building schemas programmatically and accidentally passing a list instead of tuple; parsing a dtype string back into a constructor call.","solutions":["Pass shape as a plain int or a tuple of positive ints, e.g. polars.Array(pl.Int64, (2, 3)).","Convert config/parsed values: shape = tuple(int(s) for s in str_shape.split(',')) or int(value).","Wrap a list as tuple(shape) before constructing the dtype."],"exampleFix":"// before\npl.Array(pl.Int64, [2, 3])       # list\npl.Array(pl.Int64, '3')          # string\n// after\npl.Array(pl.Int64, (2, 3))       # tuple of ints\npl.Array(pl.Int64, 3)            # single int","handlingStrategy":"validation","validationCode":"def is_valid_shape(shape) -> bool:\n    if isinstance(shape, int):\n        return shape > 0\n    return (\n        isinstance(shape, tuple)\n        and len(shape) > 0\n        and all(isinstance(s, int) and s > 0 for s in shape)\n    )\n\nshape = tuple(shape) if isinstance(shape, list) else shape\nif not is_valid_shape(shape):\n    raise ValueError(f\"bad dtype shape: {shape!r}\")","typeGuard":"def is_shape(x: object) -> TypeGuard[tuple[int, ...]]:\n    return isinstance(x, tuple) and len(x) > 0 and all(isinstance(i, int) for i in x)","tryCatchPattern":null,"preventionTips":["Always pass tuples of ints (or a bare int) for Array/List shapes","Coerce config strings/lists with int() and tuple() at the schema boundary","Add a unit test that constructs every dtype in your schema"],"tags":["python","polars","datatypes","invalid-argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}