22
33import collections .abc
44import os
5- from typing import TYPE_CHECKING , Any
5+ import warnings
6+ from typing import TYPE_CHECKING , Any , ClassVar
67
78import numpy as np
89
1415 has_loop = False
1516from typing import Generic
1617
18+ from qcodes .utils import QCoDeSDeprecationWarning
19+
1720from .parameter_base import InstrumentTypeVar_co , ParameterBase , ParameterDataTypeVar
1821from .sequence_helpers import is_sequence_of
1922
4043 )
4144
4245
46+ _SHAPE_UNSET : Any = object ()
47+
48+
4349class ArrayParameter (
4450 ParameterBase [ParameterDataTypeVar , InstrumentTypeVar_co ],
4551 Generic [ParameterDataTypeVar , InstrumentTypeVar_co ],
@@ -129,10 +135,27 @@ class ArrayParameter(
129135
130136 """
131137
138+ _DEPRECATED_POSITIONAL_ARGS : ClassVar [tuple [str , ...]] = (
139+ "shape" ,
140+ "instrument" ,
141+ "label" ,
142+ "unit" ,
143+ "setpoints" ,
144+ "setpoint_names" ,
145+ "setpoint_labels" ,
146+ "setpoint_units" ,
147+ "docstring" ,
148+ "snapshot_get" ,
149+ "snapshot_value" ,
150+ "snapshot_exclude" ,
151+ "metadata" ,
152+ )
153+
132154 def __init__ (
133155 self ,
134156 name : str ,
135- shape : Sequence [int ],
157+ * args : Any ,
158+ shape : Sequence [int ] = _SHAPE_UNSET ,
136159 # mypy seems to be confused here. The bound and default for InstrumentTypeVar_co
137160 # contains None but mypy will not allow it as a default as of v 1.19.0
138161 instrument : InstrumentTypeVar_co = None , # type: ignore[assignment]
@@ -149,6 +172,90 @@ def __init__(
149172 metadata : Mapping [Any , Any ] | None = None ,
150173 ** kwargs : Any ,
151174 ) -> None :
175+ if args :
176+ # TODO: After QCoDeS 0.57 remove the args argument and delete this code block.
177+ # we hardcode the class since mypy does not support __class__ and
178+ # self / self.__class__ / type(self) in class bodies does not give
179+ # exactly this class but the type of a subclass
180+ positional_names = ArrayParameter ._DEPRECATED_POSITIONAL_ARGS
181+ if len (args ) > len (positional_names ):
182+ raise TypeError (
183+ f"{ type (self ).__name__ } .__init__() takes at most "
184+ f"{ len (positional_names ) + 2 } positional arguments "
185+ f"({ len (args ) + 2 } given)"
186+ )
187+
188+ _defaults : dict [str , Any ] = {
189+ "shape" : _SHAPE_UNSET ,
190+ "instrument" : None ,
191+ "label" : None ,
192+ "unit" : None ,
193+ "setpoints" : None ,
194+ "setpoint_names" : None ,
195+ "setpoint_labels" : None ,
196+ "setpoint_units" : None ,
197+ "docstring" : None ,
198+ "snapshot_get" : True ,
199+ "snapshot_value" : False ,
200+ "snapshot_exclude" : False ,
201+ "metadata" : None ,
202+ }
203+
204+ _kwarg_vals : dict [str , Any ] = {
205+ "shape" : shape ,
206+ "instrument" : instrument ,
207+ "label" : label ,
208+ "unit" : unit ,
209+ "setpoints" : setpoints ,
210+ "setpoint_names" : setpoint_names ,
211+ "setpoint_labels" : setpoint_labels ,
212+ "setpoint_units" : setpoint_units ,
213+ "docstring" : docstring ,
214+ "snapshot_get" : snapshot_get ,
215+ "snapshot_value" : snapshot_value ,
216+ "snapshot_exclude" : snapshot_exclude ,
217+ "metadata" : metadata ,
218+ }
219+
220+ for i in range (len (args )):
221+ arg_name = positional_names [i ]
222+ if _kwarg_vals [arg_name ] is not _defaults [arg_name ]:
223+ raise TypeError (
224+ f"{ type (self ).__name__ } .__init__() got multiple "
225+ f"values for argument '{ arg_name } '"
226+ )
227+
228+ positional_arg_names = positional_names [: len (args )]
229+ names_str = ", " .join (f"'{ n } '" for n in positional_arg_names )
230+ warnings .warn (
231+ f"Passing { names_str } as positional argument(s) to "
232+ f"{ type (self ).__name__ } is deprecated. "
233+ f"Please pass them as keyword arguments." ,
234+ QCoDeSDeprecationWarning ,
235+ stacklevel = 2 ,
236+ )
237+
238+ _pos = dict (zip (positional_names , args ))
239+ shape = _pos .get ("shape" , shape )
240+ instrument = _pos .get ("instrument" , instrument )
241+ label = _pos .get ("label" , label )
242+ unit = _pos .get ("unit" , unit )
243+ setpoints = _pos .get ("setpoints" , setpoints )
244+ setpoint_names = _pos .get ("setpoint_names" , setpoint_names )
245+ setpoint_labels = _pos .get ("setpoint_labels" , setpoint_labels )
246+ setpoint_units = _pos .get ("setpoint_units" , setpoint_units )
247+ docstring = _pos .get ("docstring" , docstring )
248+ snapshot_get = _pos .get ("snapshot_get" , snapshot_get )
249+ snapshot_value = _pos .get ("snapshot_value" , snapshot_value )
250+ snapshot_exclude = _pos .get ("snapshot_exclude" , snapshot_exclude )
251+ metadata = _pos .get ("metadata" , metadata )
252+
253+ if shape is _SHAPE_UNSET :
254+ raise TypeError (
255+ f"{ type (self ).__name__ } .__init__() missing required "
256+ f"keyword argument: 'shape'"
257+ )
258+
152259 super ().__init__ (
153260 name ,
154261 instrument = instrument ,
0 commit comments