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 Input for str #1229

Merged
merged 2 commits into from
Mar 18, 2024
Merged

implement Input for str #1229

merged 2 commits into from
Mar 18, 2024

Conversation

davidhewitt
Copy link
Contributor

Change Summary

This is on the way to supporting https://github.com/pydantic/jiter/pull/63/files

Because that PR changes the input type for the JSON data to Cow<'_, str>, we run into trouble with the existing implementation of Input for String not being sufficient to cover the Borrowed (i.e. str) variant of the Cow.

After lots of wrestling with options I decided this is the right implementation, as it makes this as reusable as possible and helps to push further with the ideas in #1227 to unpick the two lifetimes.

I want to get this reviewed & merged before the final Cow support PR because it changes a lot about how the lifetimes are written without any of the other logical changes related to the Cow, which will be more interesting to review on their own.

Related issue number

N/A

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

Copy link

codspeed-hq bot commented Mar 18, 2024

CodSpeed Performance Report

Merging #1229 will not alter performance

Comparing dh/input-str (486f964) with main (8db3a6f)

Summary

✅ 148 untouched benchmarks

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

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

overall LGTM, my only querstion is why we're adding Sized to Input then removing it from the impl everywehre?

src/input/return_enums.rs Show resolved Hide resolved
@@ -109,7 +109,7 @@ pub fn str_as_float<'s>(input: &'s impl Input<'s>, str: &str) -> ValResult<Eithe

/// parse a string as an int, `input` is required here to get lifetimes to match up
///
fn _parse_str<'s>(_input: &'s impl Input<'s>, str: &str, len: usize) -> Option<EitherInt<'s>> {
fn _parse_str<'py>(_input: &(impl Input<'py> + ?Sized), str: &str, len: usize) -> Option<EitherInt<'py>> {
Copy link
Member

Choose a reason for hiding this comment

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

There's a lot of &(impl Input<'py> + ?Sized), can you explain better what it does/why we're using it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

impl Trait by default is a type which implements Sized, unfortunately str does not implement Sized

So for &str to be compatible with &impl Trait we need to go for &(impl Trait + ?Sized) to say we're ok with no sized requirement.

So removing Input: Sized from the trait definition is necessary to allow str, and then for impl Trait everywhere we have to add ?Sized opt out.

It's a bit ugly, but I think if we ever go for iterative JSON parsing then the whole Input trait needs to be reworked to carry state somehow, so I sort of felt that we could live with this pain in the meanwhile.

Also where possible I went for impl ToErrorValue. &str implements Sized, as it's just a reference, even if str doesn't. So impl ToErrorValue is much nicer to type compared to &(impl Input<'py> + ?Sized) where relevant. 😂

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense, thanks for the detail.

@davidhewitt davidhewitt merged commit 352d40f into main Mar 18, 2024
28 checks passed
@davidhewitt davidhewitt deleted the dh/input-str branch March 18, 2024 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants