Skip to content

Conversation

@samuelcolvin
Copy link
Member

@samuelcolvin samuelcolvin commented Apr 6, 2024

Change Summary

Trim leading zeros as well as other cleaning while coercing strings to ints.

Prior to #1250 we used str.parse::<i64>() and str.parse::<BigInt>() to coerce strings to integers, in #1250 we adopting the integer parsing logic from jiter (e.g. the logic we use to build integers from JSON strings), in JSON 0001 isn't a valid number, but I was unaware that str.parse::<i64>() did allow it, hence the break.

With this change we stripe leading zeros as part of other cleaning like striping whitespace, stripping .0+, and removing single underscores within the string.

Related issue number

Fix pydantic/pydantic#9165

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

@samuelcolvin samuelcolvin changed the title trim leading zeros before parsing str->int trim leading zeros before parsing str -> int Apr 6, 2024
@codspeed-hq
Copy link

codspeed-hq bot commented Apr 6, 2024

CodSpeed Performance Report

Merging #1266 will not alter performance

Comparing trim-zeros (52c7a2f) with main (e23d7eb)

Summary

✅ 155 untouched benchmarks

@samuelcolvin samuelcolvin enabled auto-merge (squash) April 6, 2024 12:28
@samuelcolvin samuelcolvin merged commit f636403 into main Apr 6, 2024
@samuelcolvin samuelcolvin deleted the trim-zeros branch April 6, 2024 12:28
davidhewitt pushed a commit to pydantic/pydantic that referenced this pull request Oct 20, 2025
davidhewitt pushed a commit to pydantic/pydantic that referenced this pull request Oct 22, 2025
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.

Error when parsing a string as an integer

2 participants