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

Support compare_as callable at field#10834

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

Open
andresliszt wants to merge1 commit intopydantic:main
base:main
Choose a base branch
Loading
fromandresliszt:add/compare-as

Conversation

andresliszt
Copy link
Contributor

@andreslisztandresliszt commentedNov 13, 2024
edited by pydantic-hookybot
Loading

Change Summary

This PR addscompare_as callable in theField class to add custom comparison. The classical example in the numpy ndarray, see#7307. Note thatattrs defines a similareq callable (see thedocs)

importnumpy@defineclassC:an_array=field(eq=attrs.cmp_using(eq=numpy.array_equal))

and the pydantic implementation looks like

importnumpyfrompydanticimportBaseModel,ConfigDict,FieldclassC(BaseModel):an_array:np.ndarray=Field(compare_as=numpy.array_equal,default=np.array([1,2,3]))model_config=ConfigDict(allow_arbitrary_types=True)

Note that I intentionally added adefault = np.array([1,2,3]) to illustrate the example whereexclude_default = True in the serialization, this callable can be forwarded to the Rust side. See thisdiscussion

Related issue number

#7307
#pydantic/pydantic-core#1114

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review,please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer:@sydney-runkle

@github-actionsgithub-actionsbot added the relnotes-fixUsed for bugfixes. labelNov 13, 2024
@codspeed-hqCodSpeed HQ
Copy link

codspeed-hqbot commentedNov 13, 2024
edited
Loading

CodSpeed Performance Report

Merging#10834 willnot alter performance

Comparingandresliszt:add/compare-as (35f2f43) withmain (9783bc8)

Summary

✅ 43 untouched benchmarks

@github-actionsGitHub Actions
Copy link
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  fields.py
  main.py
Project Total 

This report was generated bypython-coverage-comment-action

@andresliszt
Copy link
ContributorAuthor

please review

pydantic-hooky[bot] reacted with thumbs up emoji

@sydney-runkle
Copy link
Contributor

@andresliszt,

Thanks for the PR. Looks like there are a couple of solutions available on this front, including this one, plus:

Sorry for our review delays here. I've pinged@davidhewitt to see what he thinks regarding the best way forward here. We're eager to get a fix for this / related issues into v2.11!

@andresliszt
Copy link
ContributorAuthor

@andresliszt,

Thanks for the PR. Looks like there are a couple of solutions available on this front, including this one, plus:

Sorry for our review delays here. I've pinged@davidhewitt to see what he thinks regarding the best way forward here. We're eager to get a fix for this / related issues into v2.11!

Nice! Yeah I opennedadd default comparison, this is a generalization. In that PR the callable was used only to compare default values, which is done in Rust. Thecompare_as callable is used in the pydanticBaseModel.__eq__ and also can be forwarded to the Rust side to exclude default values.

@sydney-runkle
Copy link
Contributor

Cool. I think we want to move forward with an approach similar to this one. We're pushing outv2.10 mid week this week, but would love to help get this into v2.11, so will be more active in helping get this across the line after the release :)

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees

@sydney-runklesydney-runkle

Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@andresliszt@sydney-runkle@Viicos

[8]ページ先頭

©2009-2025 Movatter.jp