Skip to content
Myr

Fix default index for TaggedJSONSerializer.register()

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Change the default value of ``index`` to ``None`` in ``register()`` so that it is possible to insert a new tag as the penultimate item in the order list.

authored by Nick Pope · committed by Nick Pope · parent 1654937 · Browse files · 2 files+20 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+20 added−4 removed

  • tests:+16 added−0 removed
  • other:+4 added−4 removed

2 files+20 added−4 removed

modified:flask/json/tag.py+4 added-4 removed
241 unmodified lines
242243 for cls in self.default_tags:244 self.register(cls)245246 def register(self, tag_class, force=False, index=-1):246 def register(self, tag_class, force=False, index=None):247 """Register a new tag with this serializer.248249 :param tag_class: tag class to register. Will be instantiated with this250 serializer instance.251 :param force: overwrite an existing tag. If false (default), a252 :exc:`KeyError` is raised.253 :param index: index to insert the new tag in the tag order. Useful when254 the new tag is a special case of an existing tag. If -1 (default),255 the tag is appended to the end of the order.254 the new tag is a special case of an existing tag. If ``None``255 (default), the tag is appended to the end of the order.256257 :raise KeyError: if the tag key is already registered and ``force`` is258 not true.259 """
5 unmodified lines
265 raise KeyError("Tag '{0}' is already registered.".format(key))266267 self.tags[key] = tag268269 if index == -1:269 if index is None:270 self.order.append(tag)271 else:272 self.order.insert(index, tag)273
27 unmodified lines
modified:tests/test_json_tag.py+16 added
70 unmodified lines
71 t = JSONTag(None)72 pytest.raises(NotImplementedError, t.check, None)73 pytest.raises(NotImplementedError, t.to_json, None)74 pytest.raises(NotImplementedError, t.to_python, None)757677def test_tag_order():78 class Tag1(JSONTag):79 key = ' 1'8081 class Tag2(JSONTag):82 key = ' 2'8384 s = TaggedJSONSerializer()8586 s.register(Tag1, index=-1)87 assert isinstance(s.order[-2], Tag1)8889 s.register(Tag2, index=None)90 assert isinstance(s.order[-1], Tag2)