Skip to content

Commit

Permalink
[ruff] Detect more strict-integer expressions (RUF046)
Browse files Browse the repository at this point in the history
  • Loading branch information
InSyncWithFoo committed Dec 8, 2024
1 parent b9d18a5 commit 82ebdef
Show file tree
Hide file tree
Showing 3 changed files with 878 additions and 425 deletions.
110 changes: 91 additions & 19 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF046.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

### Safely fixable

# Arguments are not checked
int(id())
int(len([]))
int(ord(foo))
Expand All @@ -17,6 +16,30 @@
int(math.isqrt())
int(math.perm())

int(1)
int(v := 1)
int(~1)
int(-1)
int(+1)

int(1 + 1)
int(1 - 1)
int(1 * 1)
int(1 % 1)
int(1 ** 1)
int(1 << 1)
int(1 >> 1)
int(1 | 1)
int(1 ^ 1)
int(1 & 1)
int(1 // 1)

int(1 if ... else 2)

int(round(1))
int(round(1, None))
int(round(1, 0))


### Unsafe

Expand All @@ -25,26 +48,75 @@
int(math.trunc())


### `round()`
### No errors

int(1 and 0)
int(0 or -1)

int(foo if ... else 4)

int(round())
int(round(ndigits=2))
int(round(3.4))
int(round(3.4, 0))
int(round(3.4, 2))
int(round(5, foo))

int(3.14)
int(2.8j)

async def f():
int(await f())

int(foo.bar)
int(bar([1][False]))

int(1 == 1)
int(1 != 1)
int(1 < 1)
int(1 <= 1)
int(1 > 1)
int(1 >= 1)
int(1 in 1)
int(1 not in 1)
int(1 is 1)
int(2 is not 3)
int(foo in 1)
int(foo not in 1)
int(foo is 1)
int(foo is not 1)

int(1 == 2 == 3)
int(foo == 1)
int(foo != 1)
int(foo < 1)
int(foo <= 1)
int(foo > 1)
int(foo >= 1)

int(v := {}[{}['']])

int(foo + 1)
int(foo - 1)
int(foo * 1)
int(foo @ 1)
int(foo / 1)
int(foo % 1)
int(foo ** 1)
int(foo << 1)
int(foo >> 1)
int(foo | 1)
int(foo ^ 1)
int(foo & 1)
int(foo // 1)

## Errors
int(round(0))
int(round(0, 0))
int(round(0, None))
int(v := 3.7)

int(round(0.1))
int(round(0.1, None))
int(not 109)

# Argument type is not checked
foo = type("Foo", (), {"__round__": lambda self: 4.2})()
int(1 / 1)
int(1 @ 1)

int(round(foo))
int(round(foo, 0))
int(round(foo, None))
int(1. if ... else .2)

## No errors
int(round(0, 3.14))
int(round(0, non_literal))
int(round(0, 0), base)
int(round(0, 0, extra=keyword))
int(round(0.1, 0))
int(round(5, 1))
Loading

0 comments on commit 82ebdef

Please sign in to comment.