@@ -192,20 +192,19 @@ def support_point(op, rv, *dist_params):
192
192
return new_cls
193
193
194
194
195
- class _class_or_instancemethod ( classmethod ):
196
- """Allow a method to be called both as a classmethod and an instancemethod .
195
+ class _class_or_instance_property ( property ):
196
+ """Allow a property to be accessed from a class or an instance .
197
197
198
- Priority is given to the instancemethod .
198
+ Priority is given to the instance .
199
199
200
200
This is used to allow extracting information from the signature of a SymbolicRandomVariable
201
- which may be provided either as a class attribute or as an instance attribute.
201
+ which may be available early as a class attribute or only later as an instance attribute.
202
202
203
- Adapted from https://stackoverflow.com/a/28238047
203
+ Adapted from https://stackoverflow.com/a/13624858
204
204
"""
205
205
206
- def __get__ (self , instance , type_ ):
207
- descr_get = super ().__get__ if instance is None else self .__func__ .__get__
208
- return descr_get (instance , type_ )
206
+ def __get__ (self , owner_self , owner_cls ):
207
+ return self .fget (owner_self if owner_self is not None else owner_cls )
209
208
210
209
211
210
class SymbolicRandomVariable (MeasurableOp , OpFromGraph ):
@@ -241,8 +240,7 @@ class SymbolicRandomVariable(MeasurableOp, OpFromGraph):
241
240
_print_name : tuple [str , str ] = ("Unknown" , "\\ operatorname{Unknown}" )
242
241
"""Tuple of (name, latex name) used for for pretty-printing variables of this type"""
243
242
244
- @_class_or_instancemethod
245
- @property
243
+ @_class_or_instance_property
246
244
def signature (cls_or_self ) -> None | str :
247
245
# Convert "expanded" signature into "vanilla" signature that has no rng and size tokens
248
246
extended_signature = cls_or_self .extended_signature
@@ -257,40 +255,28 @@ def signature(cls_or_self) -> None | str:
257
255
258
256
return signature
259
257
260
- @_class_or_instancemethod
261
- @property
258
+ @_class_or_instance_property
262
259
def ndims_params (cls_or_self ) -> Sequence [int ] | None :
263
- """Number of core dimensions of the distribution's parameters."""
260
+ """Return number of core dimensions of the distribution's parameters."""
264
261
signature = cls_or_self .signature
265
262
if signature is None :
266
263
return None
267
264
inputs_signature , _ = _parse_gufunc_signature (signature )
268
265
return [len (sig ) for sig in inputs_signature ]
269
266
270
- @_class_or_instancemethod
271
- @property
267
+ @_class_or_instance_property
272
268
def ndim_supp (cls_or_self ) -> int | None :
273
- """Number of support dimensions of the RandomVariable.
269
+ """Return number of support dimensions of the RandomVariable.
274
270
275
271
(0 for scalar, 1 for vector, ...)
276
272
"""
277
273
signature = cls_or_self .signature
278
274
if signature is None :
279
- return None
275
+ return getattr ( cls_or_self , "_ndim_supp" , None )
280
276
_ , outputs_params_signature = _parse_gufunc_signature (signature )
281
277
return max (len (out_sig ) for out_sig in outputs_params_signature )
282
278
283
- @_class_or_instancemethod
284
- def _parse_extended_signature (cls_or_self ) -> tuple [tuple [str , ...], tuple [str , ...]] | None :
285
- extended_signature = cls_or_self .extended_signature
286
- if extended_signature is None :
287
- return None
288
-
289
- fake_signature = extended_signature .replace ("[rng]" , "(rng)" ).replace ("[size]" , "(size)" )
290
- return _parse_gufunc_signature (fake_signature )
291
-
292
- @_class_or_instancemethod
293
- @property
279
+ @_class_or_instance_property
294
280
def default_output (cls_or_self ) -> int | None :
295
281
extended_signature = cls_or_self .extended_signature
296
282
if extended_signature is None :
@@ -374,7 +360,7 @@ def __init__(
374
360
if "ndim_supp" in kwargs :
375
361
# For backwards compatibility we allow passing ndim_supp without signature
376
362
# This is the only variable that PyMC absolutely needs to work with SymbolicRandomVariables
377
- self .ndim_supp = kwargs .pop ("ndim_supp" )
363
+ self ._ndim_supp = kwargs .pop ("ndim_supp" )
378
364
379
365
if self .ndim_supp is None :
380
366
raise ValueError ("ndim_supp or signature must be provided" )
0 commit comments