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

model_validate returns None #7055

Closed
1 task done
shadycuz opened this issue Aug 9, 2023 · 3 comments
Closed
1 task done

model_validate returns None #7055

shadycuz opened this issue Aug 9, 2023 · 3 comments
Assignees
Labels
bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable

Comments

@shadycuz
Copy link

shadycuz commented Aug 9, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

When using MyModel.model_validate, it returns None.

This works:

@classmethod
    def create_example(cls) -> str:
        example_data = {
            "StackName": "my-stack",
            "TemplateURL": "https://my-bucket.s3.amazonaws.com/my-template.yaml",
            "Parameters": {"TemplatesBucket": "FAKE"},
        }
        example_config = cls(**example_data)

        log.debug(f"Example config:\n{example_config}")

        model_data = example_config.model_dump(exclude={"TemplateURL", "TemplatePath", "ParameterGroups"})
        model_data["Parameters"] = {}
        return yaml.dump(model_data, indent=4, sort_keys=True)

This doesn't:

@classmethod
    def create_example(cls) -> str:
        example_data = {
            "StackName": "my-stack",
            "TemplateURL": "https://my-bucket.s3.amazonaws.com/my-template.yaml",
            "Parameters": {"TemplatesBucket": "FAKE"},
        }
        example_config = cls.model_validate(example_data)

        log.debug(f"Example config:\n{example_config}")

        model_data = example_config.model_dump(exclude={"TemplateURL", "TemplatePath", "ParameterGroups"})
        model_data["Parameters"] = {}
        return yaml.dump(model_data, indent=4, sort_keys=True)

output:

debug: Example config:
debug: None
Traceback (most recent call last):
  File "/workspaces/nvcloud/.venv/bin/nvcloud", line 6, in <module>
    sys.exit(cli())
  File "/workspaces/nvcloud/.venv/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/workspaces/nvcloud/.venv/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/workspaces/nvcloud/.venv/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/workspaces/nvcloud/.venv/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/workspaces/nvcloud/.venv/lib/python3.10/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/workspaces/nvcloud/.venv/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/workspaces/nvcloud/.venv/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/workspaces/nvcloud/nvcloud/config/commands.py", line 51, in create_example
    log.info(config_class.create_example())
  File "/workspaces/nvcloud/nvcloud/config/models.py", line 341, in create_example
    model_data = example_config.model_dump(exclude={"TemplateURL", "TemplatePath", "ParameterGroups"})
AttributeError: 'NoneType' object has no attribute 'model_dump'

Example Code

No response

Python, Pydantic & OS Version

pydantic version: 2.1.1
        pydantic-core version: 2.4.0
          pydantic-core build: profile=release pgo=true mimalloc=true
                 install path: /workspaces/nvcloud/.venv/lib/python3.10/site-packages/pydantic
               python version: 3.10.12 (main, Jun  7 2023, 19:32:10) [GCC 10.2.1 20210110]
                     platform: Linux-5.15.90.1-microsoft-standard-WSL2-x86_64-with-glibc2.31
     optional deps. installed: ['typing-extensions']

Selected Assignee: @adriangb

Edit: My use case for model_validate() is I need to provide context for some of my field validators.

@shadycuz shadycuz added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Aug 9, 2023
@Viicos
Copy link
Contributor

Viicos commented Aug 9, 2023

Could you please provide more info/a MRE? I can't reproduce:

from pydantic import BaseModel

class ParametersModel(BaseModel):
    TemplatesBucket: str

class Model(BaseModel):
    StackName: str
    TemplateURL: str
    Parameters: ParametersModel

    @classmethod
    def create_example(cls) -> str:
        example_data = {
            "StackName": "my-stack",
            "TemplateURL": "https://my-bucket.s3.amazonaws.com/my-template.yaml",
            "Parameters": {"TemplatesBucket": "FAKE"},
        }
        example_config = cls.model_validate(example_data)
        print(example_config)

Model.create_example()
#> StackName='my-stack' TemplateURL='https://my-bucket.s3.amazonaws.com/my-template.yaml' Parameters=ParametersModel(TemplatesBucket='FAKE')

@Kludex
Copy link
Member

Kludex commented Aug 25, 2023

Since we can't reproduce, I'll be closing the issue.

@shadycuz Please create a new issue with a reproducible example. 🙏

@Kludex Kludex closed this as not planned Won't fix, can't repro, duplicate, stale Aug 25, 2023
@TimChild
Copy link

TimChild commented Apr 27, 2024

I have also come across this behaviour. Unfortunately, the place I am seeing it is pretty complicated (lots of subclassing and mixins) so I can't easily give a MWE.
The problem has only appeared after a pretty major refactor that I didn't think would affect my pydantic schema too much, but obviously I changed something...

Anyway, just commenting to show that this is not a totally isolated case, but I'm also pretty confident that I've done something stupid somewhere so I don't think it's worth re-opening...

Edit:
Confirmed, I did something stupid... Accidentally forgot to return self from a model_validator...
See #9334 for example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable
Projects
None yet
Development

No branches or pull requests

5 participants