@@ -3397,6 +3397,24 @@ def __init__(
3397
3397
* ,
3398
3398
doc_string : str | None = None ,
3399
3399
) -> None :
3400
+ # Quick checks to ensure that INT and FLOAT attributes are stored as int and float,
3401
+ # not np.int32, np.float32, bool, etc.
3402
+ # This also allows errors to be raised at the time of construction instead of later
3403
+ # during serialization.
3404
+ # TODO(justinchuby): Use case matching when we drop support for Python 3.9
3405
+ if value is None :
3406
+ # Value can be None for reference attributes or when it is used as a
3407
+ # placeholder for schemas
3408
+ pass
3409
+ elif type == _enums .AttributeType .INT :
3410
+ value = int (value )
3411
+ elif type == _enums .AttributeType .FLOAT :
3412
+ value = float (value )
3413
+ elif type == _enums .AttributeType .INTS :
3414
+ value = tuple (int (v ) for v in value )
3415
+ elif type == _enums .AttributeType .FLOATS :
3416
+ value = tuple (float (v ) for v in value )
3417
+
3400
3418
self ._name = name
3401
3419
self ._type = type
3402
3420
self ._value = value
@@ -3472,17 +3490,17 @@ def as_float(self) -> float:
3472
3490
raise TypeError (
3473
3491
f"Attribute '{ self .name } ' is not of type FLOAT. Actual type: { self .type } "
3474
3492
)
3475
- # Do not use isinstance check because it may prevent np.float32 etc. from being used
3476
- return float ( self .value )
3493
+ # value is guaranteed to be a float in the constructor
3494
+ return self .value
3477
3495
3478
3496
def as_int (self ) -> int :
3479
3497
"""Get the attribute value as an int."""
3480
3498
if self .type != _enums .AttributeType .INT :
3481
3499
raise TypeError (
3482
3500
f"Attribute '{ self .name } ' is not of type INT. Actual type: { self .type } "
3483
3501
)
3484
- # Do not use isinstance check because it may prevent np.int32 etc. from being used
3485
- return int ( self .value )
3502
+ # value is guaranteed to be an int in the constructor
3503
+ return self .value
3486
3504
3487
3505
def as_string (self ) -> str :
3488
3506
"""Get the attribute value as a string."""
@@ -3522,9 +3540,8 @@ def as_floats(self) -> Sequence[float]:
3522
3540
)
3523
3541
if not isinstance (self .value , Sequence ):
3524
3542
raise TypeError (f"Value of attribute '{ self !r} ' is not a Sequence." )
3525
- # Do not use isinstance check on elements because it may prevent np.int32 etc. from being used
3526
- # Create a copy of the list to prevent mutation
3527
- return [float (v ) for v in self .value ]
3543
+ # value is guaranteed to be a sequence of float in the constructor
3544
+ return self .value
3528
3545
3529
3546
def as_ints (self ) -> Sequence [int ]:
3530
3547
"""Get the attribute value as a sequence of ints."""
@@ -3534,9 +3551,8 @@ def as_ints(self) -> Sequence[int]:
3534
3551
)
3535
3552
if not isinstance (self .value , Sequence ):
3536
3553
raise TypeError (f"Value of attribute '{ self !r} ' is not a Sequence." )
3537
- # Do not use isinstance check on elements because it may prevent np.int32 etc. from being used
3538
- # Create a copy of the list to prevent mutation
3539
- return list (self .value )
3554
+ # value is guaranteed to be a sequence of int in the constructor
3555
+ return self .value
3540
3556
3541
3557
def as_strings (self ) -> Sequence [str ]:
3542
3558
"""Get the attribute value as a sequence of strings."""
@@ -3605,7 +3621,7 @@ def RefAttr(
3605
3621
return Attr (name , type , None , ref_attr_name = ref_attr_name , doc_string = doc_string )
3606
3622
3607
3623
3608
- def AttrFloat32 (name : str , value : float , doc_string : str | None = None ) -> Attr :
3624
+ def AttrFloat32 (name : str , value : float | np . floating , doc_string : str | None = None ) -> Attr :
3609
3625
"""Create a float attribute."""
3610
3626
# NOTE: The function name is capitalized to maintain API backward compatibility.
3611
3627
return Attr (
@@ -3616,7 +3632,7 @@ def AttrFloat32(name: str, value: float, doc_string: str | None = None) -> Attr:
3616
3632
)
3617
3633
3618
3634
3619
- def AttrInt64 (name : str , value : int , doc_string : str | None = None ) -> Attr :
3635
+ def AttrInt64 (name : str , value : int | np . integer , doc_string : str | None = None ) -> Attr :
3620
3636
"""Create an int attribute."""
3621
3637
# NOTE: The function name is capitalized to maintain API backward compatibility.
3622
3638
return Attr (
0 commit comments