@@ -28,7 +28,7 @@ def flatten(x: F32[N, C]) -> F32[N * C]: ...
2828Plain ``int`` values (e.g. ``3``) are also accepted as fixed dimension sizes
2929when subscripting array types.
3030
31- Use ``Value[ "expr"] `` for dimensions that depend on runtime parameters or
31+ Use ``Value( "expr") `` for dimensions that depend on runtime parameters or
3232``self`` attributes rather than previously bound shape names.
3333"""
3434
@@ -190,11 +190,14 @@ def _dim_spec(self) -> DimSpec | None:
190190
191191
192192class _ValueExpr :
193- """Runtime value expression used by ``Value[ ...] `` in shape subscripts."""
193+ """Runtime value expression used by ``Value(" ...") `` in shape subscripts."""
194194
195195 __slots__ = ("expr" , "broadcastable" )
196196
197- def __init__ (self , expr : str , * , broadcastable : bool = False ) -> None :
197+ def __init__ (self , expr : object , * , broadcastable : bool = False ) -> None :
198+ if not isinstance (expr , str ):
199+ msg = "Value(...) expects a string expression"
200+ raise TypeError (msg )
198201 self .expr = expr
199202 self .broadcastable = broadcastable
200203
@@ -209,17 +212,20 @@ def _dim_spec(self) -> ValueDim:
209212
210213 def __repr__ (self ) -> str :
211214 prefix = "+" if self .broadcastable else ""
212- return f'{ prefix } Value[ "{ self .expr } "] '
215+ return f'{ prefix } Value( "{ self .expr } ") '
213216
214217
215218# ---------------------------------------------------------------------------
216219# Pre-defined dimension symbols
217220# ---------------------------------------------------------------------------
218221
219222if tp .TYPE_CHECKING :
220- # Declared as ``Dimension`` so type checkers see the full operator set
221- # (``__add__``, ``__invert__``, ``__pos__``, …) and ``F32[N, C]`` subscripts.
223+
222224 class Value :
225+ def __new__ (cls , expr : str ) -> Dimension : ...
226+
227+ def __pos__ (self ) -> Dimension : ...
228+
223229 @classmethod
224230 def __class_getitem__ (cls , expr : str ) -> Dimension : ...
225231
@@ -236,20 +242,17 @@ def __class_getitem__(cls, expr: str) -> Dimension: ...
236242 __ : Dimension
237243else :
238244
239- class Value :
245+ class Value ( _ValueExpr ) :
240246 """Explicit runtime value expression for shape annotations.
241247
242- Use ``Value[ "size"] `` or ``Value[ "self.some_value + 3"] `` when a shape
248+ Use ``Value( "size") `` or ``Value( "self.some_value + 3") `` when a shape
243249 depends on a runtime parameter rather than a previously bound dimension.
244250 """
245251
246252 __slots__ = ()
247253
248- def __class_getitem__ (cls , expr : object ) -> _ValueExpr : # type: ignore[misc]
249- if not isinstance (expr , str ):
250- msg = "Value[...] expects a string expression"
251- raise TypeError (msg )
252- return _ValueExpr (expr )
254+ def __class_getitem__ (cls , expr : object ) -> Value : # type: ignore[misc]
255+ return cls (expr )
253256
254257 # Common named dimensions
255258 Scalar = Dimension ("" )
0 commit comments