Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improve error handling for in-evaluable refs for discriminator application#10440

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
sydney-runkle merged 5 commits intomainfromdisc-fix-alias
Oct 7, 2024

Conversation

sydney-runkle
Copy link
Contributor

@sydney-runklesydney-runkle commentedSep 18, 2024
edited
Loading

This was supposed toclose#8271, but right now it just improves the associated error.

My current hunch is that we need to do an apply discriminators step at the end of json schema building, like we do for core schemas, if we want this to work 😭. Not sure how worth the investment that is, at the moment.

I'd also be open to more creative solutions for evaluating refs for which we have not yet generated the schemas. Probably a good conversation to have with@Viicos and@adriangb.

MRE, at the moment:

fromenumimportEnumfromtypingimportAnnotated,Union,Literal,ListfrompydanticimportBaseModelasBaseModel,Field,TypeAdapterfrompydantic.fieldsimportFieldInfoimportjsonfrompydantic._internal._core_utilsimportpretty_print_core_schemaclassItemType(str,Enum):ITEM1='item1'ITEM2='item2'classCreateItem1(BaseModel):item_type:Annotated[Literal[ItemType.ITEM1],Field(alias='type')]id:intclassCreateItem2(BaseModel):item_type:Annotated[Literal[ItemType.ITEM2],Field(alias='type')]id:intclassCreateObjectDto(BaseModel):id:intitems:List[Annotated[Union[CreateItem1,CreateItem2,            ],Field(discriminator='item_type'),        ]    ]ta=TypeAdapter(Annotated[CreateObjectDto,FieldInfo(examples=[{'id':1,'items': [{'id':3,'type':'ITEM1'}]}])])print(json.dumps(ta.json_schema(),indent=2))print(pretty_print_core_schema(ta.core_schema))

@codspeed-hqCodSpeed HQ
Copy link

codspeed-hqbot commentedSep 18, 2024
edited
Loading

CodSpeed Performance Report

Merging#10440 willnot alter performance

Comparingdisc-fix-alias (113414a) withmain (eda3eb4)

Summary

✅ 38 untouched benchmarks

@github-actionsGitHub Actions
Copy link
Contributor

github-actionsbot commentedSep 18, 2024
edited
Loading

Coverage report

This PR does not seem to contain any modification to coverable code.

@cloudflare-workers-and-pagesCloudflare Workers and Pages
Copy link

cloudflare-workers-and-pagesbot commentedOct 3, 2024
edited
Loading

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit:113414a
Status: ✅  Deploy successful!
Preview URL:https://b1d5d5ab.pydantic-docs.pages.dev
Branch Preview URL:https://disc-fix-alias.pydantic-docs.pages.dev

View logs

@@ -466,7 +466,6 @@ def populate_defs(core_schema: CoreSchema, json_schema: JsonSchemaValue) -> Json
core_ref = CoreRef(core_schema['ref']) # type: ignore[typeddict-item]
defs_ref, ref_json_schema = self.get_cache_defs_ref_schema(core_ref)
json_ref = JsonRef(ref_json_schema['$ref'])
self.json_to_defs_refs[json_ref] = defs_ref
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

This is done in theself.get_cache_defs_ref_schema(core_ref) call, so no reason to do that here.

while '$ref' in choice:
assert isinstance(choice['$ref'], str)
choice = self.get_schema_from_definitions(JsonRef(choice['$ref'])) or {}
choice = self.resolve_schema_to_update(choice)
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

This function handles the above removed logic more gracefully, with an intuitive error.

@sydney-runklesydney-runkle changed the titleWorking on fixing json schema for discriminatorsImprove error handling for in-evaluable refs for discriminator applicationOct 3, 2024
Copy link
Member

@ViicosViicos left a comment

Choose a reason for hiding this comment

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

I'll try to do a deep dive in the JSON Schema generation process to see what this is about, I'm guessing it is indeed pretty similar to the core schema gen process

sydney-runkle reacted with thumbs up emoji
@sydney-runkle
Copy link
ContributorAuthor

Note from my conversation with@adriangb - we could alternatively do a first pass to collect all defs

@sydney-runklesydney-runkleenabled auto-merge (squash)October 7, 2024 13:26
@sydney-runklesydney-runkle merged commit4e71e66 intomainOct 7, 2024
58 checks passed
@sydney-runklesydney-runkle deleted the disc-fix-alias branchOctober 7, 2024 13:39
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ViicosViicosViicos approved these changes

Assignees
No one assigned
Labels
relnotes-fixUsed for bugfixes.
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

OpenAPI discriminator field disapeared with pydantic v2.5
2 participants
@sydney-runkle@Viicos

[8]ページ先頭

©2009-2025 Movatter.jp