88import numpy .typing as npt
99
1010from nitypes ._arguments import arg_to_uint , validate_dtype
11+ from nitypes ._exceptions import invalid_arg_type , invalid_array_ndim
1112from nitypes .waveform ._extended_properties import (
1213 CHANNEL_NAME ,
1314 UNIT_DESCRIPTION ,
@@ -122,18 +123,16 @@ def from_array_1d(
122123 """
123124 if isinstance (array , np .ndarray ):
124125 if array .ndim != 1 :
125- raise ValueError (
126- f"The input array must be a one-dimensional array or sequence. \n \n Number of dimensions: { array .ndim } "
126+ raise invalid_array_ndim (
127+ " input array" , " one-dimensional array or sequence" , array .ndim
127128 )
128129 elif isinstance (array , Sequence ) or (
129130 sys .version_info < (3 , 10 ) and isinstance (array , std_array .array )
130131 ):
131132 if dtype is None :
132133 raise ValueError ("You must specify a dtype when the input array is a sequence." )
133134 else :
134- raise TypeError (
135- f"The input array must be a one-dimensional array or sequence.\n \n Type: { type (array )} "
136- )
135+ raise invalid_arg_type ("input array" , "one-dimensional array or sequence" , array )
137136
138137 return AnalogWaveform (
139138 _data = np .asarray (array , dtype , copy = copy ),
@@ -198,18 +197,16 @@ def from_array_2d(
198197 """
199198 if isinstance (array , np .ndarray ):
200199 if array .ndim != 2 :
201- raise ValueError (
202- f"The input array must be a two-dimensional array or nested sequence. \n \n Number of dimensions: { array .ndim } "
200+ raise invalid_array_ndim (
201+ " input array" , " two-dimensional array or nested sequence" , array .ndim
203202 )
204203 elif isinstance (array , Sequence ) or (
205204 sys .version_info < (3 , 10 ) and isinstance (array , std_array .array )
206205 ):
207206 if dtype is None :
208207 raise ValueError ("You must specify a dtype when the input array is a sequence." )
209208 else :
210- raise TypeError (
211- f"The input array must be a two-dimensional array or nested sequence.\n \n Type: { type (array )} "
212- )
209+ raise invalid_arg_type ("input array" , "two-dimensional array or nested sequence" , array )
213210
214211 return [
215212 AnalogWaveform (
@@ -367,16 +364,18 @@ def _init_with_provided_array(
367364 capacity : SupportsIndex | None = None ,
368365 ) -> None :
369366 if not isinstance (data , np .ndarray ):
370- raise TypeError ( "The input array must be a one-dimensional array." )
367+ raise invalid_arg_type ( " input array" , " one-dimensional array" , data )
371368 if data .ndim != 1 :
372- raise ValueError ( "The input array must be a one-dimensional array." )
369+ raise invalid_array_ndim ( " input array" , " one-dimensional array" , data . ndim )
373370
374371 if dtype is None :
375372 dtype = data .dtype
376373 if dtype != data .dtype :
377- # from_array_1d() converts the input array to the requested dtype, so this error may be
378- # unreachable.
379- raise TypeError ("The data type of the input array must match the requested data type." )
374+ raise TypeError (
375+ "The data type of the input array must match the requested data type.\n \n "
376+ f"Array data type: { data .dtype } \n "
377+ f"Requested data type: { np .dtype (dtype )} "
378+ )
380379 validate_dtype (dtype , _ANALOG_DTYPES )
381380
382381 capacity = arg_to_uint ("capacity" , capacity , len (data ))
@@ -528,10 +527,7 @@ def capacity(self) -> int:
528527
529528 @capacity .setter
530529 def capacity (self , value : int ) -> None :
531- if value < 0 :
532- raise ValueError (
533- "The capacity must be a non-negative integer.\n \n " f"Capacity: { value } "
534- )
530+ value = arg_to_uint ("capacity" , value )
535531 if value < self ._start_index + self ._sample_count :
536532 raise ValueError (
537533 "The capacity must be equal to or greater than the number of samples in the waveform.\n \n "
@@ -561,7 +557,7 @@ def channel_name(self) -> str:
561557 @channel_name .setter
562558 def channel_name (self , value : str ) -> None :
563559 if not isinstance (value , str ):
564- raise TypeError ( "The channel name must be a str. \n \n " f"Provided value: { value !r } " )
560+ raise invalid_arg_type ( " channel name" , " str" , value )
565561 self ._extended_properties [CHANNEL_NAME ] = value
566562
567563 @property
@@ -574,7 +570,7 @@ def unit_description(self) -> str:
574570 @unit_description .setter
575571 def unit_description (self , value : str ) -> None :
576572 if not isinstance (value , str ):
577- raise TypeError ( "The unit description must be a str. \n \n " f"Provided value: { value !r } " )
573+ raise invalid_arg_type ( " unit description" , " str" , value )
578574 self ._extended_properties [UNIT_DESCRIPTION ] = value
579575
580576 @property
@@ -595,7 +591,7 @@ def timing(self) -> Timing:
595591 @timing .setter
596592 def timing (self , value : Timing ) -> None :
597593 if not isinstance (value , Timing ):
598- raise TypeError ( "The timing information must be a Timing object." )
594+ raise invalid_arg_type ( " timing information" , " Timing object" , value )
599595 self ._timing = value
600596 self ._precision_timing = None
601597
@@ -632,7 +628,7 @@ def precision_timing(self) -> PrecisionTiming:
632628 @precision_timing .setter
633629 def precision_timing (self , value : PrecisionTiming ) -> None :
634630 if not isinstance (value , PrecisionTiming ):
635- raise TypeError ( "The precision timing information must be a PrecisionTiming object." )
631+ raise invalid_arg_type ( " precision timing information" , " PrecisionTiming object" , value )
636632 self ._precision_timing = value
637633 self ._timing = None
638634
@@ -644,7 +640,5 @@ def scale_mode(self) -> ScaleMode:
644640 @scale_mode .setter
645641 def scale_mode (self , value : ScaleMode ) -> None :
646642 if not isinstance (value , ScaleMode ):
647- raise TypeError (
648- "The scale mode must be a ScaleMode object.\n \n " f"Provided value: { value !r} "
649- )
643+ raise invalid_arg_type ("scale mode" , "ScaleMode object" , value )
650644 self ._scale_mode = value
0 commit comments