Skip to content

Commit 34e06f2

Browse files
[red-knot] Do not show types for literal expressions on hover (#17290)
## Summary Resolves #17289. After this change, Red Knot will no longer show types on hover for `None`, `...`, `True`, `False`, numbers, strings (but not f-strings), and bytes literals. ## Test Plan Unit tests.
1 parent a388c73 commit 34e06f2

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

crates/red_knot_ide/src/hover.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::goto::find_goto_target;
1+
use crate::goto::{find_goto_target, GotoTarget};
22
use crate::{Db, MarkupKind, RangedValue};
33
use red_knot_python_semantic::types::Type;
44
use red_knot_python_semantic::SemanticModel;
@@ -12,6 +12,12 @@ pub fn hover(db: &dyn Db, file: File, offset: TextSize) -> Option<RangedValue<Ho
1212
let parsed = parsed_module(db.upcast(), file);
1313
let goto_target = find_goto_target(parsed, offset)?;
1414

15+
if let GotoTarget::Expression(expr) = goto_target {
16+
if expr.is_literal_expr() {
17+
return None;
18+
}
19+
}
20+
1521
let model = SemanticModel::new(db.upcast(), file);
1622
let ty = goto_target.inferred_type(&model)?;
1723

@@ -496,6 +502,57 @@ mod tests {
496502
");
497503
}
498504

505+
#[test]
506+
fn hover_whitespace() {
507+
let test = cursor_test(
508+
r#"
509+
class C:
510+
<CURSOR>
511+
foo: str = 'bar'
512+
"#,
513+
);
514+
515+
assert_snapshot!(test.hover(), @"Hover provided no content");
516+
}
517+
518+
#[test]
519+
fn hover_literal_int() {
520+
let test = cursor_test(
521+
r#"
522+
print(
523+
0 + 1<CURSOR>
524+
)
525+
"#,
526+
);
527+
528+
assert_snapshot!(test.hover(), @"Hover provided no content");
529+
}
530+
531+
#[test]
532+
fn hover_literal_ellipsis() {
533+
let test = cursor_test(
534+
r#"
535+
print(
536+
.<CURSOR>..
537+
)
538+
"#,
539+
);
540+
541+
assert_snapshot!(test.hover(), @"Hover provided no content");
542+
}
543+
544+
#[test]
545+
fn hover_docstring() {
546+
let test = cursor_test(
547+
r#"
548+
def f():
549+
"""Lorem ipsum dolor sit amet.<CURSOR>"""
550+
"#,
551+
);
552+
553+
assert_snapshot!(test.hover(), @"Hover provided no content");
554+
}
555+
499556
impl CursorTest {
500557
fn hover(&self) -> String {
501558
use std::fmt::Write;

crates/ruff_python_ast/src/nodes.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ impl Expr {
102102
}
103103

104104
impl ExprRef<'_> {
105+
/// See [`Expr::is_literal_expr`].
106+
pub fn is_literal_expr(&self) -> bool {
107+
matches!(
108+
self,
109+
ExprRef::StringLiteral(_)
110+
| ExprRef::BytesLiteral(_)
111+
| ExprRef::NumberLiteral(_)
112+
| ExprRef::BooleanLiteral(_)
113+
| ExprRef::NoneLiteral(_)
114+
| ExprRef::EllipsisLiteral(_)
115+
)
116+
}
117+
105118
pub fn precedence(&self) -> OperatorPrecedence {
106119
OperatorPrecedence::from(self)
107120
}

0 commit comments

Comments
 (0)