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 unsubstituted type variables with both a default and a bound or constraints#10789

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

Conversation

FyZzyss
Copy link
Contributor

@FyZzyssFyZzyss commentedNov 7, 2024
edited by Viicos
Loading

Change Summary

Add support for Bound and Default together in TypeVar:

frompydanticimportBaseModelclassMyBaseModel(BaseModel):field:strclassExtendedBaseModel(MyBaseModel):another_field:strT=TypeVar('T',bound=MyBaseModel,default=ExtendedBaseModel)

Related issue number

fixes#9418

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

eudu, jonathangreen, bernd-k1337, and mykhakos reacted with heart emoji
@github-actionsgithub-actionsbot added the relnotes-fixUsed for bugfixes. labelNov 7, 2024
@codspeed-hqCodSpeed HQ
Copy link

codspeed-hqbot commentedNov 7, 2024
edited
Loading

CodSpeed Performance Report

Merging#10789 willnot alter performance

ComparingFyZzyss:9418-support-equals-bound-and-default (22a2bd4) withmain (0157e34)

Summary

✅ 44 untouched benchmarks

@github-actionsGitHub Actions
Copy link
Contributor

github-actionsbot commentedNov 7, 2024
edited
Loading

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic/_internal
  _generate_schema.py
Project Total 

This report was generated bypython-coverage-comment-action

@FyZzyssFyZzyss marked this pull request as ready for reviewNovember 7, 2024 17:59
@FyZzyss
Copy link
ContributorAuthor

please review

pydantic-hooky[bot] reacted with thumbs up emoji

@FyZzyssFyZzyssforce-pushed the9418-support-equals-bound-and-default branch from82ccf3b toe519871CompareNovember 7, 2024 18:51
@FyZzyssFyZzyss marked this pull request as draftNovember 7, 2024 19:04
@FyZzyssFyZzyss marked this pull request as ready for reviewNovember 7, 2024 19:16
@FyZzyssFyZzyss marked this pull request as draftNovember 7, 2024 19:41
@FyZzyssFyZzyss changed the titleSupport identical Bound and Default in TypeVarSupport Bound and Default in TypeVarNov 7, 2024
@FyZzyssFyZzyss marked this pull request as ready for reviewNovember 7, 2024 20:03
@FyZzyssFyZzyss changed the titleSupport Bound and Default in TypeVarSupport Bound and Default together in TypeVarNov 7, 2024
@FyZzyssFyZzyssforce-pushed the9418-support-equals-bound-and-default branch 2 times, most recently from7961f35 to183b6faCompareNovember 9, 2024 14:51
@sydney-runklesydney-runkle added relnotes-feature and removed relnotes-fixUsed for bugfixes. labelsNov 11, 2024
@FyZzyssFyZzyssforce-pushed the9418-support-equals-bound-and-default branch 4 times, most recently from70991c2 to620d58aCompareNovember 13, 2024 11:36
@sydney-runkle
Copy link
Contributor

@FyZzyss,

Thanks for your contribution! We'll review this soon - working on pushing v2.10 out now, but I'm anticipating we can get this into v2.11 :)

FyZzyss reacted with thumbs up emoji

@FyZzyssFyZzyssforce-pushed the9418-support-equals-bound-and-default branch 2 times, most recently from9840763 to58bea02CompareNovember 14, 2024 10:30
Copy link
Contributor

@sydney-runklesydney-runkle left a comment

Choose a reason for hiding this comment

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

Left some initial thoughts... I think maybe we could support an identical bound and default. From the original issue, this would require some addition like:

ifboundisnotNoneanddefaultisnotNoneandbound!=default:raiseRuntimeError(...)

Happy to get@Viicos' thoughts here too, he's got some great insight on detailed types like this.

@sydney-runklesydney-runkle added awaiting author revisionawaiting changes from the PR author and removed ready for review labelsNov 14, 2024
@Viicos
Copy link
Member

I think it's safe to support all combinations with bounds, constraints and defaults. In any case, we prioritize the default type overconstraints orbound:

T1=TypeVar('T1',bound=int,default=SubInt)-->generatesaschemafor`SubInt`T2=TypeVar('T2',int,str,default=int)-->generatesaschemafor`int`

Having a different bound and default shouldn't be an issue. The only requirement -- from a static type checking perspective -- is thatdefault should be a subtype ofbound. But such checks are out of scope for Pydantic (see#10462 (comment)).

This should work:

def_unsubstituted_typevar_schema(self,typevar:typing.TypeVar)->core_schema.CoreSchema:try:has_default=typevar.has_default()exceptAttributeError:# Happens if using `typing.TypeVar` on Python < 3.13passelse:ifhas_default:returnself.generate_schema(typevar.__default__)# pyright: ignore[reportAttributeAccessIssue]iftypevar.__constraints__:returnself._union_schema(typing.Union[typevar.__constraints__])iftypevar.__bound__:schema=self.generate_schema(typevar.__bound__)schema['serialization']=core_schema.wrap_serializer_function_ser_schema(lambdax,h:h(x),schema=core_schema.any_schema(),            )returnschemareturncore_schema.any_schema()
FyZzyss reacted with thumbs up emojisydney-runkle reacted with heart emoji

@FyZzyss
Copy link
ContributorAuthor

FyZzyss commentedNov 15, 2024
edited
Loading

Totally agree that it's safe.
I started this PR as supporting equals bound/default, but recognized that it's unnecessary limitation.

This code only works for unsubstituted models, so we simply prioritize the default type (expected behavior for me).
Python Type checker will check ifdefault is not a subtype ofbound inTypeVar.

I will take Viicos suggestion

sydney-runkle reacted with thumbs up emoji

@FyZzyssFyZzyssforce-pushed the9418-support-equals-bound-and-default branch from58bea02 tod03ad87CompareNovember 15, 2024 07:38
@FyZzyssFyZzyss marked this pull request as draftNovember 15, 2024 07:45
@FyZzyssFyZzyss marked this pull request as ready for reviewNovember 15, 2024 07:49
@FyZzyssFyZzyssforce-pushed the9418-support-equals-bound-and-default branch from4d46a2b tocd4db43CompareNovember 15, 2024 08:14
@Viicos
Copy link
Member

Python will check and won't start ifdefault is not a subtype ofbound inTypeVar.

It doesn't, only static type checkers enforce it :)

FyZzyss reacted with thumbs up emoji

@FyZzyssFyZzyss requested a review fromViicosNovember 15, 2024 10:47
@FyZzyssFyZzyssforce-pushed the9418-support-equals-bound-and-default branch from1a4c49e tod6f8780CompareNovember 15, 2024 18:09
@FyZzyssFyZzyss requested a review fromViicosNovember 15, 2024 19:54
Copy link
Contributor

@sydney-runklesydney-runkle left a comment

Choose a reason for hiding this comment

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

This LGTM otherwise, just one minor suggestion :)

@FyZzyssFyZzyssforce-pushed the9418-support-equals-bound-and-default branch fromdb08e3e toa3687bfCompareNovember 16, 2024 21:05
FyZzyssand others added2 commitsNovember 17, 2024 00:07
Co-authored-by: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com>
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.

Thanks!

@ViicosViicos changed the titleSupport Bound and Default together in TypeVarSupport unsubstituted type variables with both a default and a bound or constraintsNov 17, 2024
@ViicosViicos merged commit81b886c intopydantic:mainNov 17, 2024
52 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ViicosViicosViicos approved these changes

@sydney-runklesydney-runkleAwaiting requested review from sydney-runkle

Assignees

@sydney-runklesydney-runkle

Labels
awaiting author revisionawaiting changes from the PR authorrelnotes-feature
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Support TypeVar with Identical Bound and Default
3 participants
@FyZzyss@sydney-runkle@Viicos

[8]ページ先頭

©2009-2025 Movatter.jp