Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load accepts Sequence rather than Iterable (rejects generators) #2795

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from

Conversation

sloria
Copy link
Member

@sloria sloria commented Jan 19, 2025

addresses #1898

from marshmallow import Schema, fields, validates_schema


class MySchema(Schema):
    fld = fields.String()

    @validates_schema
    def _validate(self, data, **kwargs):
        print("schema validator called")


MySchema().load(({"fld": "abc"}, {"fld": "def"}), many=True)
# schema validator called
# schema validator called


def gen():
    yield from ({"fld": "abc"}, {"fld": "def"})


MySchema().load(gen(), many=True)
# Before this change, silently fails to call schema validator
# After this change, an error is raised:
# marshmallow.exceptions.ValidationError: {"_schema": ["Invalid input type."]}

Caveat: for some reason, mypy doesn't error when passing a generator. it seems that generators are covariant with Sequence and are therefore equivalent for type-checking purposes. not sure if there is a better way to handle this.

def gen():
    yield from ({"fld": "abc"}, {"fld": "def"})


assert isinstance(gen(), Sequence) is False
MySchema().load(gen(), many=True)  # mypy accepts this

@sloria
Copy link
Member Author

sloria commented Jan 19, 2025

ah, looks like this breaks handling of sets. will investigate later

update: fixed

@sloria
Copy link
Member Author

sloria commented Jan 20, 2025

this is ready for review. i thought this was a breaking change since it changes what we're accepting as valid input. but one could argue this is bugfix, since non-sequence inputs are silently failing to call schema validators. should this be backported to v3?

@sloria sloria requested a review from lafrech January 20, 2025 00:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant