Quantity.getValueAs() is a Quantity, not a float in Python

type(App.Units.Quantity('89 mm').getValueAs('m'))

returns

<class 'Base.Quantity'>

I know some workarounds but the help of getValueAs() states “returns a floating point value as the provided unit” and the returned quantity may even be another physical quantity if the unit used in getValueAs() is not the same as the original variable.

Gaël

OS: Pop!_OS 22.04 LTS (sway-nvidia)
Word size of FreeCAD: 64-bit
Version: 0.21.1.33694 (Git) AppImage
Build type: Release
Branch: (HEAD detached at 0.21.1)
Hash: f6708547a9bb3f71a4aaade12109f511a72c207c
Python 3.10.12, Qt 5.15.8, Coin 4.0.0, Vtk 9.2.5, OCC 7.6.3
Locale: English/United States (en_US)
Installed mods: 
  * freecad.cross 0.1.2
  * Freecad-Built-in-themes-beta 1.2.2

Just to precise, Quantity.getValueAs() returns a unit-less Quantity.

q = App.Units.Quantity('89 mm').getValueAs('m').Value
type(q) #<class 'float'>

Even if it is a bug that the return is a quantity instead of a float, changing it at this point would potentially break many addons.

I think breaking changes before a 1.0 version should not be an issue. Moreover, for all official workbenches and all workbenches from https://github.com/FreeCAD/FreeCAD-addons the use of “.Value” is very easy to detect and fix. On top of that, (“Quantity.getValueAs().Value” can be replaced with “float(Quantity.getValueAs())” already now, to be compatible with the old and new “Quantity.getValueAs()”.

Rather than break things unnecessarily, it could be better to add a new function that provides the desired result and fix the documentation for the old function, if that is needed. Maybe name it getFloatValueAs() → float.

My question would then be. What would then be the purpose of getValueAs()?

In any case, the documentation of getValueAs states that it returns a float, which is untrue.

Simply correct documentation, and leave interface the same.

When there is risk of breaking old code, it is better IMHO to make a new method/function (there are slightly different terminology between languages) and keep the old code work, with the “buggy” or “imprecise” method.

Probably when 1.0 will be out, code could issue some “deprecation warning” that says that in long term (al least 1.2) some functions/methods will be dropped to give the time to developer to port the code to the new API or place some code to make workaround based on FreeCAD version.

Kind Regards

Carlo D.

Is this feature in the program related to my problem, which I voiced in this topic?

I can’t get data in the required format for list(a, b, c, d) from dimensional constraints.
I see the values I need, but there are not enough decimal places. Have to use a spreadsheet converter (workaround)
Thank you

https://devtalk.freecad.org/t/calculating-the-volume-of-a-specific-material-in-a-multimaterial-object-expressions/74263/1