Python marshmallow.missing() Examples
The following are 30
code examples of marshmallow.missing().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
marshmallow
, or try the search function
.
Example #1
Source File: pymongo.py From umongo with MIT License | 6 votes |
def _io_validate_data_proxy(schema, data_proxy, partial=None): errors = {} for name, field in schema.fields.items(): if partial and name not in partial: continue value = data_proxy.get(name) if value is ma.missing: continue try: if field.io_validate_recursive: field.io_validate_recursive(field, value) if field.io_validate: _run_validators(field.io_validate, field, value) except ma.ValidationError as exc: errors[name] = exc.messages if errors: raise ma.ValidationError(errors)
Example #2
Source File: __init__.py From environs with MIT License | 6 votes |
def _func2method(func: typing.Callable, method_name: str) -> ParserMethod: def method( self: "Env", name: str, default: typing.Any = ma.missing, subcast: typing.Type = None, **kwargs ): if self._sealed: raise EnvSealedError("Env has already been sealed. New values cannot be parsed.") parsed_key, raw_value, proxied_key = self._get_from_environ(name, default) if raw_value is ma.missing: raise EnvError('Environment variable "{}" not set'.format(proxied_key or parsed_key)) value = func(raw_value, **kwargs) self._fields[parsed_key] = ma.fields.Field(**kwargs) self._values[parsed_key] = value return value method.__name__ = method_name return method # From webargs
Example #3
Source File: schema.py From umongo with MIT License | 6 votes |
def schema_from_umongo_get_attribute(self, obj, attr, default): """ Overwrite default `Schema.get_attribute` method by this one to access umongo missing fields instead of returning `None`. example:: class MySchema(marshsmallow.Schema): get_attribute = schema_from_umongo_get_attribute # Define the rest of your schema ... """ ret = ma.Schema.get_attribute(self, obj, attr, default) if ret is None and ret is not default and attr in obj.schema.fields: raw_ret = obj._data.get(attr) return default if raw_ret is ma.missing else raw_ret return ret
Example #4
Source File: attrs.py From marshmallow-annotations with MIT License | 6 votes |
def _preprocess_typehint(self, typehint, kwargs, field_name, target): # while this seems contradictory to this converter, we need to # ignore attrs specific actions when a container type, e.g. a List[T], # contains a non-attrs manufactured type because this is recursively # called during schema generation. # however those types will still require an entry in the registry # is handled outside this converter if not _is_attrs(target): return attr = _get_attr_from_attrs(target.__attrs_attrs__, field_name) if attr.default != NOTHING: # default to optional even if the typehint isn't # but don't override the user saying otherwise kwargs.setdefault("required", False) kwargs.setdefault("missing", missing)
Example #5
Source File: test_data_proxy.py From umongo with MIT License | 6 votes |
def test_equality(self): class MySchema(Schema): a = fields.IntField() b = fields.IntField(attribute='in_mongo_b') MyDataProxy = data_proxy_factory('My', MySchema()) d1 = MyDataProxy() d1.load({'a': 1, 'b': 2}) assert d1 == {'a': 1, 'in_mongo_b': 2} d2 = MyDataProxy() d2.load({'a': 1, 'b': 2}) assert d1 == d2 assert d1 != None # noqa: E711 (None comparison) assert d1 != ma.missing assert None != d1 # noqa: E711 (None comparison) assert ma.missing != d1
Example #6
Source File: test_marshmallow.py From umongo with MIT License | 6 votes |
def test_dump_only(self): @self.instance.register class Doc(Document): dl = fields.IntField() do = fields.IntField(dump_only=True) lo = fields.IntField(load_only=True) nope = fields.IntField(dump_only=True, load_only=True) with pytest.raises(ma.ValidationError): Doc(do=1) with pytest.raises(ma.ValidationError): Doc(nope=1) assert Doc(dl=1, lo=2).dump() == {'dl': 1} with pytest.raises(ma.ValidationError) as excinfo: Doc(nope=ma.missing, do=ma.missing) assert excinfo.value.messages == {'nope': ['Unknown field.'], 'do': ['Unknown field.']}
Example #7
Source File: test_data_proxy.py From umongo with MIT License | 6 votes |
def test_set_to_missing_fields(self): class MySchema(Schema): a = fields.IntField() b = fields.IntField(attribute='in_mongo_b') MyDataProxy = data_proxy_factory('My', MySchema()) d = MyDataProxy(data={'a': 1}) assert d.get('b') is ma.missing assert d._data['in_mongo_b'] is ma.missing d.set('b', 2) assert d.get('b') == 2 d.delete('b') # Can do it two time in a row without error d.delete('b') assert d._data['in_mongo_b'] is ma.missing
Example #8
Source File: test_embedded_document.py From umongo with MIT License | 6 votes |
def test_equality(self): @self.instance.register class MyChildEmbeddedDocument(EmbeddedDocument): num = fields.IntField() @self.instance.register class MyParentEmbeddedDocument(EmbeddedDocument): embedded = fields.EmbeddedField(MyChildEmbeddedDocument) emb_1 = MyParentEmbeddedDocument(embedded={'num': 1}) emb_2 = MyParentEmbeddedDocument(embedded={'num': 1}) emb_3 = MyParentEmbeddedDocument(embedded={}) emb_4 = MyParentEmbeddedDocument() assert emb_1 == emb_2 assert emb_1 != emb_3 assert emb_1 != emb_4 assert emb_1 != None # noqa: E711 (None comparison) assert emb_1 != ma.missing assert None != emb_1 # noqa: E711 (None comparison) assert ma.missing != emb_1
Example #9
Source File: filtering.py From flask-resty with MIT License | 6 votes |
def get_field(self, view): """Construct the marshmallow field for deserializing filter values. This takes the field from the deserializer, then creates a copy with the desired semantics around missing values. :param view: The view with the model we wish to filter for. :type view: :py:class:`ModelView` """ base_field = view.deserializer.fields[self._column_name] try: field = self._fields[base_field] except KeyError: # We don't want the default value handling on the original field, # as that's only relevant for object deserialization. field = copy.deepcopy(base_field) field.required = self._required field.missing = self._missing self._fields[base_field] = field return field
Example #10
Source File: data_proxy.py From umongo with MIT License | 6 votes |
def _to_mongo_update(self): mongo_data = {} set_data = {} unset_data = [] for name in self.get_modified_fields(): field = self._fields[name] name = field.attribute or name val = field.serialize_to_mongo(self._data[name]) if val is ma.missing: unset_data.append(name) else: set_data[name] = val if set_data: mongo_data['$set'] = set_data if unset_data: mongo_data['$unset'] = {k: "" for k in unset_data} return mongo_data or None
Example #11
Source File: data_proxy.py From umongo with MIT License | 5 votes |
def _to_mongo(self): mongo_data = {} for key, val in self._data.items(): field = self._fields_from_mongo_key[key] val = field.serialize_to_mongo(val) if val is not ma.missing: mongo_data[key] = val return mongo_data
Example #12
Source File: data_proxy.py From umongo with MIT License | 5 votes |
def load(self, data): # Always use marshmallow partial load to skip required checks loaded_data = self.schema.load(data, partial=True) self._data = loaded_data # Map the modified fields list on the the loaded data self.clear_modified() for key in loaded_data: self._mark_as_modified(key) # TODO: mark added missing fields as modified? self._add_missing_fields()
Example #13
Source File: embedded_document.py From umongo with MIT License | 5 votes |
def __getattr__(self, name): if name[:2] == name[-2:] == '__': raise AttributeError(name) value = self._data.get(name, to_raise=AttributeError) return value if value is not ma.missing else None
Example #14
Source File: document.py From umongo with MIT License | 5 votes |
def __getattr__(self, name): if name[:2] == name[-2:] == '__': raise AttributeError(name) value = self._data.get(name, to_raise=AttributeError) return value if value is not ma.missing else None
Example #15
Source File: document.py From umongo with MIT License | 5 votes |
def __getitem__(self, name): value = self._data.get(name) return value if value is not ma.missing else None
Example #16
Source File: document.py From umongo with MIT License | 5 votes |
def clone(self): """Return a copy of this Document as a new Document instance All fields are deep-copied except the _id field. """ new = self.__class__() data = deepcopy(self._data._data) # Replace ID with new ID ("missing" unless a default value is provided) data['_id'] = new._data._data['_id'] new._data._data = data new._data._modified_data = set(data.keys()) return new
Example #17
Source File: embedded_document.py From umongo with MIT License | 5 votes |
def __getitem__(self, name): value = self._data.get(name) return value if value is not ma.missing else None
Example #18
Source File: data_proxy.py From umongo with MIT License | 5 votes |
def required_validate(self): errors = {} for name, field in self.schema.fields.items(): value = self._data[field.attribute or name] if field.required and value is ma.missing: errors[name] = [_("Missing data for required field.")] elif hasattr(field, '_required_validate'): try: field._required_validate(value) except ma.ValidationError as exc: errors[name] = exc.messages if errors: raise ma.ValidationError(errors) # Standards iterators providing oo and mongo worlds views
Example #19
Source File: schema.py From umongo with MIT License | 5 votes |
def as_marshmallow_schema(self, *, mongo_world=False): """ Return a pure-marshmallow version of this schema class. :param mongo_world: If True the schema will work against the mongo world instead of the OO world (default: False). """ # Use a cache to avoid generating several times the same schema cache_key = (self.__class__, self.MA_BASE_SCHEMA_CLS, mongo_world) if cache_key in self._marshmallow_schemas_cache: return self._marshmallow_schemas_cache[cache_key] # Create schema if not found in cache nmspc = { name: field.as_marshmallow_field(mongo_world=mongo_world) for name, field in self.fields.items() } name = 'Marshmallow%s' % type(self).__name__ # By default OO world returns `missing` fields as `None`, # disable this behavior here to let marshmallow deal with it if not mongo_world: nmspc['get_attribute'] = schema_from_umongo_get_attribute m_schema = type(name, (self.MA_BASE_SCHEMA_CLS, ), nmspc) # Add i18n support to the schema # We can't use I18nErrorDict here because __getitem__ is not called # when error_messages is updated with _default_error_messages. m_schema._default_error_messages = { k: _(v) for k, v in m_schema._default_error_messages.items()} self._marshmallow_schemas_cache[cache_key] = m_schema return m_schema
Example #20
Source File: test_data_proxy.py From umongo with MIT License | 5 votes |
def test_default(self): default_value = ObjectId('507f1f77bcf86cd799439011') default_callable = lambda: ObjectId('507f1f77bcf86cd799439012') class MySchema(Schema): no_default = fields.ObjectIdField() with_default = fields.ObjectIdField(default=default_value) with_callable_default = fields.ObjectIdField(default=default_callable) MyDataProxy = data_proxy_factory('My', MySchema()) d = MyDataProxy(data={}) assert d._data['no_default'] is ma.missing assert d._data['with_default'] == default_value assert d._data['with_callable_default'] == default_callable() assert d.get('no_default') is ma.missing assert d.get('with_default') == default_value assert d.get('with_callable_default') == default_callable() assert d.to_mongo() == { 'with_default': default_value, 'with_callable_default': default_callable(), } assert d.dump() == { 'with_default': str(default_value), 'with_callable_default': str(default_callable()), } d.delete('with_default') assert d._data['with_default'] == default_value assert d.get('with_default') == default_value d.delete('with_callable_default') assert d._data['with_callable_default'] == default_callable() assert d.get('with_callable_default') == default_callable()
Example #21
Source File: test_marshmallow.py From umongo with MIT License | 5 votes |
def test_marshmallow_access_custom_attributes(self): @self.instance.register class Doc(EmbeddedDocument): a = fields.IntField() attribute_foo = 'foo' @property def str_prop(self): return "I'm a property !" @property def none_prop(self): return None @property def missing_prop(self): return ma.missing class Schema(Doc.schema.as_marshmallow_schema()): str_prop = ma.fields.Str(dump_only=True) none_prop = ma.fields.Str(allow_none=True, dump_only=True) missing_prop = ma.fields.Str(dump_only=True) attribute_foo = ma.fields.Str(dump_only=True) assert Schema().dump(Doc(a=1)) == { 'a': 1, 'str_prop': "I'm a property !", 'none_prop': None, 'attribute_foo': 'foo', }
Example #22
Source File: marshmallow_to_swagger.py From flask-rebar with MIT License | 5 votes |
def get_default(self, obj, context): if ( obj.missing is not m.missing # Marshmallow accepts a callable for the default. This is tricky # to handle, so let's just ignore this for now. and not callable(obj.missing) ): return obj.missing else: return UNSET
Example #23
Source File: __init__.py From marshmallow_dataclass with MIT License | 5 votes |
def _get_field_default(field: dataclasses.Field): """ Return a marshmallow default value given a dataclass default value >>> _get_field_default(dataclasses.field()) <marshmallow.missing> """ # Remove `type: ignore` when https://github.com/python/mypy/issues/6910 is fixed default_factory = field.default_factory # type: ignore if default_factory is not dataclasses.MISSING: return default_factory elif field.default is dataclasses.MISSING: return marshmallow.missing return field.default
Example #24
Source File: jit.py From toasted-marshmallow with Apache License 2.0 | 5 votes |
def serialize(self, attr_name, field_symbol, assignment_template, field_obj): # type: (str, str, str, fields.Field) -> IndentedString body = IndentedString() if self.context.is_serializing: default_str = 'default' default_value = field_obj.default else: default_str = 'missing' default_value = field_obj.missing if field_obj.required: body += assignment_template.format('obj["{attr_name}"]'.format( attr_name=attr_name)) return body if default_value == missing: body += 'if "{attr_name}" in obj:'.format(attr_name=attr_name) with body.indent(): body += assignment_template.format('obj["{attr_name}"]'.format( attr_name=attr_name)) else: if callable(default_value): default_str += '()' body += assignment_template.format( 'obj.get("{attr_name}", {field_symbol}__{default_str})'.format( attr_name=attr_name, field_symbol=field_symbol, default_str=default_str)) return body
Example #25
Source File: jit.py From toasted-marshmallow with Apache License 2.0 | 5 votes |
def generate_unmarshall_method(schema, context=missing): context = context or JitContext() context.is_serializing = False return generate_marshall_method(schema, context)
Example #26
Source File: base.py From marshmallow-jsonschema with MIT License | 5 votes |
def _from_python_type(self, obj, field, pytype): """Get schema definition from python type.""" json_schema = {"title": field.attribute or field.name} for key, val in PY_TO_JSON_TYPES_MAP[pytype].items(): json_schema[key] = val if field.dump_only: json_schema["readonly"] = True if field.default is not missing: json_schema["default"] = field.default if field.allow_none: previous_type = json_schema["type"] json_schema["type"] = [previous_type, "null"] # NOTE: doubled up to maintain backwards compatibility metadata = field.metadata.get("metadata", {}) metadata.update(field.metadata) for md_key, md_val in metadata.items(): if md_key in ("metadata", "name"): continue json_schema[md_key] = md_val if isinstance(field, fields.List): json_schema["items"] = self._get_schema_for_field(obj, list_inner(field)) return json_schema
Example #27
Source File: test_converter.py From marshmallow-annotations with MIT License | 5 votes |
def test_defaults_missing(registry_): class FieldDefaultConverter(BaseConverter): def _get_field_defaults(self, item): return {"points": [1.0, 2.0]} converter = FieldDefaultConverter(registry=registry_) generated_fields = converter.convert_all(SomeType) assert generated_fields["id"].missing == missing assert generated_fields["name"].missing is None assert generated_fields["points"].missing == [1.0, 2.0]
Example #28
Source File: test_converter.py From marshmallow-annotations with MIT License | 5 votes |
def test_override_missing(registry_): converter = BaseConverter(registry=registry_) named_options = {"points": {"missing": list}, "name": {"missing": "a"}} generated_fields = converter.convert_all(SomeType, configs=named_options) assert generated_fields["id"].missing == missing assert generated_fields["name"].missing == "a" assert generated_fields["points"].missing == list
Example #29
Source File: filtering.py From flask-resty with MIT License | 5 votes |
def get_default_filter(self, view): field = self.get_field(view) if field.required: raise ApiError(400, {"code": "invalid_filter.missing"}) value = field.missing() if callable(field.missing) else field.missing if value is marshmallow.missing: return None return self.get_element_filter(view, value)
Example #30
Source File: filtering.py From flask-resty with MIT License | 5 votes |
def __init__( self, column_name=None, operator=None, *, required=False, missing=marshmallow.missing, validate=True, **kwargs, ): super().__init__(**kwargs) if operator is None and callable(column_name): operator = column_name column_name = None if not operator: raise TypeError("must specify operator") self._has_explicit_column_name = column_name is not None self._column_name = column_name self._operator = operator self._fields = {} self._required = required self._missing = missing self._validate = validate