Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Implement optional number to str coercion #975

Merged
merged 7 commits into from
Sep 20, 2023

Conversation

lig
Copy link
Contributor

@lig lig commented Sep 19, 2023

Change Summary

Implement optional number to str coercion

Related issue number

See pydantic/pydantic#6045

Checklist

  • Unit tests for the changes exist
  • Documentation reflects the changes where applicable
  • Pydantic tests pass with this pydantic-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

@codecov
Copy link

codecov bot commented Sep 19, 2023

Codecov Report

Merging #975 (4f457ff) into main (367a67a) will decrease coverage by 0.03%.
Report is 1 commits behind head on main.
The diff coverage is 93.10%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #975      +/-   ##
==========================================
- Coverage   93.14%   93.11%   -0.03%     
==========================================
  Files         106      106              
  Lines       15763    15791      +28     
  Branches       25       25              
==========================================
+ Hits        14682    14704      +22     
- Misses       1075     1081       +6     
  Partials        6        6              
Files Changed Coverage Δ
src/input/input_json.rs 89.21% <50.00%> (-0.56%) ⬇️
python/pydantic_core/core_schema.py 96.78% <100.00%> (+<0.01%) ⬆️
src/input/input_abstract.rs 86.76% <100.00%> (ø)
src/input/input_python.rs 98.36% <100.00%> (+0.03%) ⬆️
src/input/return_enums.rs 85.38% <100.00%> (+0.07%) ⬆️
src/validators/string.rs 100.00% <100.00%> (ø)
src/validators/url.rs 93.19% <100.00%> (ø)

... and 5 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 by Codecov. Last update 367a67a...4f457ff. Read the comment docs.

@codspeed-hq
Copy link

codspeed-hq bot commented Sep 19, 2023

CodSpeed Performance Report

Merging #975 will not alter performance

Comparing lig/6045-v2-validating-int-str (a73ab44) with main (367a67a)

Summary

✅ 138 untouched benchmarks

src/input/input_python.rs Outdated Show resolved Hide resolved
src/input/input_json.rs Outdated Show resolved Hide resolved
// final output needs to be Python anyway.
Ok(s) => PyString::new(self.py(), s),
Err(_) => return Err(ValError::new(ErrorTypeDefaults::StringUnicode, self)),
};
Ok(s.into())
} else if coerce_numbers_to_str {
if let Ok(py_decimal) = self.lax_decimal(py) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The downside of going through lax_decimal will be performance - you're creating a whole new Python decimal on the way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it is. We knew it will be less performant feature anyway.

It hits this only if it's not a string which is already the "bad" case and the alternative is hitting several TypeError exceptions. The alternative is to almost copy the lax_decimal code.

I'll give it another try.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implemented using just isinstance calls.

@lig lig force-pushed the lig/6045-v2-validating-int-str branch from 9ec8778 to 3156a08 Compare September 19, 2023 19:57
@lig
Copy link
Contributor Author

lig commented Sep 19, 2023

please review

Copy link
Contributor

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Overall looks good to me! Just some small nits...

python/pydantic_core/core_schema.py Outdated Show resolved Hide resolved
src/input/input_python.rs Outdated Show resolved Hide resolved
src/input/input_python.rs Outdated Show resolved Hide resolved
src/validators/string.rs Outdated Show resolved Hide resolved
tests/validators/test_string.py Outdated Show resolved Hide resolved
@davidhewitt
Copy link
Contributor

please update

@lig
Copy link
Contributor Author

lig commented Sep 20, 2023

please review

src/input/input_python.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

@lig lig merged commit 157a643 into main Sep 20, 2023
30 checks passed
@lig lig deleted the lig/6045-v2-validating-int-str branch September 20, 2023 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants