diff options
-rw-r--r-- | test_uritemplate.py | 6 | ||||
-rw-r--r-- | uritemplate/template.py | 10 |
2 files changed, 14 insertions, 2 deletions
diff --git a/test_uritemplate.py b/test_uritemplate.py index d23a7ed..756a9a6 100644 --- a/test_uritemplate.py +++ b/test_uritemplate.py @@ -491,6 +491,12 @@ class TestURITemplate(RFCTemplateExamples('RFCMeta', (TestCase,), {})): d[u] += 1 self.assertEqual(d, {t: 2}) + def test_no_mutate(self): + args = {} + t = URITemplate('') + t.expand(args, key=1) + self.assertEqual(args, {}) + class TestURIVariable(TestCase): def setUp(self): diff --git a/uritemplate/template.py b/uritemplate/template.py index 0d45214..363e8ea 100644 --- a/uritemplate/template.py +++ b/uritemplate/template.py @@ -21,6 +21,10 @@ from uritemplate.variable import URIVariable template_re = re.compile('{([^\}]+)}') +def _update(kwargs, var_dict): + kwargs.update((k, v) for k, v in var_dict.items() if k not in kwargs) + + class URITemplate(object): """This parses the template and will be used to expand it. @@ -121,7 +125,8 @@ class URITemplate(object): ``val2`` will be used instead of ``val1``. """ - kwargs.update(var_dict or {}) + if var_dict: + _update(kwargs, var_dict) return self._expand(kwargs, False) @@ -141,6 +146,7 @@ class URITemplate(object): t.partial() # => URITemplate('https://api.github.com{/end}') """ - kwargs.update(var_dict or {}) + if var_dict: + _update(kwargs, var_dict) return URITemplate(self._expand(kwargs, True)) |