Skip to content

Commit 039a69f

Browse files
Gankrasharkdp
andauthored
[ty] supress inlay hints for +1 and -1 (#21368)
It's everyone's favourite language corner case! Also having kicked the tires on it, I'm pretty happy to call this (in conjunction with #21367): Fixes astral-sh/ty#494 There's cases where you can make noisy Literal hints appear, so we can always iterate on it, but this handles like, 98% of the cases in the wild, which is great. --------- Co-authored-by: David Peter <sharkdp@users.noreply.github.com>
1 parent 3656b44 commit 039a69f

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

crates/ty_ide/src/inlay_hints.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::Db;
44
use ruff_db::files::File;
55
use ruff_db::parsed::parsed_module;
66
use ruff_python_ast::visitor::source_order::{self, SourceOrderVisitor, TraversalSignal};
7-
use ruff_python_ast::{AnyNodeRef, ArgOrKeyword, Expr, Stmt};
7+
use ruff_python_ast::{AnyNodeRef, ArgOrKeyword, Expr, ExprUnaryOp, Stmt, UnaryOp};
88
use ruff_text_size::{Ranged, TextRange, TextSize};
99
use ty_python_semantic::types::Type;
1010
use ty_python_semantic::types::ide_support::inlay_hint_function_argument_details;
@@ -345,6 +345,9 @@ fn type_hint_is_excessive_for_expr(expr: &Expr) -> bool {
345345
// This one expands to `Template` which isn't verbose but is redundant
346346
| Expr::TString(_)=> true,
347347

348+
// You too `+1 and `-1`, get back here
349+
Expr::UnaryOp(ExprUnaryOp { op: UnaryOp::UAdd | UnaryOp::USub, operand, .. }) => matches!(**operand, Expr::NumberLiteral(_)),
350+
348351
// Everything else is reasonable
349352
_ => false,
350353
}
@@ -659,6 +662,8 @@ mod tests {
659662
g = f"{e} {f}"
660663
h = t"wow %d"
661664
i = b'\x00'
665+
j = +1
666+
k = -1.0
662667
"#,
663668
);
664669

@@ -672,6 +677,8 @@ mod tests {
672677
g = f"{e} {f}"
673678
h = t"wow %d"
674679
i = b'\x00'
680+
j = +1
681+
k = -1.0
675682
"#);
676683
}
677684

@@ -688,6 +695,8 @@ mod tests {
688695
g = (f"{ft}", f"{ft}")
689696
h = (t"wow %d", t"wow %d")
690697
i = (b'\x01', b'\x02')
698+
j = (+1, +2.0)
699+
k = (-1, -2.0)
691700
"#,
692701
);
693702

@@ -701,6 +710,8 @@ mod tests {
701710
g = (f"{ft}", f"{ft}")
702711
h = (t"wow %d", t"wow %d")
703712
i = (b'\x01', b'\x02')
713+
j = (+1, +2.0)
714+
k = (-1, -2.0)
704715
"#);
705716
}
706717

@@ -717,6 +728,8 @@ mod tests {
717728
g1, g2 = (f"{ft}", f"{ft}")
718729
h1, h2 = (t"wow %d", t"wow %d")
719730
i1, i2 = (b'\x01', b'\x02')
731+
j1, j2 = (+1, +2.0)
732+
k1, k2 = (-1, -2.0)
720733
"#,
721734
);
722735

@@ -730,6 +743,8 @@ mod tests {
730743
g1, g2 = (f"{ft}", f"{ft}")
731744
h1, h2 = (t"wow %d", t"wow %d")
732745
i1, i2 = (b'\x01', b'\x02')
746+
j1, j2 = (+1, +2.0)
747+
k1, k2 = (-1, -2.0)
733748
"#);
734749
}
735750

@@ -746,6 +761,8 @@ mod tests {
746761
g1, g2 = f"{ft}", f"{ft}"
747762
h1, h2 = t"wow %d", t"wow %d"
748763
i1, i2 = b'\x01', b'\x02'
764+
j1, j2 = +1, +2.0
765+
k1, k2 = -1, -2.0
749766
"#,
750767
);
751768

@@ -759,6 +776,8 @@ mod tests {
759776
g1, g2 = f"{ft}", f"{ft}"
760777
h1, h2 = t"wow %d", t"wow %d"
761778
i1, i2 = b'\x01', b'\x02'
779+
j1, j2 = +1, +2.0
780+
k1, k2 = -1, -2.0
762781
"#);
763782
}
764783

@@ -775,6 +794,8 @@ mod tests {
775794
g = [f"{ft}", f"{ft}"]
776795
h = [t"wow %d", t"wow %d"]
777796
i = [b'\x01', b'\x02']
797+
j = [+1, +2.0]
798+
k = [-1, -2.0]
778799
"#,
779800
);
780801

@@ -788,6 +809,8 @@ mod tests {
788809
g[: list[Unknown | str]] = [f"{ft}", f"{ft}"]
789810
h[: list[Unknown | Template]] = [t"wow %d", t"wow %d"]
790811
i[: list[Unknown | bytes]] = [b'\x01', b'\x02']
812+
j[: list[Unknown | int | float]] = [+1, +2.0]
813+
k[: list[Unknown | int | float]] = [-1, -2.0]
791814
"#);
792815
}
793816

0 commit comments

Comments
 (0)