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

Add python validators for decimal constraints (max_digits anddecimal_places)#10506

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 13 commits intomainfromadd-decimal-validators
Sep 27, 2024

Conversation

sydney-runkle
Copy link
Contributor

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

Closes#10498

This specifically addresses issues where there is annotated metadata applied between the type and the decimal constraints.

For this sample:

importdecimalfrompydanticimportBaseModel,Field,BeforeValidator,ValidationErrorfromtypingimportAnnotatedDecimalAnnotation=Annotated[decimal.Decimal,BeforeValidator(lambdav:v)]classExampleSchema(BaseModel):example_value:Annotated[DecimalAnnotation,Field(max_digits=10,decimal_places=4)]|Nonee=ExampleSchema(example_value=decimal.Decimal('123.4567'))try:ExampleSchema(example_value=decimal.Decimal('123.45678'))exceptValidationErrorase:print(e)"""    example_value    Decimal input should have no more than 4 decimal places [type=decimal_max_places, input_value=Decimal('123.45678'), input_type=Decimal]        For further information visit https://errors.pydantic.dev/2.10/v/decimal_max_places    """try:ExampleSchema(example_value=decimal.Decimal('12345678.901'))exceptValidationErrorase:print(e)"""    example_value    Decimal input should have no more than 10 digits in total [type=decimal_max_digits, input_value=Decimal('12345678.901'), input_type=Decimal]        For further information visit https://errors.pydantic.dev/2.10/v/decimal_max_digits    """

The output used to be:

File"/Users/programming/pydantic_work/pydantic/pydantic/_internal/_known_annotated_metadata.py",line293,inapply_known_metadataraiseRuntimeError(f"Unable to apply constraint '{constraint}' to schema of type '{schema_type}'")RuntimeError:Unabletoapplyconstraint'max_digits'toschemaoftype'function-before'

@github-actionsgithub-actionsbot added the relnotes-fixUsed for bugfixes. labelSep 27, 2024
@cloudflare-workers-and-pagesCloudflare Workers and Pages
Copy link

cloudflare-workers-and-pagesbot commentedSep 27, 2024
edited
Loading

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit:9adef0c
Status: ✅  Deploy successful!
Preview URL:https://99e28bdc.pydantic-docs.pages.dev
Branch Preview URL:https://add-decimal-validators.pydantic-docs.pages.dev

View logs

@sydney-runklesydney-runkle added relnotes-feature and removed relnotes-fixUsed for bugfixes. labelsSep 27, 2024
@codspeed-hqCodSpeed HQ
Copy link

codspeed-hqbot commentedSep 27, 2024
edited
Loading

CodSpeed Performance Report

Merging#10506 willnot alter performance

Comparingadd-decimal-validators (9adef0c) withmain (1f74db2)

Summary

✅ 38 untouched benchmarks

@github-actionsGitHub Actions
Copy link
Contributor

github-actionsbot commentedSep 27, 2024
edited
Loading

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic/_internal
  _known_annotated_metadata.py298-300
  _validators.py351,385,400
Project Total 

This report was generated bypython-coverage-comment-action

@sydney-runklesydney-runkle added relnotes-fixUsed for bugfixes. and removed relnotes-feature labelsSep 27, 2024
@sydney-runkle
Copy link
ContributorAuthor

I think it's alright if I merge this (given the approval and iterations on feedback), but if there's anything left unaddressed, happy to open another PR on Monday!

@sydney-runklesydney-runkle merged commit01b5929 intomainSep 27, 2024
62 checks passed
@sydney-runklesydney-runkle deleted the add-decimal-validators branchSeptember 27, 2024 21:10
@Viicos
Copy link
Member

I'm wondering: why is the Python validation only called when a validator is already present? Can we avoid duplication between pydantic-core and here?

Other thing, seems likeallow_inf_nan suffers from the same issue.

@sydney-runkle
Copy link
ContributorAuthor

I'm wondering: why is the Python validation only called when a validator is already present? Can we avoid duplication between pydantic-core and here?

Good question. I think this issue helps to explain things:#10036

Specifically, we use these Python validators for cases where it'd violate our promise to maintain the order of annotations if we directly applied the constraint to the core schema. But, the validator is still compatible with the type after the custom validator is called, so we can use these python wrappers.

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

@alexmojakialexmojakialexmojaki left review comments

@adriangbadriangbadriangb 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.

Optional[Decimal] with field constraints error: Unable to apply constraint 'max_digits' to schema of type 'function-after'
4 participants
@sydney-runkle@Viicos@adriangb@alexmojaki

[8]ページ先頭

©2009-2025 Movatter.jp