jax-ml/jax · error · AttributeError

{self!r} has no `shape`.

Error message

{self!r} has no `shape`.

What it means

TransformedRef.shape raises AttributeError when the underlying transformed type has no shape attribute (e.g. the ref's aval isn't a ShapedArray after transforms). The 'from None' suppresses the original error chain for a cleaner message.

Source

Thrown at jax/_src/state/types.py:335

      if isinstance(ref, TransformedRef):
        return ref.type
      elif type(ref) in core.pytype_aval_mappings:
        return core.typeof(ref)
      else:
        return ref

    if self.multiref:
      ref_ty = tuple(_type(r) for r in self.ref)
      return cast(MultiRefTransform, self.transforms[0]).transform_types(ref_ty)
    ref_ty = _type(self.ref)
    for t in self.transforms:
      ref_ty = t.transform_type(ref_ty)
    return ref_ty

  @property
  def shape(self) -> tuple[int | Array, ...]:
    if not hasattr(self.type, "shape"):
      raise AttributeError(f"{self!r} has no `shape`.") from None
    return self.type.shape

  @property
  def dtype(self):
    if not hasattr(self.type, "dtype"):
      raise AttributeError(f"{self!r} has no `dtype`.") from None
    return self.type.dtype

  ndim = property(lambda self: len(self.shape))
  size = property(lambda self: math.prod(self.shape))
  T = property(lambda self: self.transpose(tuple(reversed(range(self.ndim)))))

  @property
  def at(self) -> RefIndexer:
    return RefIndexer(self)

  def bitcast(self, dtype):
    if self.is_dynamic_size:

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Guard with hasattr(ref.type, 'shape') or getattr before access
  2. Check the ref's aval type before using shape-dependent properties
  3. Restructure so only array-backed refs reach this code path

Example fix

// before
s = ref.shape
// after
s = ref.shape if hasattr(ref.type, "shape") else None
Defensive patterns

Strategy: type-guard

Type guard

def has_shape(ref):
    return hasattr(ref.type, "shape") if hasattr(ref, "type") else hasattr(ref, "shape")

Prevention

When it happens

Trigger: Accessing .shape (or .ndim/.size which derive from it) on a ref whose transformed aval lacks a shape, such as certain memory-space or token-like avals.

Common situations: Generic utility code that probes .shape on arbitrary ref-like objects; refs whose inner aval is not an array.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/1d56bfdb5f492270. Report an issue: GitHub.