mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:25:17 +00:00

## Summary fixes #6956 details in issue Following an advice in https://github.com/astral-sh/ruff/issues/6956#issuecomment-1817672585, this change separates expressions to 3 levels of "constant likelihood": * literals, empty dict and tuples... (definitely constant, level 2) * CONSTANT_CASE vars (probably constant, 1) * all other expressions (0) a comparison is marked yoda if the level is strictly higher on its left hand side following https://github.com/astral-sh/ruff/issues/6956#issuecomment-1697107822 marking compound expressions of literals (e.g. `60 * 60` ) as constants this change current behaviour on `SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60)` in the fixture from error to ok
42 lines
895 B
Python
42 lines
895 B
Python
# Errors
|
|
"yoda" == compare # SIM300
|
|
42 == age # SIM300
|
|
("a", "b") == compare # SIM300
|
|
"yoda" <= compare # SIM300
|
|
"yoda" < compare # SIM300
|
|
42 > age # SIM300
|
|
-42 > age # SIM300
|
|
+42 > age # SIM300
|
|
YODA == age # SIM300
|
|
YODA > age # SIM300
|
|
YODA >= age # SIM300
|
|
JediOrder.YODA == age # SIM300
|
|
0 < (number - 100) # SIM300
|
|
B<A[0][0]or B
|
|
B or(B)<A[0][0]
|
|
|
|
# Errors in preview
|
|
['upper'] == UPPER_LIST
|
|
{} == DummyHandler.CONFIG
|
|
|
|
# Errors in stable
|
|
UPPER_LIST == ['upper']
|
|
DummyHandler.CONFIG == {}
|
|
|
|
# OK
|
|
compare == "yoda"
|
|
age == 42
|
|
compare == ("a", "b")
|
|
x == y
|
|
"yoda" == compare == 1
|
|
"yoda" == compare == someothervar
|
|
"yoda" == "yoda"
|
|
age == YODA
|
|
age < YODA
|
|
age <= YODA
|
|
YODA == YODA
|
|
age == JediOrder.YODA
|
|
(number - 100) > 0
|
|
SECONDS_IN_DAY == 60 * 60 * 24 # Error in 0.1.8
|
|
SomeClass().settings.SOME_CONSTANT_VALUE > (60 * 60) # Error in 0.1.8
|
|
{"non-empty-dict": "is-ok"} == DummyHandler.CONFIG
|