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

Inconsistent behavior of Field(include=<...>) between v1 and v2 #5714

Closed
dmontagu opened this issue May 8, 2023 · 2 comments
Closed

Inconsistent behavior of Field(include=<...>) between v1 and v2 #5714

dmontagu opened this issue May 8, 2023 · 2 comments
Assignees

Comments

@dmontagu
Copy link
Contributor

dmontagu commented May 8, 2023

The following code has different behavior in v1 and v2:

from pydantic import BaseModel, Field

class Inner(BaseModel):
    x: int

class Outer(BaseModel):
    inner: Inner = Field(include={})

print(Outer(inner=Inner(x=1)).model_dump_json())
#> v1: {"inner": {}}
#> v2: {"inner":{"x":1}}

I haven't checked, but I'm assuming the exclude keyword argument to Field is also affected. As far as I can tell, this seems to be the primary purpose of the include/exclude arguments (now that Config.fields is gone), so it may be worth removing them if we don't plan to fix this.

@robertofd1995
Copy link

robertofd1995 commented Aug 3, 2023

This relates also the this other bug #6736

And I can confirm that exclude is also affected as it can be seeing in the other issue.

This not only affects the model_dump_json but also model_json_schema

I think this needs to be fixed and not removed from the library, since pretty much all the types I have work with pydantic I had to exclude some attributes out (either from the public schema or from the output dict).

Also notice that if the field is mark as private (e.x _inner) in order to exclude it, with callable attribute it adds self as first argument, which makes the function fail since the first expected argument would be always self.

Is anyone working in a fix for this?

@sydney-runkle
Copy link
Member

This is no longer relevant, as we deprecated the include param to the Field() constructor. See #6852

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

No branches or pull requests

4 participants