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

Consistent validation with complex number to float type coercion#1574

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
ambroseling wants to merge2 commits intopydantic:main
base:main
Choose a base branch
Loading
fromambroseling:main

Conversation

ambroseling
Copy link

@ambroselingambroseling commentedDec 8, 2024
edited by pydantic-hookybot
Loading

Change Summary

When validating floats in non-strict mode, this PR adds support for properly handling complex to float coercion by
by extracting their real component, this behavior is prohibited in strict mode and returns ValidationError. Appropriate unit tests are added totest_float andtest_float_strict .

Related issue number

Fixespydantic/pydantic#10430

Checklist

  • Unit tests for the changes exist
  • Documentation reflects the changes where applicable
  • Pydantic tests pass with thispydantic-core (except for expected changes)
  • My PR is ready to review,please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer:@davidhewitt

@codecovCodecov
Copy link

codecovbot commentedDec 8, 2024
edited
Loading

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.63%. Comparing base(ab503cb) to head(8e5b5ff).
Report is 251 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@##             main    #1574      +/-   ##==========================================- Coverage   90.21%   89.63%   -0.59%==========================================  Files         106      112       +6       Lines       16339    17898    +1559       Branches       36       40       +4     ==========================================+ Hits        14740    16042    +1302- Misses       1592     1836     +244- Partials        7       20      +13
Files with missing linesCoverage Δ
src/input/input_python.rs98.08% <100.00%> (+0.87%)⬆️

... and55 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend -Click here to learn more
Δ = absolute <relative> (impact),ø = not affected,? = missing data
Powered byCodecov. Last update4c02cbd...8e5b5ff. Read thecomment docs.

@codspeed-hqCodSpeed HQ
Copy link

codspeed-hqbot commentedDec 8, 2024
edited
Loading

CodSpeed Performance Report

Merging#1574 willnot alter performance

Comparingambroseling:main (8e5b5ff) withmain (4c02cbd)

Summary

✅ 155 untouched benchmarks

@ambroselingambroseling marked this pull request as ready for reviewDecember 8, 2024 18:54
@ambroseling
Copy link
Author

Hi@davidhewitt@sydney-runkle

The CI is failing due to a pyright version mismatch (trying to install v1.1.389). I've verified that this version exists in the npm registry but for some reason CI was unable to fetch it. This seems to be an infrastructure issue rather than a problem with my changes.

Would it be possible to get some guidance on this or have the CI re-run? And it would be great if my changes can be reviewed.

Thank you for your time!

if strict {
return Err(ValError::new(ErrorTypeDefaults::FloatType, self));
}
let real = self.getattr(intern!(self.py(), "real")).unwrap();
Copy link
Contributor

@changhcchanghcDec 9, 2024
edited
Loading

Choose a reason for hiding this comment

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

Perhaps we should raise a warning here. In numpy's case, they do raise a customComplexWarning for discarding the imaginary part. At least we should document whatever coercion is implemented here because there can be a few possibilities for complex -> float:

  • no coercion is allowed: consistent with the default python behaviour, e.g.float(complex(1, 2)) gives an error, even when the imaginary part is 0
  • coercion is allowed only when the imaginary part is 0: this is what I would actually expect when coercions can actually take place
  • coercion is allowed for all complex numbers, done by simply dropping the imaginary part: this seems to be a numpy-only behaviour and I'm not sure if non-numpy users would expect this

...if we really want to implement this coercion. Please take a look at my comment.pydantic/pydantic#10430 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

I am somewhat open to lax mode allowingcomplex ->floatif the imaginary component is zero, similarly we allowdatetime ->dateif the datetime is at midnight.

I think we should never drop a nonzero imaginary part.

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

@davidhewittdavidhewittdavidhewitt left review comments

@changhcchanghcchanghc left review comments

Assignees

@davidhewittdavidhewitt

Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Inconsistent validation with complex number
3 participants
@ambroseling@davidhewitt@changhc

[8]ページ先頭

©2009-2025 Movatter.jp