Skip to content

[ty] Implement Python's floor division semantics for Literal ints#18249

Merged
carljm merged 3 commits intoastral-sh:mainfrom
brandtbucher:floor-division
May 22, 2025
Merged

[ty] Implement Python's floor division semantics for Literal ints#18249
carljm merged 3 commits intoastral-sh:mainfrom
brandtbucher:floor-division

Conversation

@brandtbucher
Copy link
Contributor

Division works differently in Python than in Rust. If the result is negative and there is a remainder, the division rounds down (instead of towards zero). The remainder needs to be adjusted to compensate so that (lhs // rhs) * rhs + (lhs % rhs) == lhs.

Fixes astral-sh/ty#481. @AlexWaygood.

@brandtbucher brandtbucher changed the title [ty] Implement Python's floor division semantics for Literal ints [ty] Implement Python's floor division semantics for Literal ints May 22, 2025
@github-actions
Copy link
Contributor

mypy_primer results

No ecosystem changes detected ✅

@AlexWaygood AlexWaygood added bug Something isn't working ty Multi-file analysis & type inference labels May 22, 2025
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Looks good! Verified the asserted results in tests match actual runtime results, and the code looks right. Thank you!

@carljm carljm merged commit 91b7a57 into astral-sh:main May 22, 2025
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect literal math with positive and negative operands

3 participants