Python marshmallow.fields.List() Examples

The following are 30 code examples of marshmallow.fields.List(). 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.fields , or try the search function .
Example #1
Source File: test_errors.py    From flask-rebar with MIT License 9 votes vote down vote up
def create_app(self):
        app = Flask(__name__)
        Rebar().init_app(app=app)

        class NestedSchema(validation.RequestSchema):
            baz = fields.List(fields.Integer())

        class Schema(validation.RequestSchema):
            foo = fields.Integer(required=True)
            bar = fields.Email()
            nested = fields.Nested(NestedSchema)

        @app.route("/stuffs", methods=["POST"])
        def json_body_handler():
            data = get_json_body_params_or_400(schema=Schema)
            return response(data)

        return app 
Example #2
Source File: decorators_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_multidict_list_values_interpreted_correctly(app, client):  # noqa
    class TestSchema(Schema):
        name = fields.List(fields.String(), required=True)

    multidict = MultiDict([
        ("name", "value"),
        ("new_value", "still_here")
    ])
    result = _convert_multidict_values_to_schema(multidict, TestSchema())

    # `name` should be converted to a list.
    assert result["name"] == ["value"]

    # `new_value` should *not* be removed here, even though it"s not in the schema.
    assert result["new_value"] == "still_here"

    # Also makes sure handling a list with >1 values also works.
    multidict = MultiDict([
        ("name", "value"),
        ("name", "value2"),
    ])
    result = _convert_multidict_values_to_schema(multidict, TestSchema())
    assert result["name"] == ["value", "value2"] 
Example #3
Source File: base.py    From pyrh with MIT License 6 votes vote down vote up
def _process_dict_values(value: Any) -> Any:
    """Process a returned from a JSON response.

    Args:
        value: A dict, list, or value returned from a JSON response.

    Returns:
        Either an UnknownModel, a List of processed values, or the original value \
            passed through.

    """
    if isinstance(value, Mapping):
        return UnknownModel(**value)
    elif isinstance(value, list):
        return [_process_dict_values(v) for v in value]
    else:
        return value 
Example #4
Source File: test_related.py    From flask-resty with MIT License 6 votes vote down vote up
def schemas():
    class ParentSchema(Schema):
        id = fields.Integer(as_string=True)
        name = fields.String(required=True)

        children = RelatedItem("ChildSchema", many=True, exclude=("parent",))
        child_ids = fields.List(fields.Integer(as_string=True), load_only=True)

    class ChildSchema(Schema):
        @classmethod
        def get_query_options(cls, load):
            return (load.joinedload("parent"),)

        id = fields.Integer(as_string=True)
        name = fields.String(required=True)

        parent = RelatedItem(
            ParentSchema, exclude=("children",), allow_none=True
        )
        parent_id = fields.Integer(
            as_string=True, allow_none=True, load_only=True
        )

    return {"parent": ParentSchema(), "child": ChildSchema()} 
Example #5
Source File: test_args.py    From flask-resty with MIT License 6 votes vote down vote up
def schemas():
    class NameSchema(Schema):
        name = fields.String(required=True)

    class NameListSchema(Schema):
        names = fields.List(fields.String(), data_key="name", required=True)

    class NameDelimitedListSchema(Schema):
        names = DelimitedList(fields.String(), data_key="name", required=True)

    class NameDefaultSchema(Schema):
        name = fields.String(missing="foo")

    return {
        "name": NameSchema(),
        "name_list": NameListSchema(),
        "name_delimited_list": NameDelimitedListSchema(),
        "name_default": NameDefaultSchema(),
    } 
Example #6
Source File: test_one_of_schema.py    From marshmallow-oneofschema with MIT License 6 votes vote down vote up
def test_using_as_nested_schema(self):
        class SchemaWithList(m.Schema):
            items = f.List(f.Nested(MySchema))

        schema = SchemaWithList()
        result = schema.load(
            {
                "items": [
                    {"type": "Foo", "value": "hello world!"},
                    {"type": "Bar", "value": 123},
                ]
            }
        )
        assert {"items": [Foo("hello world!"), Bar(123)]} == result

        with pytest.raises(m.ValidationError) as exc_info:
            schema.load(
                {"items": [{"type": "Foo", "value": "hello world!"}, {"value": 123}]}
            )
        assert {"items": {1: {"type": [REQUIRED_ERROR]}}} == exc_info.value.messages 
Example #7
Source File: decorators_test.py    From flask_accepts with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_accepts_with_postional_args_query_params_schema_and_header_schema(app, client):  # noqa
    class QueryParamsSchema(Schema):
        query_param = fields.List(fields.String(), required=True)

    class HeadersSchema(Schema):
        Header = fields.Integer(required=True)

    @app.route("/test")
    @accepts(
        dict(name="foo", type=int, help="An important foo"),
        query_params_schema=QueryParamsSchema,
        headers_schema=HeadersSchema)
    def test():
        assert request.parsed_args["foo"] == 3
        assert request.parsed_query_params["query_param"] == ["baz", "qux"]
        assert request.parsed_headers["Header"] == 3
        return "success"

    with client as cl:
        resp = cl.get("/test?foo=3&query_param=baz&query_param=qux", headers={"Header": "3"})
        assert resp.status_code == 200 
Example #8
Source File: schema.py    From py-ipv8 with GNU Lesser General Public License v3.0 6 votes vote down vote up
def schema(**kwargs):
    """
    Create a schema. Mostly useful for creating single-use schemas on-the-fly.
    """
    items = list(kwargs.items())
    if len(items) != 1 or not isinstance(items[0][1], dict):
        raise RuntimeError('schema required 1 keyword argument of type dict')
    name, spec = items[0]

    schema_dict = {}
    for key, value in spec.items():
        cls, description = value if isinstance(value, tuple) else (value, None)
        required = key.endswith('*')
        key = key.rstrip('*')
        kwargs = {'required': required, 'description': description}

        if isinstance(cls, SchemaMeta):
            schema_dict[key] = Nested(cls, required=required)
        elif isinstance(cls, list) and len(cls) == 1:
            cls = cls[0]
            schema_dict[key] = List(Nested(cls), **kwargs) if isinstance(cls, SchemaMeta) else List(cls, **kwargs)
        else:
            schema_dict[key] = cls.__call__(**kwargs) if callable(cls) else cls
    return type(name, (Schema,), schema_dict) 
Example #9
Source File: test_class_schema.py    From marshmallow_dataclass with MIT License 6 votes vote down vote up
def test_use_type_mapping_from_base_schema(self):
        class CustomType:
            pass

        class CustomField(Field):
            pass

        class CustomListField(ListField):
            pass

        class BaseSchema(Schema):
            TYPE_MAPPING = {CustomType: CustomField, typing.List: CustomListField}

        @dataclasses.dataclass
        class WithCustomField:
            custom: CustomType
            custom_list: typing.List[float]
            uuid: UUID
            n: int

        schema = class_schema(WithCustomField, base_schema=BaseSchema)()
        self.assertIsInstance(schema.fields["custom"], CustomField)
        self.assertIsInstance(schema.fields["custom_list"], CustomListField)
        self.assertIsInstance(schema.fields["uuid"], UUIDField)
        self.assertIsInstance(schema.fields["n"], Integer) 
Example #10
Source File: metadata_extensions.py    From invenio-app-ils with MIT License 6 votes vote down vote up
def _validate_marshmallow_type(self, field_cfg):
        """Make sure the Marshmallow type is one we support."""
        def validate_basic_marshmallow_type(_type):
            allowed_types = [
                Bool, DateString, Integer, SanitizedUnicode
            ]
            assert any([
                isinstance(_type, allowed_type) for allowed_type
                in allowed_types
            ])

        marshmallow_type = field_cfg["marshmallow"]
        if isinstance(marshmallow_type, List):
            validate_basic_marshmallow_type(marshmallow_type.inner)
        else:
            validate_basic_marshmallow_type(marshmallow_type) 
Example #11
Source File: test_dump.py    From marshmallow-jsonschema with MIT License 6 votes vote down vote up
def test_list_nested():
    """Test that a list field will work with an inner nested field."""

    class InnerSchema(Schema):
        foo = fields.Integer(required=True)

    class ListSchema(Schema):
        bar = fields.List(fields.Nested(InnerSchema), required=True)

    schema = ListSchema()
    dumped = validate_and_dump(schema)

    nested_json = dumped["definitions"]["ListSchema"]["properties"]["bar"]

    assert nested_json["type"] == "array"
    assert "items" in nested_json

    item_schema = nested_json["items"]
    assert "InnerSchema" in item_schema["$ref"] 
Example #12
Source File: routes.py    From aries-cloudagent-python with Apache License 2.0 6 votes vote down vote up
def revocation_registries_created(request: web.BaseRequest):
    """
    Request handler to get revocation registries that current agent created.

    Args:
        request: aiohttp request object

    Returns:
        List of identifiers of matching revocation registries.

    """
    context = request.app["request_context"]

    search_tags = [
        tag for tag in vars(RevRegsCreatedQueryStringSchema)["_declared_fields"]
    ]
    tag_filter = {
        tag: request.query[tag] for tag in search_tags if tag in request.query
    }
    found = await IssuerRevRegRecord.query(context, tag_filter)

    return web.json_response({"rev_reg_ids": [record.revoc_reg_id for record in found]}) 
Example #13
Source File: test_field_for_schema.py    From marshmallow_dataclass with MIT License 6 votes vote down vote up
def test_override_container_type_with_type_mapping(self):
        type_mapping = [
            (List, fields.List, List[int]),
            (Dict, fields.Dict, Dict[str, int]),
            (Tuple, fields.Tuple, Tuple[int, str, bytes]),
        ]
        for base_type, marshmallow_field, schema in type_mapping:

            class MyType(marshmallow_field):
                ...

            self.assertIsInstance(field_for_schema(schema), marshmallow_field)

            class BaseSchema(Schema):
                TYPE_MAPPING = {base_type: MyType}

            self.assertIsInstance(
                field_for_schema(schema, base_schema=BaseSchema), MyType
            ) 
Example #14
Source File: agent_config.py    From polyaxon with Apache License 2.0 5 votes vote down vote up
def secrets(self) -> List[V1K8sResourceType]:
        if self._secrets or not self._all_connections:
            return self._secrets
        secret_names = set()
        secrets = []
        for c in self._all_connections:
            if c.secret and c.secret.name not in secret_names:
                secret_names.add(c.secret.name)
                secrets.append(c.get_secret())
        self._secrets = secrets
        return self._secrets 
Example #15
Source File: agent_config.py    From polyaxon with Apache License 2.0 5 votes vote down vote up
def config_maps(self) -> List[V1K8sResourceType]:
        if self._config_maps or not self._all_connections:
            return self._config_maps
        config_map_names = set()
        config_maps = []
        for c in self._all_connections:
            if c.config_map and c.config_map.name not in config_map_names:
                config_map_names.add(c.config_map.name)
                config_maps.append(c.get_config_map())
        self._config_maps = config_maps
        return self._config_maps 
Example #16
Source File: mm.py    From dataclasses-json with MIT License 5 votes vote down vote up
def loads(self, json_data: JsonData,  # type: ignore
                  many: bool = True, partial: bool = None, unknown: str = None,
                  **kwargs) -> typing.List[A]:
            # ignore the mypy error of the decorator because mm does not define bytes as correct input data
            # mm has the wrong return type annotation (dict) so we can ignore the mypy error
            # for the return type overlap
            pass 
Example #17
Source File: schemas.py    From polyaxon with Apache License 2.0 5 votes vote down vote up
def chunk_logs(cls, logs: List[V1Log]):
        total_size = len(logs)
        for i in range(0, total_size, cls.CHUNK_SIZE):
            yield cls(logs=logs[i : i + cls.CHUNK_SIZE]) 
Example #18
Source File: io.py    From polyaxon with Apache License 2.0 5 votes vote down vote up
def validate_io_value(
    name: str,
    iotype: str,
    value: Any,
    default: Any,
    is_optional: bool,
    is_list: bool,
    options: List[Any],
    parse: bool = True,
):
    try:
        parsed_value = parser.TYPE_MAPPING[iotype](
            key=name,
            value=value,
            is_list=is_list,
            is_optional=is_optional,
            default=default,
            options=options,
        )
        if parse:
            return parsed_value
        # Return the original value, the parser will return specs sometimes
        if value is not None:
            return value
        return default
    except PolyaxonSchemaError as e:
        raise ValidationError(
            "Could not parse value `%s`, an error was encountered: %s" % (value, e)
        ) 
Example #19
Source File: api.py    From raiden-services with MIT License 5 votes vote down vote up
def create_and_store_feedback_tokens(
    pathfinding_service: PathfindingService,
    token_network_address: TokenNetworkAddress,
    routes: List[Path],
) -> FeedbackToken:
    feedback_token = FeedbackToken(token_network_address=token_network_address)

    # TODO: use executemany here
    for route in routes:
        pathfinding_service.database.prepare_feedback(
            token=feedback_token, route=route.nodes, estimated_fee=route.estimated_fee
        )

    return feedback_token 
Example #20
Source File: test_schema.py    From swagger-marshmallow-codegen with MIT License 5 votes vote down vote up
def test_dump_specific_list(self):
        from marshmallow import Schema

        class X(Schema):
            v = fields.Integer(required=True)

        class S(self._getTargetClass()):
            name = fields.String()

            class Meta:
                additional_field = fields.List(fields.Nested(X))

        C = namedtuple("C", "value, expected, ok")
        candidates = [
            C(
                value={"name": "foo", "xs": [], "ys": [{"v": "100"}]},
                expected={"name": "foo", "xs": [], "ys": [{"v": 100}]},
                ok=True,
            ),
            C(
                value={"name": "foo", "xs": [{"v": "100", "*another*": "*another*"}]},
                expected={"name": "foo", "xs": [{"v": 100}]},
                ok=True,
            ),
        ]
        for c in candidates:
            with self.subTest(value=c.value, ok=c.ok):
                self.assert_value(lambda: S().dump(c.value), c) 
Example #21
Source File: test_schema.py    From swagger-marshmallow-codegen with MIT License 5 votes vote down vote up
def test_load_specific_list(self):
        from marshmallow import Schema

        class X(Schema):
            v = fields.Integer(required=True)

        class S(self._getTargetClass()):
            name = fields.String()

            class Meta:
                additional_field = fields.List(fields.Nested(X))

        C = namedtuple("C", "value, expected, ok")
        candidates = [
            C(
                value={"name": "foo", "xs": []},
                expected={"name": "foo", "xs": []},
                ok=True,
            ),
            C(
                value={"name": "foo", "xs": [{"v": "100"}]},
                expected={"name": "foo", "xs": [{"v": 100}]},
                ok=True,
            ),
            C(value={"name": "foo", "xs": [{}]}, expected=None, ok=False),
        ]
        for c in candidates:
            with self.subTest(value=c.value, ok=c.ok):
                self.assert_value(lambda: S().load(c.value), c) 
Example #22
Source File: mm.py    From dataclasses-json with MIT License 5 votes vote down vote up
def dump(self, obj: typing.List[A], many: bool = None) -> typing.List[
            TEncoded]:  # type: ignore
            # mm has the wrong return type annotation (dict) so we can ignore the mypy error
            pass 
Example #23
Source File: test_schema.py    From swagger-marshmallow-codegen with MIT License 5 votes vote down vote up
def test_dump_list(self):
        class S(self._getTargetClass()):
            class schema_class(Schema):
                value = fields.List(fields.Integer(), required=True)

        C = namedtuple("C", "value, expected, ok")
        candidates = [
            C(value=["10"], expected=[10], ok=True),
            C(value=["10", "20"], expected=[10, 20], ok=True),
            C(value=["foo", "bar"], expected=None, ok=False),
        ]
        for c in candidates:
            with self.subTest(value=c.value, ok=c.ok):
                self.assert_dump_value(lambda: S().dump(c.value), c) 
Example #24
Source File: test_schema.py    From swagger-marshmallow-codegen with MIT License 5 votes vote down vote up
def test_load_list(self):
        class S(self._getTargetClass()):
            class schema_class(Schema):
                value = fields.List(fields.Integer(), required=True)

        C = namedtuple("C", "value, expected, ok")
        candidates = [
            C(value=["10"], expected=[10], ok=True),
            C(value=["10", "20"], expected=[10, 20], ok=True),
            C(value=["foo", "bar"], expected=None, ok=False),
        ]
        for c in candidates:
            with self.subTest(value=c.value, ok=c.ok):
                self.assert_load_value(lambda: S().load(c.value), c) 
Example #25
Source File: test_validation.py    From flask-rebar with MIT License 5 votes vote down vote up
def test_serialize_errors(self):
        with self.assertRaises(ValidationError) as ctx:
            compat.dump(IntegerList(), {"foos": [1, "two"]})

        self.assertEqual(
            ctx.exception.messages,
            {
                # Marshmallow's fields.List formats the dump errors differently
                # than load :shrug:
                "foos": ["Not a valid integer."]
            },
        ) 
Example #26
Source File: mm.py    From dataclasses-json with MIT License 5 votes vote down vote up
def dumps(self, obj: typing.List[A], many: bool = None, *args,
                  **kwargs) -> str:
            pass 
Example #27
Source File: test_field_for_schema.py    From marshmallow_dataclass with MIT License 5 votes vote down vote up
def test_builtin_list(self):
        self.assertFieldsEqual(
            field_for_schema(list, metadata=dict(required=False)),
            fields.List(fields.Raw(required=True, allow_none=True), required=False),
        ) 
Example #28
Source File: mm.py    From dataclasses-json with MIT License 5 votes vote down vote up
def load(self, data: typing.List[TEncoded],
                 many: bool = True, partial: bool = None,
                 unknown: str = None) -> \
                typing.List[A]:
            # ignore the mypy error of the decorator because mm does not define lists as an allowed input type
            pass 
Example #29
Source File: test_converter.py    From marshmallow-annotations with MIT License 5 votes vote down vote up
def test_passes_interior_options_to_list_subtype(registry_):
    converter = BaseConverter(registry=registry_)

    opts = {"_interior": {"as_string": True}}
    field = converter.convert(typing.List[int], opts)

    assert field.container.as_string 
Example #30
Source File: test_dump.py    From marshmallow-jsonschema with MIT License 5 votes vote down vote up
def test_list():
    class ListSchema(Schema):
        foo = fields.List(fields.String(), required=True)

    schema = ListSchema()
    dumped = validate_and_dump(schema)

    nested_json = dumped["definitions"]["ListSchema"]["properties"]["foo"]
    assert nested_json["type"] == "array"
    assert "items" in nested_json

    item_schema = nested_json["items"]
    assert item_schema["type"] == "string"