You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Once upon a time, I wrote this long and detailed comment on some of the core choices for the Prefixed type:
"""# Note on Numeric Types `Prefixed` supports a sole type for its `number` field: the standard library's `Decimal`. One might wonder why it does not include `int` and `float` as well, or perhaps some other numeric-like types. This boils down to limitations in validation. Previous versions of `Prefixed` have used a `Number` union-type along these lines:`Number = Union[Decimal, float, int]`This is nominally handy, but results in many values being converted among these types, often where one may not expect. The pydantic docs are clear about their limitations in this respect: https://pydantic-docs.helpmanual.io/usage/types/#unionsThese limitations include the fact that despite being declared in list-like "ordered" syntax, union-types do not have orders in the Python standard library. So even interpreting `Union[Decimal, float, int]` as "prefer Decimal, use float and then int if that (for whatever reason) doesn't work" fails, since the `Union` syntax can be reordered arbitrarily by the language. The other clear alternative is not doing runtime type-validation of `Prefixed`, or of classes which instantiate them. Prior versions also tried this, to fairly confounding results. Incorporating standard-library `dataclasses` as members of larger `pydantic.dataclasses` seems to *work* - i.e. it does not produce `ValidationError`s or similar - but ultimately with enough of them, triggers some highly inscrutable errors in the standard-library methods. So: all `Decimal`, all `pydantic.dataclasses`."""
Turns out we failed to apply that to many of the Primitive parameters.
In particular they use a union-type called ScalarParam, which is:
# Type alias for many scalar parametersScalarParam=Union[Prefixed, int, float, str]
Which, as that comment indicates, turns to int lots of times that you don't want. @aviralpandey found this in his ADC work. As with most of these subtle translation bugs, it appeared a pain to track down.
The text was updated successfully, but these errors were encountered:
We can definitely prevent the confusion by making these all Prefixed or Optional[Prefixed].
Required user-level changes would, for now, be to creating a Prefixed one way or another, e.g.:
importhdl21ashfromhdl21.prefiximportmfromhdl21.primitivesimportVdcVdc.Params(dc=1000*m) # A long-ish way to write "one"Vdc.Params(dc=h.Prefixed(1, h.Prefix.UNIT)) # A longer one Vdc.Params(dc=h.Prefixed(1)) # UNIT becomes the default prefix
Ideally we could make conversions into these inline, e.g. with:
Vdc.Params(dc=1)
Where this gets converted to Prefixed(Decimal(1)) along the way.
Opened an issue with Pydantic to see whether they can support such a thing.
Once upon a time, I wrote this long and detailed comment on some of the core choices for the
Prefixed
type:Turns out we failed to apply that to many of the
Primitive
parameters.In particular they use a union-type called
ScalarParam
, which is:Which, as that comment indicates, turns to
int
lots of times that you don't want.@aviralpandey found this in his ADC work. As with most of these subtle translation bugs, it appeared a pain to track down.
The text was updated successfully, but these errors were encountered: