Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-14842: Fix deprecation warnings from PropertyList/Set.get #37

Merged
merged 1 commit into from
Jun 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,17 @@ def _propertyContainerGet(container, name, returnStyle):
name : ``str``
Name of item
returnStyle : ``ReturnStyle``
Control whether data is returned as an array or scalar:
Control whether numeric or string data is returned as an array

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got myself confused when reconciling the docs and commit message. I understand:
(1) getArray(nameofAPropertyList) should return a scalar that one can iterate over.
(2) If ReturnStyle.SCALAR, then it returns the last value?

Does this mean the scalar reference in #1 is a different concept than #2? Because I would naively think: the property list is being returned as a scalar, which means I get the last value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of iterating (in my commit message) is that if you want to iterate over all items in a PropertySet or PropertyList then getArray is the approved way to do it because it returns all numeric or string data (instead of ignoring all but the last value, like getScalar. However, before this commit getArray would raise an exception for items that were neither numeric nor strings, which meant iterating over all items was a very messy business (you had to determine the data type first, then call getArray if numeric or string). Now you can just call getArray on any item and get something sensible back -- something that contains all known information about that item.

or scalar (the other types, ``PropertyList``, ``PropertySet``
and ``PersistablePtr``, are always returned as a scalar):
- ReturnStyle.ARRAY: return numeric or string data types
as an array of values. Raise TypeError for
PropertyList, PropertySet or PersistablePtr.
as an array of values.
- ReturnStyle.SCALAR: return numeric or string data types
as a single value; if the item has multiple values then
return the last value. Return PropertyList, PropertySet
or PersistablePtr as a single item.
return the last value.
- ReturnStyle.AUTO: (deprecated) return numeric or string data
as a scalar if there is just one item, or as an array
otherwise. Return PropertyList, PropertySet
or PersistablePtr as a single item.

Raises
------
ValueError
If `returnStyle`=``ReturnStyle.ARRAY`` and the item is of type
``PropertyList``, ``PropertySet`` or ``PersistablePtr``

Notes
-----
`returnStyle` is handled as follows:
otherwise.
"""
if not container.exists(name):
raise lsst.pex.exceptions.NotFoundError(name + " not found")
Expand All @@ -97,9 +86,6 @@ def _propertyContainerGet(container, name, returnStyle):
return value
return value[-1]

if returnStyle == ReturnStyle.ARRAY:
raise TypeError("Item {} is not numeric or string".format(name))

try:
return container.getAsPropertyListPtr(name)
except Exception:
Expand Down Expand Up @@ -253,7 +239,10 @@ def get(self, name):
return _propertyContainerGet(self, name, returnStyle=ReturnStyle.AUTO)

def getArray(self, name):
"""Return an item as an array; the item type must be numeric or string
"""Return an item as an array if the item is numeric or string

If the item is a ``PropertySet``, ``PropertyList`` or
``lsst.daf.base.PersistablePtr`` then return the item as a scalar.

Parameters
----------
Expand All @@ -264,10 +253,6 @@ def getArray(self, name):
------
lsst.pex.exceptions.NotFoundError
If the item does not exist.
TypeError
If item type is ``lsst.daf.base.PropertyList``,
``lsst.daf.base.PropertySet``
or ``lsst.daf.base.PersistablePtr``.
"""
return _propertyContainerGet(self, name, returnStyle=ReturnStyle.ARRAY)

Expand Down
6 changes: 2 additions & 4 deletions tests/test_PropertySet_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,14 @@ def testSubPS(self):
ps1 = dafBase.PropertySet()
ps1.set("a", 1)
ps.setPropertySet("b", ps1)
with self.assertRaises(TypeError):
ps.getArray("b")
self.assertEqual(ps.getArray("b"), ps1)
self.assertEqual(ps.getScalar("b"), ps1)
with self.assertWarns(DeprecationWarning):
self.assertEqual(ps.get("b.a"), 1)
self.assertEqual(ps.getArray("b.a"), [1])
self.assertEqual(ps.getScalar("b.a"), 1)
ps.set("c", ps1)
with self.assertRaises(TypeError):
ps.getArray("c")
self.assertEqual(ps.getArray("c"), ps1)
self.assertEqual(ps.getScalar("c"), ps1)
with self.assertWarns(DeprecationWarning):
self.assertEqual(ps.get("c.a"), 1)
Expand Down