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

AddUNSET sentinel#11883

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

Draft
Viicos wants to merge2 commits intomain
base:main
Choose a base branch
Loading
fromunset-sentinel
Draft

AddUNSET sentinel#11883

Viicos wants to merge2 commits intomainfromunset-sentinel

Conversation

Viicos
Copy link
Member

@ViicosViicos commentedMay 16, 2025
edited
Loading

Change Summary

Requirespydantic/pydantic-core#1711.

Example:

frompydanticimportBaseModelfrompydantic_coreimportUNSETclassA(BaseModel):a:int|UNSET=UNSETa1=A()a1.model_dump()#> {}a2=A(a=1)a2.model_dump()#> {'a': 1}A.model_json_schema()#> {'properties': {'a': {'title': 'A', 'type': 'integer'}}, 'title': 'A', 'type': 'object'}classB(BaseModel):a:UNSET=UNSETB.model_json_schema()#> {'properties': {}, 'title': 'B', 'type': 'object'}

Things to consider:

  • Sentinel name:UNSET seems to be the best fit (andmatchesmsgspec), but will be confusing as we already have a concept of unset fields (tracked in__pydantic_fields_set__). These unset fields can already be excluded by specifyingexclude_unset=True during serialization. If users explicitly setexclude_unset=False but theUNSET sentinel is still excluded, this will be confusing. One alternative could beMISSING: it also makes it a little bit clearer that the expected behavior is to have fields set toMISSING excluded from the output.
  • Sentinel location: currently defined inpydantic-core, but we need to expose it from theexperimental module as we rely on the draftPEP 661.
  • Should we create a new core schema for theUNSET sentinel? I'm currently using a'literal' schema, but not ideal for a couple reasons:
    • The core validator uses equality to validate literal values, but sentinels should be compared using identity.
    • If we happen to implement an optimization where we merge'literal' schemas together (e.g.Literal[1] | UNSET ->Literal[1, UNSET]), we would need to account for this in the JSON Schema generation logic).

@github-actionsgithub-actionsbot added the relnotes-fixUsed for bugfixes. labelMay 16, 2025
@github-actionsGitHub Actions
Copy link
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  fields.py
  json_schema.py786
  pydantic/_internal
  _generate_schema.py1016
Project Total 

This report was generated bypython-coverage-comment-action

@davidhewitt
Copy link
Contributor

  • Should we create a new core schema for theUNSET sentinel?

I could imagine users might want custom sentinels too, should there be a generic "sentinel" schema?

@ViicosViicos added relnotes-feature needs-blogpost-entryThis PR needs to be documented in the release notes blog post and removed relnotes-fixUsed for bugfixes. labelsMay 18, 2025
@cloudflare-workers-and-pagesCloudflare Workers and Pages
Copy link

cloudflare-workers-and-pagesbot commentedJul 1, 2025
edited
Loading

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit:05021b7
Status:🚫  Build failed.

View logs

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
needs-blogpost-entryThis PR needs to be documented in the release notes blog postrelnotes-feature
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@Viicos@davidhewitt

[8]ページ先頭

©2009-2025 Movatter.jp