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

RecursionError: maximum recursion depth exceeded while calling a Python object #2183

Open
Nagaraj4896 opened this issue Sep 11, 2023 · 1 comment

Comments

@Nagaraj4896
Copy link

Nagaraj4896 commented Sep 11, 2023

I am getting RecursionError: maximum recursion depth exceeded while calling a Python object error for following code, Is there any other way to selecting the schema based on a value

import marshmallow
from marshmallow import fields, post_load

class FoodSchema(marshmallow.Schema):
    food_type = fields.String()

    @post_load
    def make_food(self, data, **kwargs):
        food_type = data.get('food_type', '').lower()
        if food_type == 'fruit':
            return FruitSchema().load(data)
        elif food_type == 'vegetable':
            return VegetableSchema().load(data)
        elif food_type == 'meat':
            return MeatSchema().load(data)
        else:
            raise ValueError("Invalid food type")

class FruitSchema(FoodSchema):
    # Add fields specific to the Fruit class here

class VegetableSchema(FoodSchema):
    # Add fields specific to the Vegetable class here

class MeatSchema(FoodSchema):
    # Add fields specific to the Meat class here

print(FoodSchema().load({'food_type':'fruit'}))
@lafrech
Copy link
Member

lafrech commented Sep 11, 2023

You may want to have a look at marshmallow-onfofschema, although I'm not sure it works at schema top-level. It might be limited to polymorphic fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants