Skip to content
This repository

Missing drop #98

Merged
merged 2 commits into from over 1 year ago

3 participants

Joe Dallago Andrew Brookins Chris McDonough
Joe Dallago
Collaborator

I basically just added some docs to issue #84. Seems ready to go to me.

Chris McDonough mcdonc merged commit a564f43 into from
Chris McDonough mcdonc closed this
Andrew Brookins

Thanks for adding docs, Joe.

I always appreciate having a "How to submit patches" checklist in the library docs, to help make sure I touch all the bases when submitting. Does Colander have one of those?

Something like:

  • Clone the repo
  • Create a branch for your patch
  • Write code
  • Add tests
  • Add docs if the patch introduces a new feature
  • Submit pull request

I know that workflow is pretty much standard, but it's always nice to be explicit about it anyway. I'd be happy to add docs for this if it doesn't already exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
18 colander/__init__.py
@@ -40,6 +40,15 @@ def __reduce__(self):
40 40
41 41 null = _null()
42 42
  43 +class _drop(object):
  44 + """
  45 + Represents a value that should be dropped if it is missing during
  46 + deserialization.
  47 + """
  48 + pass
  49 +
  50 +drop = _drop()
  51 +
43 52 def interpolate(msgs):
44 53 for s in msgs:
45 54 if hasattr(s, 'interpolate'):
@@ -531,11 +540,14 @@ def _impl(self, node, value, callback):
531 540 name = subnode.name
532 541 subval = value.pop(name, null)
533 542 try:
534   - result[name] = callback(subnode, subval)
  543 + sub_result = callback(subnode, subval)
535 544 except Invalid as e:
536 545 if error is None:
537 546 error = Invalid(node)
538 547 error.add(e, num)
  548 + else:
  549 + if sub_result is not drop:
  550 + result[name] = sub_result
539 551
540 552 if self.unknown == 'raise':
541 553 if value:
@@ -1640,7 +1652,9 @@ class _SchemaNode(object):
1640 1652 not provided, the missing value of this node will be the special marker
1641 1653 value :attr:`colander.required`, indicating that it is considered
1642 1654 'required'. When ``missing`` is :attr:`colander.required`, the
1643   - ``required`` computed attribute will be ``True``.
  1655 + ``required`` computed attribute will be ``True``. When ``missing`` is
  1656 + :attr:`colander.drop`, the node is dropped from the schema if it isn't
  1657 + set during serialization/deserialization.
1644 1658
1645 1659 - ``preparer``: Optional preparer for this node. It should be
1646 1660 an object that implements the
10 colander/tests/test_colander.py
@@ -2895,6 +2895,16 @@ class MySchema(colander.Schema):
2895 2895 self.assertEqual(node.children[2].title, '')
2896 2896 self.assertEqual(node.children[3].title, 'thing2')
2897 2897
  2898 + def test_deserialize_drop(self):
  2899 + import colander
  2900 + class MySchema(colander.Schema):
  2901 + a = colander.SchemaNode(colander.String())
  2902 + b = colander.SchemaNode(colander.String(), missing=colander.drop)
  2903 + node = MySchema()
  2904 + expected = {'a': 'test'}
  2905 + result = node.deserialize(expected)
  2906 + self.assertEqual(result, expected)
  2907 +
2898 2908 class TestSequenceSchema(unittest.TestCase):
2899 2909 def test_succeed(self):
2900 2910 import colander
4 docs/api.rst
Source Rendered
@@ -139,3 +139,7 @@ Schema-Related
139 139 .. attribute:: required
140 140
141 141 Represents a required value in colander-related operations.
  142 +
  143 + .. attribute:: drop
  144 + Represents a value that will be dropped from the schema if it is missing. Passed
  145 + ass a value to the `missing` keyword argument.

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.