Skip to content

Incorrect literal math with positive and negative operands #481

@correctmost

Description

@correctmost

Summary

from typing import reveal_type

reveal_type(256 % -129)
reveal_type(-129 % 16)

reveal_type(-10 // 8)
reveal_type(10 // -6)

Actual results:

3 | reveal_type(256 % -129)
  |             ^^^^^^^^^^ `Literal[127]`

4 | reveal_type(-129 % 16)
  |             ^^^^^^^^^ `Literal[-1]`

6 | reveal_type(-10 // 8)
  |             ^^^^^^^^ `Literal[-1]`

7 | reveal_type(10 // -6)
  |             ^^^^^^^^ `Literal[-1]`
  |

Expected results:

3 | reveal_type(256 % -129)
  |             ^^^^^^^^^^ `Literal[-2]`

4 | reveal_type(-129 % 16)
  |             ^^^^^^^^^ `Literal[15]`

6 | reveal_type(-10 // 8)
  |             ^^^^^^^^ `Literal[-2]`

7 | reveal_type(10 // -6)
  |             ^^^^^^^^ `Literal[-2]`
  |

Pyright had similar bugs:

Version

ty 0.0.1-alpha.6

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingruntime semanticsAccurate modeling of how Python's semantics work at runtime

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions