Skip to content

Commit fc2ebea

Browse files
authored
[red-knot] Make infer.rs unit tests independent of public symbol inference (#15690)
## Summary Make the remaining `infer.rs` unit tests independent from public symbol type inference decisions (see upcoming change in #15674). ## Test Plan - Made sure that the unit tests actually fail if one of the `assert_type` assertions is changed.
1 parent 43160b4 commit fc2ebea

File tree

1 file changed

+79
-89
lines changed
  • crates/red_knot_python_semantic/src/types

1 file changed

+79
-89
lines changed

crates/red_knot_python_semantic/src/types/infer.rs

Lines changed: 79 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -6007,24 +6007,13 @@ mod tests {
60076007
use crate::semantic_index::definition::Definition;
60086008
use crate::semantic_index::symbol::FileScopeId;
60096009
use crate::semantic_index::{global_scope, semantic_index, symbol_table, use_def_map};
6010+
use crate::types::check_types;
60106011
use ruff_db::files::{system_path_to_file, File};
60116012
use ruff_db::system::DbWithTestSystem;
60126013
use ruff_db::testing::assert_function_query_was_not_run;
60136014

60146015
use super::*;
60156016

6016-
#[track_caller]
6017-
fn assert_public_type(db: &TestDb, file_name: &str, symbol_name: &str, expected: &str) {
6018-
let file = system_path_to_file(db, file_name).expect("file to exist");
6019-
6020-
let ty = global_symbol(db, file, symbol_name).expect_type();
6021-
assert_eq!(
6022-
ty.display(db).to_string(),
6023-
expected,
6024-
"Mismatch for symbol '{symbol_name}' in '{file_name}'"
6025-
);
6026-
}
6027-
60286017
#[track_caller]
60296018
fn get_symbol<'db>(
60306019
db: &'db TestDb,
@@ -6049,64 +6038,66 @@ mod tests {
60496038
symbol(db, scope, symbol_name)
60506039
}
60516040

6041+
#[track_caller]
6042+
fn assert_diagnostic_messages(diagnostics: &TypeCheckDiagnostics, expected: &[&str]) {
6043+
let messages: Vec<&str> = diagnostics
6044+
.iter()
6045+
.map(|diagnostic| diagnostic.message())
6046+
.collect();
6047+
assert_eq!(&messages, expected);
6048+
}
6049+
6050+
#[track_caller]
6051+
fn assert_file_diagnostics(db: &TestDb, filename: &str, expected: &[&str]) {
6052+
let file = system_path_to_file(db, filename).unwrap();
6053+
let diagnostics = check_types(db, file);
6054+
6055+
assert_diagnostic_messages(diagnostics, expected);
6056+
}
6057+
60526058
#[test]
60536059
fn not_literal_string() -> anyhow::Result<()> {
60546060
let mut db = setup_db();
60556061
let content = format!(
60566062
r#"
6057-
v = not "{y}"
6058-
w = not 10*"{y}"
6059-
x = not "{y}"*10
6060-
z = not 0*"{y}"
6061-
u = not (-100)*"{y}"
6062-
"#,
6063+
from typing_extensions import assert_type
6064+
6065+
assert_type(not "{y}", bool)
6066+
assert_type(not 10*"{y}", bool)
6067+
assert_type(not "{y}"*10, bool)
6068+
assert_type(not 0*"{y}", Literal[True])
6069+
assert_type(not (-100)*"{y}", Literal[True])
6070+
"#,
60636071
y = "a".repeat(TypeInferenceBuilder::MAX_STRING_LITERAL_SIZE + 1),
60646072
);
60656073
db.write_dedented("src/a.py", &content)?;
60666074

6067-
assert_public_type(&db, "src/a.py", "v", "bool");
6068-
assert_public_type(&db, "src/a.py", "w", "bool");
6069-
assert_public_type(&db, "src/a.py", "x", "bool");
6070-
assert_public_type(&db, "src/a.py", "z", "Literal[True]");
6071-
assert_public_type(&db, "src/a.py", "u", "Literal[True]");
6075+
assert_file_diagnostics(&db, "src/a.py", &[]);
60726076

60736077
Ok(())
60746078
}
60756079

60766080
#[test]
60776081
fn multiplied_string() -> anyhow::Result<()> {
60786082
let mut db = setup_db();
6079-
6080-
db.write_dedented(
6081-
"src/a.py",
6082-
&format!(
6083-
r#"
6084-
w = 2 * "hello"
6085-
x = "goodbye" * 3
6086-
y = "a" * {y}
6087-
z = {z} * "b"
6088-
a = 0 * "hello"
6089-
b = -3 * "hello"
6083+
let content = format!(
6084+
r#"
6085+
from typing_extensions import assert_type
6086+
6087+
assert_type(2 * "hello", Literal["hellohello"])
6088+
assert_type("goodbye" * 3, Literal["goodbyegoodbyegoodbye"])
6089+
assert_type("a" * {y}, Literal["{a_repeated}"])
6090+
assert_type({z} * "b", LiteralString)
6091+
assert_type(0 * "hello", Literal[""])
6092+
assert_type(-3 * "hello", Literal[""])
60906093
"#,
6091-
y = TypeInferenceBuilder::MAX_STRING_LITERAL_SIZE,
6092-
z = TypeInferenceBuilder::MAX_STRING_LITERAL_SIZE + 1
6093-
),
6094-
)?;
6095-
6096-
assert_public_type(&db, "src/a.py", "w", r#"Literal["hellohello"]"#);
6097-
assert_public_type(&db, "src/a.py", "x", r#"Literal["goodbyegoodbyegoodbye"]"#);
6098-
assert_public_type(
6099-
&db,
6100-
"src/a.py",
6101-
"y",
6102-
&format!(
6103-
r#"Literal["{}"]"#,
6104-
"a".repeat(TypeInferenceBuilder::MAX_STRING_LITERAL_SIZE)
6105-
),
6094+
y = TypeInferenceBuilder::MAX_STRING_LITERAL_SIZE,
6095+
z = TypeInferenceBuilder::MAX_STRING_LITERAL_SIZE + 1,
6096+
a_repeated = "a".repeat(TypeInferenceBuilder::MAX_STRING_LITERAL_SIZE),
61066097
);
6107-
assert_public_type(&db, "src/a.py", "z", "LiteralString");
6108-
assert_public_type(&db, "src/a.py", "a", r#"Literal[""]"#);
6109-
assert_public_type(&db, "src/a.py", "b", r#"Literal[""]"#);
6098+
db.write_dedented("src/a.py", &content)?;
6099+
6100+
assert_file_diagnostics(&db, "src/a.py", &[]);
61106101

61116102
Ok(())
61126103
}
@@ -6116,21 +6107,20 @@ mod tests {
61166107
let mut db = setup_db();
61176108
let content = format!(
61186109
r#"
6119-
v = "{y}"
6120-
w = 10*"{y}"
6121-
x = "{y}"*10
6122-
z = 0*"{y}"
6123-
u = (-100)*"{y}"
6124-
"#,
6110+
from typing_extensions import assert_type
6111+
6112+
assert_type("{y}", LiteralString)
6113+
assert_type(10*"{y}", LiteralString)
6114+
assert_type("{y}"*10, LiteralString)
6115+
assert_type(0*"{y}", Literal[""])
6116+
assert_type((-100)*"{y}", Literal[""])
6117+
"#,
61256118
y = "a".repeat(TypeInferenceBuilder::MAX_STRING_LITERAL_SIZE + 1),
61266119
);
61276120
db.write_dedented("src/a.py", &content)?;
61286121

6129-
assert_public_type(&db, "src/a.py", "v", "LiteralString");
6130-
assert_public_type(&db, "src/a.py", "w", "LiteralString");
6131-
assert_public_type(&db, "src/a.py", "x", "LiteralString");
6132-
assert_public_type(&db, "src/a.py", "z", r#"Literal[""]"#);
6133-
assert_public_type(&db, "src/a.py", "u", r#"Literal[""]"#);
6122+
assert_file_diagnostics(&db, "src/a.py", &[]);
6123+
61346124
Ok(())
61356125
}
61366126

@@ -6139,16 +6129,17 @@ mod tests {
61396129
let mut db = setup_db();
61406130
let content = format!(
61416131
r#"
6142-
w = "{y}"
6143-
x = "a" + "{z}"
6144-
"#,
6132+
from typing_extensions import assert_type
6133+
6134+
assert_type("{y}", LiteralString)
6135+
assert_type("a" + "{z}", LiteralString)
6136+
"#,
61456137
y = "a".repeat(TypeInferenceBuilder::MAX_STRING_LITERAL_SIZE + 1),
61466138
z = "a".repeat(TypeInferenceBuilder::MAX_STRING_LITERAL_SIZE),
61476139
);
61486140
db.write_dedented("src/a.py", &content)?;
61496141

6150-
assert_public_type(&db, "src/a.py", "w", "LiteralString");
6151-
assert_public_type(&db, "src/a.py", "x", "LiteralString");
6142+
assert_file_diagnostics(&db, "src/a.py", &[]);
61526143

61536144
Ok(())
61546145
}
@@ -6158,19 +6149,18 @@ mod tests {
61586149
let mut db = setup_db();
61596150
let content = format!(
61606151
r#"
6161-
v = "{y}"
6162-
w = "{y}" + "a"
6163-
x = "a" + "{y}"
6164-
z = "{y}" + "{y}"
6165-
"#,
6152+
from typing_extensions import assert_type
6153+
6154+
assert_type("{y}", LiteralString)
6155+
assert_type("{y}" + "a", LiteralString)
6156+
assert_type("a" + "{y}", LiteralString)
6157+
assert_type("{y}" + "{y}", LiteralString)
6158+
"#,
61666159
y = "a".repeat(TypeInferenceBuilder::MAX_STRING_LITERAL_SIZE + 1),
61676160
);
61686161
db.write_dedented("src/a.py", &content)?;
61696162

6170-
assert_public_type(&db, "src/a.py", "v", "LiteralString");
6171-
assert_public_type(&db, "src/a.py", "w", "LiteralString");
6172-
assert_public_type(&db, "src/a.py", "x", "LiteralString");
6173-
assert_public_type(&db, "src/a.py", "z", "LiteralString");
6163+
assert_file_diagnostics(&db, "src/a.py", &[]);
61746164

61756165
Ok(())
61766166
}
@@ -6257,22 +6247,22 @@ mod tests {
62576247

62586248
db.write_files([
62596249
("/src/a.py", "from foo import x"),
6260-
("/src/foo.py", "x = 10\ndef foo(): ..."),
6250+
("/src/foo.py", "x: int = 10\ndef foo(): ..."),
62616251
])?;
62626252

62636253
let a = system_path_to_file(&db, "/src/a.py").unwrap();
62646254
let x_ty = global_symbol(&db, a, "x").expect_type();
62656255

6266-
assert_eq!(x_ty.display(&db).to_string(), "Literal[10]");
6256+
assert_eq!(x_ty.display(&db).to_string(), "int");
62676257

62686258
// Change `x` to a different value
6269-
db.write_file("/src/foo.py", "x = 20\ndef foo(): ...")?;
6259+
db.write_file("/src/foo.py", "x: bool = True\ndef foo(): ...")?;
62706260

62716261
let a = system_path_to_file(&db, "/src/a.py").unwrap();
62726262

62736263
let x_ty_2 = global_symbol(&db, a, "x").expect_type();
62746264

6275-
assert_eq!(x_ty_2.display(&db).to_string(), "Literal[20]");
6265+
assert_eq!(x_ty_2.display(&db).to_string(), "bool");
62766266

62776267
Ok(())
62786268
}
@@ -6283,23 +6273,23 @@ mod tests {
62836273

62846274
db.write_files([
62856275
("/src/a.py", "from foo import x"),
6286-
("/src/foo.py", "x = 10\ndef foo(): y = 1"),
6276+
("/src/foo.py", "x: int = 10\ndef foo(): y = 1"),
62876277
])?;
62886278

62896279
let a = system_path_to_file(&db, "/src/a.py").unwrap();
62906280
let x_ty = global_symbol(&db, a, "x").expect_type();
62916281

6292-
assert_eq!(x_ty.display(&db).to_string(), "Literal[10]");
6282+
assert_eq!(x_ty.display(&db).to_string(), "int");
62936283

6294-
db.write_file("/src/foo.py", "x = 10\ndef foo(): pass")?;
6284+
db.write_file("/src/foo.py", "x: int = 10\ndef foo(): pass")?;
62956285

62966286
let a = system_path_to_file(&db, "/src/a.py").unwrap();
62976287

62986288
db.clear_salsa_events();
62996289

63006290
let x_ty_2 = global_symbol(&db, a, "x").expect_type();
63016291

6302-
assert_eq!(x_ty_2.display(&db).to_string(), "Literal[10]");
6292+
assert_eq!(x_ty_2.display(&db).to_string(), "int");
63036293

63046294
let events = db.take_salsa_events();
63056295

@@ -6319,23 +6309,23 @@ mod tests {
63196309

63206310
db.write_files([
63216311
("/src/a.py", "from foo import x"),
6322-
("/src/foo.py", "x = 10\ny = 20"),
6312+
("/src/foo.py", "x: int = 10\ny: bool = True"),
63236313
])?;
63246314

63256315
let a = system_path_to_file(&db, "/src/a.py").unwrap();
63266316
let x_ty = global_symbol(&db, a, "x").expect_type();
63276317

6328-
assert_eq!(x_ty.display(&db).to_string(), "Literal[10]");
6318+
assert_eq!(x_ty.display(&db).to_string(), "int");
63296319

6330-
db.write_file("/src/foo.py", "x = 10\ny = 30")?;
6320+
db.write_file("/src/foo.py", "x: int = 10\ny: bool = False")?;
63316321

63326322
let a = system_path_to_file(&db, "/src/a.py").unwrap();
63336323

63346324
db.clear_salsa_events();
63356325

63366326
let x_ty_2 = global_symbol(&db, a, "x").expect_type();
63376327

6338-
assert_eq!(x_ty_2.display(&db).to_string(), "Literal[10]");
6328+
assert_eq!(x_ty_2.display(&db).to_string(), "int");
63396329

63406330
let events = db.take_salsa_events();
63416331

0 commit comments

Comments
 (0)