{"record":{"id":"53e31b9a00f35f85","repo":"jax-ml/jax","slug":"invalid-argument-to-dtype-x","errorCode":null,"errorMessage":"Invalid argument to dtype: {x}.","messagePattern":"Invalid argument to dtype: (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/dtypes.py","lineNumber":1005,"sourceCode":"\ndef register_type_whose_dtype_should_not_be_canonicalized(typ: type):\n  global _types_whose_dtype_should_not_be_canonicalized\n  _types_whose_dtype_should_not_be_canonicalized += (typ,)\n\ndef dtype(x: Any) -> DType:\n  \"\"\"Return the dtype object for a value or type.\n\n  Python scalars, Python scalar types, NumPy scalar type, NumPy dtypes, and\n  non-JAX arrays will have their dtypes canonicalized.\n\n  Note: this is not the same function as jax.numpy.dtype, which simply aliases\n  numpy.dtype.\"\"\"\n  # TODO(phawkins): in the future, we would like to:\n  # - return the default dtype for Python scalar types and values\n  # - canonicalize NumPy array and scalar types\n  # - return NumPy dtypes as-is, uncanonicalized.\n  if x is None:\n    raise ValueError(f\"Invalid argument to dtype: {x}.\")\n  if isinstance(x, type):\n    # Python scalar types, e.g., int, float\n    if (dt := python_scalar_types_to_dtypes.get(x)) is not None:\n      return canonicalize_dtype(dt)\n\n    # Numpy scalar types, e.g., np.int32, np.float32\n    if _issubclass(x, np.generic):\n      dt = np.dtype(x)\n      return _maybe_canonicalize_explicit_dtype(dt, \"dtype\")\n\n  # Python scalar values, e.g., int(3), float(3.14)\n  elif (dt := python_scalar_types_to_dtypes.get(type(x))) is not None:\n    return canonicalize_dtype(dt)\n  # Jax Arrays, literal arrays, and scalars.\n  # We intentionally do not canonicalize these types: once we've formed an x64\n  # value, that is something we respect irrespective of the x64 mode.\n  elif isinstance(x, _types_whose_dtype_should_not_be_canonicalized):\n    return x.dtype","sourceCodeStart":987,"sourceCodeEnd":1023,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/dtypes.py#L987-L1023","documentation":"jax.dtypes.dtype(x) (used broadly by jnp.dtype, default_int_dtype, supports_inf, etc.) rejects None as an argument because there is no meaningful dtype. The f-string renders the None value.","triggerScenarios":"Passing None directly: jax.dtypes.dtype(None), jnp.zeros(3, dtype=None) paths where None is not treated as 'use default', or a helper that forwards an unset dtype variable into dtype().","commonSituations":"A config object with an optional dtype field (None default) forwarded without a fallback; refactoring where dtype=None previously meant default in old JAX versions but now must be omitted.","solutions":["Guard before calling: dt = dtype(x) if x is not None else jnp.float32","Use jnp.result_type which substitutes default_float_dtype for None","Pass a concrete dtype or omit the argument instead of None"],"exampleFix":"# before\ndt = jax.dtypes.dtype(maybe_none_dtype)\n\n# after\ndt = jax.dtypes.dtype(maybe_none_dtype) if maybe_none_dtype is not None else jnp.result_type(1.0)","handlingStrategy":"validation","validationCode":"if x is None:\n    x = 1.0  # or raise your own descriptive error\ndt = jax.dtypes.dtype(x)","typeGuard":"def is_inferable_dtype_arg(x) -> bool:\n    return x is not None","tryCatchPattern":null,"preventionTips":["Never forward possibly-None dtype variables; apply a default first","Use jnp.result_type for None-means-default semantics"],"tags":["jax","dtype","none-argument"],"backgroundTag":"invalid-argument-type","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}