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 schema references in discriminated unions #7646

Merged
merged 2 commits into from Sep 26, 2023

Conversation

adriangb
Copy link
Member

@adriangb adriangb commented Sep 26, 2023

Fixes #7611 (comment) (I hope)

@adriangb adriangb self-assigned this Sep 26, 2023
@adriangb adriangb added the relnotes-fix Used for bugfixes. label Sep 26, 2023
@cloudflare-pages
Copy link

cloudflare-pages bot commented Sep 26, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 3d78e2f
Status: ✅  Deploy successful!
Preview URL: https://f0398515.pydantic-docs2.pages.dev
Branch Preview URL: https://discriminated-union-refs-fix.pydantic-docs2.pages.dev

View logs

@adriangb
Copy link
Member Author

@Fokko can you try this branch? I tried it locally and can import files I could not before but don't know how to get your unit tests to run (missing env vars, etc.)

@Fokko
Copy link

Fokko commented Sep 26, 2023

@adriangb Thanks for the quick turnaround, this fixed it: apache/iceberg#8646

tnx

@adriangb adriangb merged commit b51804e into main Sep 26, 2023
60 checks passed
@adriangb adriangb deleted the discriminated-union-refs-fix branch September 26, 2023 18:14
@dmontagu
Copy link
Contributor

I think the following example reproduces the issue that this PR is trying to solve:

from typing import Literal, Annotated

from pydantic import BaseModel, Field, RootModel


class Model1(BaseModel):
    kind: Literal['1'] = '1'
    two: 'Model2 | None'


class Model2(BaseModel):
    kind: Literal['2'] = '2'
    one: Model1 | None


class Root1(RootModel[Model1]):
    pass


class Root2(RootModel[Model2]):
    pass


class Outer(BaseModel):
    first: Annotated[Root1 | Root2, Field(discriminator='kind')]
    second: Annotated[Root1 | Root2, Field(discriminator='kind')]

@Fokko
Copy link

Fokko commented Sep 26, 2023

@dmontagu Yes, that looks like it. We use the discriminator to distinguish between the different versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes-fix Used for bugfixes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Recurse KeyError when using several BaseModels
3 participants