@@ -919,7 +919,7 @@ def _translate_named_indices(
919
919
return tuple (domain_slice )
920
920
921
921
def field_getitem (self , named_indices : NamedFieldIndices ) -> Any :
922
- return self ._ndarrayfield [self ._translate_named_indices (named_indices )]
922
+ return self ._ndarrayfield [self ._translate_named_indices (named_indices )]. as_scalar ()
923
923
924
924
def field_setitem (self , named_indices : NamedFieldIndices , value : Any ):
925
925
if common .is_mutable_field (self ._ndarrayfield ):
@@ -1040,6 +1040,7 @@ class IndexField(common.Field):
1040
1040
"""
1041
1041
1042
1042
_dimension : common .Dimension
1043
+ _cur_index : Optional [core_defs .IntegralScalar ] = None
1043
1044
1044
1045
@property
1045
1046
def __gt_domain__ (self ) -> common .Domain :
@@ -1055,7 +1056,10 @@ def __gt_builtin_func__(func: Callable, /) -> NoReturn: # type: ignore[override
1055
1056
1056
1057
@property
1057
1058
def domain (self ) -> common .Domain :
1058
- return common .Domain ((self ._dimension , common .UnitRange .infinite ()))
1059
+ if self ._cur_index is None :
1060
+ return common .Domain ((self ._dimension , common .UnitRange .infinite ()))
1061
+ else :
1062
+ return common .Domain ()
1059
1063
1060
1064
@property
1061
1065
def codomain (self ) -> type [core_defs .int32 ]:
@@ -1072,16 +1076,24 @@ def ndarray(self) -> core_defs.NDArrayObject:
1072
1076
def asnumpy (self ) -> np .ndarray :
1073
1077
raise NotImplementedError ()
1074
1078
1079
+ def as_scalar (self ) -> core_defs .IntegralScalar :
1080
+ if self .domain .ndim != 0 :
1081
+ raise ValueError (
1082
+ "'as_scalar' is only valid on 0-dimensional 'Field's, got a {self.domain.ndim}-dimensional 'Field'."
1083
+ )
1084
+ assert self ._cur_index is not None
1085
+ return self ._cur_index
1086
+
1075
1087
def remap (self , index_field : common .ConnectivityField | fbuiltins .FieldOffset ) -> common .Field :
1076
1088
# TODO can be implemented by constructing and ndarray (but do we know of which kind?)
1077
1089
raise NotImplementedError ()
1078
1090
1079
- def restrict (self , item : common .AnyIndexSpec ) -> common .Field | core_defs . int32 :
1091
+ def restrict (self , item : common .AnyIndexSpec ) -> common .Field :
1080
1092
if common .is_absolute_index_sequence (item ) and all (common .is_named_index (e ) for e in item ): # type: ignore[arg-type] # we don't want to pollute the typing of `is_absolute_index_sequence` for this temporary code # fmt: off
1081
1093
d , r = item [0 ]
1082
1094
assert d == self ._dimension
1083
- assert isinstance (r , int )
1084
- return self .dtype . scalar_type ( r )
1095
+ assert isinstance (r , core_defs . INTEGRAL_TYPES )
1096
+ return self .__class__ ( self . _dimension , r ) # type: ignore[arg-type] # not sure why the assert above does not work
1085
1097
# TODO set a domain...
1086
1098
raise NotImplementedError ()
1087
1099
@@ -1195,8 +1207,12 @@ def remap(self, index_field: common.ConnectivityField | fbuiltins.FieldOffset) -
1195
1207
# TODO can be implemented by constructing and ndarray (but do we know of which kind?)
1196
1208
raise NotImplementedError ()
1197
1209
1198
- def restrict (self , item : common .AnyIndexSpec ) -> common .Field | core_defs . ScalarT :
1210
+ def restrict (self , item : common .AnyIndexSpec ) -> common .Field :
1199
1211
# TODO set a domain...
1212
+ return self
1213
+
1214
+ def as_scalar (self ) -> core_defs .ScalarT :
1215
+ assert self .domain .ndim == 0
1200
1216
return self ._value
1201
1217
1202
1218
__call__ = remap
0 commit comments