@@ -3183,34 +3183,58 @@ def __repr__(self) -> str:
3183
3183
# Well typed getters
3184
3184
def as_float (self ) -> float :
3185
3185
"""Get the attribute value as a float."""
3186
+ if self .type != _enums .AttributeType .FLOAT :
3187
+ raise TypeError (
3188
+ f"Attribute '{ self .name } ' is not of type FLOAT. Actual type: { self .type } "
3189
+ )
3186
3190
# Do not use isinstance check because it may prevent np.float32 etc. from being used
3187
3191
return float (self .value )
3188
3192
3189
3193
def as_int (self ) -> int :
3190
3194
"""Get the attribute value as an int."""
3195
+ if self .type != _enums .AttributeType .INT :
3196
+ raise TypeError (
3197
+ f"Attribute '{ self .name } ' is not of type INT. Actual type: { self .type } "
3198
+ )
3191
3199
# Do not use isinstance check because it may prevent np.int32 etc. from being used
3192
3200
return int (self .value )
3193
3201
3194
3202
def as_string (self ) -> str :
3195
3203
"""Get the attribute value as a string."""
3204
+ if self .type != _enums .AttributeType .STRING :
3205
+ raise TypeError (
3206
+ f"Attribute '{ self .name } ' is not of type STRING. Actual type: { self .type } "
3207
+ )
3196
3208
if not isinstance (self .value , str ):
3197
3209
raise TypeError (f"Value of attribute '{ self !r} ' is not a string." )
3198
3210
return self .value
3199
3211
3200
3212
def as_tensor (self ) -> _protocols .TensorProtocol :
3201
3213
"""Get the attribute value as a tensor."""
3214
+ if self .type != _enums .AttributeType .TENSOR :
3215
+ raise TypeError (
3216
+ f"Attribute '{ self .name } ' is not of type TENSOR. Actual type: { self .type } "
3217
+ )
3202
3218
if not isinstance (self .value , _protocols .TensorProtocol ):
3203
3219
raise TypeError (f"Value of attribute '{ self !r} ' is not a tensor." )
3204
3220
return self .value
3205
3221
3206
3222
def as_graph (self ) -> Graph :
3207
3223
"""Get the attribute value as a graph."""
3224
+ if self .type != _enums .AttributeType .GRAPH :
3225
+ raise TypeError (
3226
+ f"Attribute '{ self .name } ' is not of type GRAPH. Actual type: { self .type } "
3227
+ )
3208
3228
if not isinstance (self .value , Graph ):
3209
3229
raise TypeError (f"Value of attribute '{ self !r} ' is not a graph." )
3210
3230
return self .value
3211
3231
3212
3232
def as_floats (self ) -> Sequence [float ]:
3213
3233
"""Get the attribute value as a sequence of floats."""
3234
+ if self .type != _enums .AttributeType .FLOATS :
3235
+ raise TypeError (
3236
+ f"Attribute '{ self .name } ' is not of type FLOATS. Actual type: { self .type } "
3237
+ )
3214
3238
if not isinstance (self .value , Sequence ):
3215
3239
raise TypeError (f"Value of attribute '{ self !r} ' is not a Sequence." )
3216
3240
# Do not use isinstance check on elements because it may prevent np.int32 etc. from being used
@@ -3219,6 +3243,10 @@ def as_floats(self) -> Sequence[float]:
3219
3243
3220
3244
def as_ints (self ) -> Sequence [int ]:
3221
3245
"""Get the attribute value as a sequence of ints."""
3246
+ if self .type != _enums .AttributeType .INTS :
3247
+ raise TypeError (
3248
+ f"Attribute '{ self .name } ' is not of type INTS. Actual type: { self .type } "
3249
+ )
3222
3250
if not isinstance (self .value , Sequence ):
3223
3251
raise TypeError (f"Value of attribute '{ self !r} ' is not a Sequence." )
3224
3252
# Do not use isinstance check on elements because it may prevent np.int32 etc. from being used
@@ -3227,6 +3255,10 @@ def as_ints(self) -> Sequence[int]:
3227
3255
3228
3256
def as_strings (self ) -> Sequence [str ]:
3229
3257
"""Get the attribute value as a sequence of strings."""
3258
+ if self .type != _enums .AttributeType .STRINGS :
3259
+ raise TypeError (
3260
+ f"Attribute '{ self .name } ' is not of type STRINGS. Actual type: { self .type } "
3261
+ )
3230
3262
if not isinstance (self .value , Sequence ):
3231
3263
raise TypeError (f"Value of attribute '{ self !r} ' is not a Sequence." )
3232
3264
if onnxscript .DEBUG :
@@ -3237,6 +3269,10 @@ def as_strings(self) -> Sequence[str]:
3237
3269
3238
3270
def as_tensors (self ) -> Sequence [_protocols .TensorProtocol ]:
3239
3271
"""Get the attribute value as a sequence of tensors."""
3272
+ if self .type != _enums .AttributeType .TENSORS :
3273
+ raise TypeError (
3274
+ f"Attribute '{ self .name } ' is not of type TENSORS. Actual type: { self .type } "
3275
+ )
3240
3276
if not isinstance (self .value , Sequence ):
3241
3277
raise TypeError (f"Value of attribute '{ self !r} ' is not a Sequence." )
3242
3278
if onnxscript .DEBUG :
@@ -3247,6 +3283,10 @@ def as_tensors(self) -> Sequence[_protocols.TensorProtocol]:
3247
3283
3248
3284
def as_graphs (self ) -> Sequence [Graph ]:
3249
3285
"""Get the attribute value as a sequence of graphs."""
3286
+ if self .type != _enums .AttributeType .GRAPHS :
3287
+ raise TypeError (
3288
+ f"Attribute '{ self .name } ' is not of type GRAPHS. Actual type: { self .type } "
3289
+ )
3250
3290
if not isinstance (self .value , Sequence ):
3251
3291
raise TypeError (f"Value of attribute '{ self !r} ' is not a Sequence." )
3252
3292
if onnxscript .DEBUG :
0 commit comments