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

fix: required fields in nested pydantic models need to follow parent nullability #2199

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

Conversation

joscha
Copy link
Contributor

@joscha joscha commented Jan 8, 2025

Description

Fields of nested pydantic models that are not kept as JSON via skip_nested_types=True need to follow the "requiredness" of their parent.

Example:

class A(BaseModel):
  id: int

class B(BaseModel):
  id: int
  child_a: Optional[A]
  dlt_config: ClassVar[DltConfig] = {"skip_nested_types": True}

results in a table that looks like this:

table B:

name nullable
id false
child_a__id false

which is incorrect as I can serialize an instance of B that does not have a child_a, and thus, by extension, no id for child_a.

The change in this pull request retains the nullability of the field that contains the nested model, effectively making any required field of a nested, flattened model nullable as well if the parent model field is optional, i.e. the resulting table with the change in this PR is:

table B:

name nullable
id false
child_a__id true

If we agree this is a bug and the fix in this PR is the way to go I can add a test & fix for deeply nested models as well.

Copy link

netlify bot commented Jan 8, 2025

Deploy Preview for dlt-hub-docs canceled.

Name Link
🔨 Latest commit 4855ec6
🔍 Latest deploy log https://app.netlify.com/sites/dlt-hub-docs/deploys/67b39fd70535820008c7efea

@sh-rp
Copy link
Collaborator

sh-rp commented Jan 9, 2025

@rudolfix I think it makes sense to forward the nullability to flattened fields, wdyt?

@rudolfix rudolfix requested a review from sh-rp January 14, 2025 14:42
Copy link
Collaborator

@sh-rp sh-rp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just small changes, then we can merge this.



def test_parent_nullable_means_children_nullable():
class MyParent(BaseModel):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joscha could you add the reverse test? Another field with a non optional child model and test that this is not nullable? Then make sure the conflicts are solved and the linter passes, then we can merge this :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes, good point, will do!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inverse test in 74af54c

@joscha joscha force-pushed the joscha/pydantic-model-nested-must-be-nullable branch from 0212eb0 to 74af54c Compare February 10, 2025 16:16
@joscha
Copy link
Contributor Author

joscha commented Feb 10, 2025

Changes are in @sh-rp. Unsure about the failing dest tests - maybe an access issue? I can't see how my change would have affected them?

@joscha joscha requested a review from sh-rp February 10, 2025 16:53
@sh-rp
Copy link
Collaborator

sh-rp commented Feb 17, 2025

@joscha looks good to me, thanks! Could you merge in devel (or rebase) to make sure the tests pass?

@joscha

This comment was marked as outdated.

@joscha
Copy link
Contributor Author

joscha commented Feb 17, 2025

@joscha looks good to me, thanks! Could you merge in devel (or rebase) to make sure the tests pass?

all green @sh-rp

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.

2 participants