question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unknown Excluded in sublists

See original GitHub issue

Hi,

I am quite new to the marshmallow ecosystem. I am considering the use of desert as a way to write python API clients by explicitly describing their data structure to ease the deserialisation.

In the process of writing an API Client for a specific purpose one could be confronted to a lot of unnecessary data in API responses that he does not intend to use. Exploring this data and writing up the schemas while continuously testing, a way to build such code could be to default to marshmallow EXCLUDE for unknown fields. That way, continuous TDD with “deserialisation of real life JSONS” tests would be possible without ValidationError while not having to describe unused chunks of the data.

Furthermore, continuing to use EXCLUDE after this initial stage of development could be acceptable, if the policy of the API itself is to authorize adding of values for minor changes. One could want to make its API client future-proof by not breaking when such a change happens on the other side that he does not necessarily controls.

But this behavior is broken when deserialising unknown fields in a List:

from dataclasses import dataclass
from typing import List

import desert
import marshmallow


@dataclass
class Ambiguous:
    firstname: str
    lastname: str


@dataclass
class AmbiguousList:
    id: str
    not_yet_known: List[Ambiguous]


AmbiguousListSchema = desert.schema_class(AmbiguousList, meta={"unknown": marshmallow.EXCLUDE})()

AmbiguousListSchema.loads("""
{
    "id":"789456132",
    "not_yet_known": [
        {
            "firstname": "John",
            "lastname": "Smith"
        },
        {
            "firstname": "Henry",
            "lastname": "Smith",
        }
    ]
}
""")
# Ok

AmbiguousListSchema.loads("""
{
    "id":"789456132",
    "shiny_new_arg": "uninteresting info",
    "not_yet_known": [
        {
            "firstname": "John",
            "lastname": "Smith"
        },
        {
            "firstname": "Henry",
            "lastname": "Smith"
        }
    ]
}
""")
# Still Ok

AmbiguousListSchema.loads("""
{
    "id":"789456132",
    "shiny_new_arg": "uninteresting info",
    "not_yet_known": [
        {
            "firstname": "John",
            "lastname": "Smith"
        },
        {
            "firstname": "Henry",
            "lastname": "Smith",
            "shiny_new_arg": "uninteresting info"
        }
    ]
}
""")
# marshmallow.exceptions.ValidationError: {'not_yet_known': {1: {'shiny_new_arg': ['Unknown field.']}}}

I am not sure what is the library that is responsible. It seems that marshmallow_dataclass shares the same error.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Ouradzecommented, Mar 18, 2021

Hi, if it can help someone, I used this to make it work with desert and still keeping it not too convoluted.

@dataclass
class Child:
    name: str

@dataclass
class Parent:
    name: str
    dummy_list: List[str]
    children: List[Child] = field(
        default_factory=list,
        metadata=desert.metadata(
            m_fields.Nested(
                desert.schema(Child, meta={"unknown": EXCLUDE}, many=True), required=True
            )
        ),
    )

1reaction
wanderrfulcommented, Sep 4, 2020

There’s a similar discussion to this in the marshmallow-code repo: https://github.com/marshmallow-code/marshmallow/issues/1490

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Removing sublists from a list of lists - Stack Overflow
I'd like to be able to remove all the lists that are sublists of one of the other lists, for example I'd like...
Read more >
Add elements in a sublist - Mathematica Stack Exchange
Add elements in a sublist · Ask Question. Asked 10 years, 7 months ago ... How to sum up subelements of a list...
Read more >
How to get sublist of an ArrayList with example - BeginnersBook
In this tutorial, we will see how to get a sublist from an existing ArrayList. We will be using the subList() method of...
Read more >
Bounced Email - NetSuite Applications Suite
Excluding Predicted Risks · Setting Vendor-Specific Risk Confidence Settings · Predicted Risks Portlet ... Unknown User. Bad Domain. Address Error.
Read more >
List of SQL reserved words - Drupal
... STYLE; SUBCLASS_ORIGIN; SUBLIST; SUBMULTISET; SUBSTRING; SUCCESSFUL ... UNDER; UNDO; UNENCRYPTED; UNION; UNIQUE; UNKNOWN; UNLISTEN ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found