Repository: deepanshs/csdmpy (language: python)

Issue:
Error when saving CSDM objects
When I create a csdm object, I can save it with no issues.  When I try to change the csdm object, by replacing scaling a dimension or replacing a dimension, the object then will not save

Exact error message: "TypeError: Object of type PhysicalType is not JSON serializable"

Windows, python 3.9.7, csdmpy 0.4.1


Discussion / hints:
@mccarthy677 For log purposes, can you also provide a code snippet for reproducing the bug?

Fix patch:
diff --git a/csdmpy/dependent_variable/__init__.py b/csdmpy/dependent_variable/__init__.py
index 4019d50..45e90c5 100644
--- a/csdmpy/dependent_variable/__init__.py
+++ b/csdmpy/dependent_variable/__init__.py
@@ -544,7 +544,7 @@ def quantity_name(self):
         Raises:
             NotImplementedError: When assigning a value.
         """
-        return self.subtype.quantity_name
+        return str(self.subtype.quantity_name)
 
     @quantity_name.setter
     def quantity_name(self, value=""):
diff --git a/csdmpy/dependent_variable/base_class.py b/csdmpy/dependent_variable/base_class.py
index 8a95aba..dd5cf68 100644
--- a/csdmpy/dependent_variable/base_class.py
+++ b/csdmpy/dependent_variable/base_class.py
@@ -136,7 +136,7 @@ def unit(self):
     @property
     def quantity_name(self):
         """Return quantity name associated with the physical quantity."""
-        return deepcopy(self._quantity_name)
+        return deepcopy(str(self._quantity_name))
 
     @quantity_name.setter
     def quantity_name(self, value=""):
diff --git a/csdmpy/dimension/__init__.py b/csdmpy/dimension/__init__.py
index 2391b91..1a404bd 100644
--- a/csdmpy/dimension/__init__.py
+++ b/csdmpy/dimension/__init__.py
@@ -640,7 +640,7 @@ def quantity_name(self):
             AttributeError: For `labeled` dimensions.
             NotImplementedError: When assigning a value.
         """
-        return self.subtype.quantity_name
+        return str(self.subtype.quantity_name)
 
     @quantity_name.setter
     def quantity_name(self, value):
diff --git a/csdmpy/dimension/quantitative.py b/csdmpy/dimension/quantitative.py
index c84abe7..5d697d5 100644
--- a/csdmpy/dimension/quantitative.py
+++ b/csdmpy/dimension/quantitative.py
@@ -113,7 +113,7 @@ def period(self, value=None):
     @property
     def quantity_name(self):
         """Quantity name associated with this dimension."""
-        return deepcopy(self._quantity_name)
+        return deepcopy(str(self._quantity_name))
 
     @quantity_name.setter
     def quantity_name(self, value):


Test patch:
diff --git a/tests/dimension_test.py b/tests/dimension_test.py
index 8462a56..a8ef914 100644
--- a/tests/dimension_test.py
+++ b/tests/dimension_test.py
@@ -681,3 +681,12 @@ def test_as_dimension():
     error = "Cannot convert a 2 dimensional array to a Dimension object."
     with pytest.raises(ValueError, match=".*{0}.*".format(error)):
         cp.as_dimension(np.arange(100).reshape(10, 10))
+
+
+def test_dimension_scale():
+    """Test for unit physical type in astropy>=4.0"""
+    dim = cp.LinearDimension(count=10, increment="1m")
+    dim2 = dim / 2.4
+    assert np.allclose(dim2.coordinates.value, np.arange(10) / 2.4)
+    assert dim2.quantity_name == "length"
+    assert type(dim2.quantity_name) == str