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

Fix ConfigDict's handling of unicode keys #13

Merged
merged 1 commit into from
Dec 12, 2016
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
3 changes: 2 additions & 1 deletion python/lsst/pex/config/configDictField.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import traceback

from .config import Config, FieldValidationError, _typeStr, _joinNamePath
from .config import Config, FieldValidationError, _autocast, _typeStr, _joinNamePath
from .dictField import Dict, DictField
from .comparison import compareConfigs, compareScalars, getComparisonName

Expand All @@ -48,6 +48,7 @@ def __setitem__(self, k, x, at=None, label="setitem", setHistory=True):
raise FieldValidationError(self._field, self._config, msg)

# validate keytype
k = _autocast(k, self._field.keytype)
Copy link
Member Author

Choose a reason for hiding this comment

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

@r-owen it's probably the case that we should be using _autocast more consistently in pex_config. This works because a future str is an instance of unicode, although at present, you end up with the unicode-ness being stripped so this is going to fail (and pex_config will fail in general) if we pass in unicode strings that can't be converted to native Python 2 str. There are advantages to dropping Python 2.

if type(k) != self._field.keytype:
msg = "Key %r is of type %s, expected type %s" % \
(k, _typeStr(k), _typeStr(self._field.keytype))
Expand Down
2 changes: 1 addition & 1 deletion tests/configDictField.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def testAssignment(self):
self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d1", {"a": 0})
self.assertRaises(pexConfig.FieldValidationError, setattr, c, "d1", [1.2, 3, 4])
c.d1 = None
c.d1 = {"a": Config1, "b": Config1()}
c.d1 = {"a": Config1, u"b": Config1()}

def testValidate(self):
c = Config2()
Expand Down
2 changes: 1 addition & 1 deletion tests/dictField.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def testAssignment(self):
c.d4 = d
self.assertEqual(c.d4, d)
c.d4["a"] = 12
c.d4["b"] = "three"
c.d4[u"b"] = "three"
c.d4["c"] = None
self.assertEqual(c.d4["a"], 12)
self.assertEqual(c.d4["b"], "three")
Expand Down